Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1141: Setup rector #1088

Merged
merged 5 commits into from
Jun 1, 2023
Merged

PHPLIB-1141: Setup rector #1088

merged 5 commits into from
Jun 1, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented May 30, 2023

Fix PHPLIB-1141

The library uses rector to refactor the code for new features.
To run automatic refactoring, use the rector command:

$ vendor/bin/rector

Job merged with psalm: https://github.com/mongodb/mongo-php-library/actions/runs/5134018025/jobs/9237409213?pr=1088

The rules need to be adjusted due to false positive like this:

  • The 3rd argument is correctly defined in ext-mongodb stub.
src/Operation/Delete.php:164

    ---------- begin diff ----------
@@ @@
         $bulk = new Bulk($this->createBulkWriteOptions());
         $bulk->delete($this->filter, $this->createDeleteOptions());

-        $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createExecuteOptions());
+        $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk);

         return new DeleteResult($writeResult);
     }
    ----------- end diff -----------
  • The variable is always initialized when accessed.
tests/SpecTests/ClientSideEncryptionSpecTest.php:308

    ---------- begin diff ----------
@@ @@
      */
     public function testClientSideEncryption(stdClass $test, ?array $runOn, array $data, ?stdClass $encryptedFields = null, ?array $keyVaultData = null, ?stdClass $jsonSchema = null, ?string $databaseName = null, ?string $collectionName = null): void
     {
+        $context = null;
         if (isset(self::$incompleteTests[$this->dataDescription()])) {
             $this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);
         }
@@ @@

     private function encryptCorpusValue(string $fieldName, stdClass $data, ClientEncryption $clientEncryption)
     {
+        $encrypted = null;
         $encryptionOptions = [
             'algorithm' => $data->algo === 'rand' ? ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_RANDOM : ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC,
         ];
    ----------- end diff -----------

@GromNaN GromNaN force-pushed the PHPLIB-1141 branch 2 times, most recently from 8789703 to 8d5f04c Compare May 30, 2023 15:54
@GromNaN GromNaN marked this pull request as draft May 30, 2023 16:02
@GromNaN GromNaN force-pushed the PHPLIB-1141 branch 3 times, most recently from 7905ad1 to 7da6c97 Compare May 31, 2023 14:46
@@ -28,7 +28,7 @@ function toJSON(object $document): string
$documents = [];

for ($i = 0; $i < 100; $i++) {
$documents[] = ['randomValue' => rand(0, 1000)];
$documents[] = ['randomValue' => random_int(0, 1000)];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit overzealous, as we don't actually need a cryptographically secure number here. But it's also just an example file so I don't feel strongly about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the new "best practice". Using it proves that the doc have been updated 😉

@@ -20,7 +20,7 @@ public static function register($protocol = 'unusable'): void
stream_wrapper_unregister($protocol);
}

stream_wrapper_register($protocol, static::class, STREAM_IS_URL);
stream_wrapper_register($protocol, self::class, STREAM_IS_URL);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static is useless for a final class. self can be resolved at compile time.

@@ -368,6 +368,7 @@ private function doToString()
return 'matches ' . $this->exporter()->export($this->value);
}

/** @psalm-return never-return */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helps to detect code paths and avoid false-positive undefined variable access.

tests/GridFS/BucketFunctionalTest.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from jmikola May 31, 2023 14:47
@GromNaN GromNaN marked this pull request as ready for review May 31, 2023 14:48
@GromNaN GromNaN requested a review from alcaeus May 31, 2023 14:48
@jmikola
Copy link
Member

jmikola commented Jun 1, 2023

The variable is always initialized when accessed (in ClientSideEncryptionSpecTest)

Are those two examples due to the tooling not realizing that fail() and markTestIncomplete() throw?

@@ -28,7 +28,7 @@ function toJSON(object $document): string
$documents = [];

for ($i = 0; $i < 100; $i++) {
$documents[] = ['randomValue' => rand(0, 1000)];
$documents[] = ['randomValue' => random_int(0, 1000)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit overzealous, as we don't actually need a cryptographically secure number here. But it's also just an example file so I don't feel strongly about it.

AddDefaultValueForUndefinedVariableRector::class => [
__DIR__ . '/tests/',
],
// @see https://github.com/phpstan/phpstan-src/pull/2429
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would See: make more sense here since we're not in an actual doc block?

tests/GridFS/BucketFunctionalTest.php Outdated Show resolved Hide resolved
@@ -1107,8 +1107,7 @@ public function testResumeTokenNotFoundDoesNotAdvanceKey(): void
try {
$changeStream->next();
$this->fail('Exception for missing resume token was not thrown');
} catch (ResumeTokenException $e) {
} catch (ServerException $e) {
} catch (ResumeTokenException | ServerException $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed that this was available since 7.1 (long before general union types in 8.0): https://wiki.php.net/rfc/multiple-catch

@@ -65,3 +65,6 @@ jobs:

- name: "Run Psalm"
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)"

- name: "Run Rector"
run: "vendor/bin/rector --ansi --dry-run"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted that --dry-run returns a non-zero exit code if changes are detected, which makes it suitable for CI.

tests/GridFS/BucketFunctionalTest.php Outdated Show resolved Hide resolved
@GromNaN
Copy link
Member Author

GromNaN commented Jun 1, 2023

Are those two examples due to the tooling not realizing that fail() and markTestIncomplete() throw?

PHPUnit 9.6 had the annotation @psalm-return never-return that was then replaced by :never native return type.

People seems to have the opposite behavior: phpstan/phpstan-phpunit#52

@GromNaN GromNaN merged commit 9d4e7b8 into mongodb:master Jun 1, 2023
@GromNaN GromNaN deleted the PHPLIB-1141 branch June 1, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants