diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index ef1a2c0ba..5b0840407 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -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" diff --git a/examples/aggregate.php b/examples/aggregate.php index 7f9da6cc3..d1ee69450 100644 --- a/examples/aggregate.php +++ b/examples/aggregate.php @@ -11,7 +11,7 @@ use function MongoDB\BSON\fromPHP; use function MongoDB\BSON\toRelaxedExtendedJSON; use function printf; -use function rand; +use function random_int; require __DIR__ . '/../vendor/autoload.php'; @@ -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)]; } $collection->insertMany($documents); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 02b0fd035..a4dee252f 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -13,6 +13,7 @@ examples tests tools + rector.php diff --git a/rector.php b/rector.php index 08029a4d8..cf5d60690 100644 --- a/rector.php +++ b/rector.php @@ -2,6 +2,9 @@ use Rector\Config\RectorConfig; use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector; +use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector; +use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector; +use Rector\Set\ValueObject\LevelSetList; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ @@ -11,13 +14,20 @@ __DIR__ . '/tools', ]); - /** - * All classes are public API by default, unless marked with @internal. - */ - $rectorConfig->ruleWithConfiguration(RemoveAnnotationRector::class, ['api']); + // Modernize code + $rectorConfig->sets([LevelSetList::UP_TO_PHP_72]); + + $rectorConfig->skip([ + // Falsely detect unassigned variables in code paths stopped by PHPUnit\Framework\Assert::markTestSkipped() + AddDefaultValueForUndefinedVariableRector::class => [ + __DIR__ . '/tests/', + ], + // @see https://github.com/phpstan/phpstan-src/pull/2429 + RemoveExtraParametersRector::class => [ + __DIR__ . '/src/Operation/', + ], + ]); - // define sets of rules - // $rectorConfig->sets([ - // LevelSetList::UP_TO_PHP_72 - // ]); + // All classes are public API by default, unless marked with @internal. + $rectorConfig->ruleWithConfiguration(RemoveAnnotationRector::class, ['api']); }; diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 167cbe291..5f1170265 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -210,7 +210,7 @@ public function provideTypeMapValues() 'Array field path converted to array' => [ [ 'root' => 'object', - 'array' => 'MongoDB\Model\BSONArray', + 'array' => BSONArray::class, 'fieldPaths' => [ 'field' => 'array', 'field.$' => 'object', @@ -219,7 +219,7 @@ public function provideTypeMapValues() ], [ 'root' => 'object', - 'array' => 'MongoDB\Model\BSONArray', + 'array' => BSONArray::class, 'fieldPaths' => ['nested' => 'array'], ], 'field.$', @@ -227,14 +227,14 @@ public function provideTypeMapValues() 'Array field path without root key' => [ [ 'root' => 'object', - 'array' => 'MongoDB\Model\BSONArray', + 'array' => BSONArray::class, 'fieldPaths' => [ 'field' => 'array', 'field.$.nested' => 'array', ], ], [ - 'array' => 'MongoDB\Model\BSONArray', + 'array' => BSONArray::class, 'fieldPaths' => ['nested' => 'array'], ], 'field.$', diff --git a/tests/GridFS/BucketFunctionalTest.php b/tests/GridFS/BucketFunctionalTest.php index 97eb7317d..dbdfe2017 100644 --- a/tests/GridFS/BucketFunctionalTest.php +++ b/tests/GridFS/BucketFunctionalTest.php @@ -549,7 +549,7 @@ public function testOpenDownloadStreamByNameShouldRequireFilenameAndRevisionToEx $this->bucket->uploadFromStream('filename', $this->createStream('bar')); $this->expectException(FileNotFoundException::class); - $this->bucket->openDownloadStream($filename, ['revision' => $revision]); + $this->bucket->openDownloadStreamByName($filename, ['revision' => $revision]); } public function testOpenUploadStream(): void diff --git a/tests/GridFS/UnusableStream.php b/tests/GridFS/UnusableStream.php index c7686638b..34e964da4 100644 --- a/tests/GridFS/UnusableStream.php +++ b/tests/GridFS/UnusableStream.php @@ -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); } public function stream_close(): void diff --git a/tests/Operation/WatchFunctionalTest.php b/tests/Operation/WatchFunctionalTest.php index cae3d850a..aefadcb56 100644 --- a/tests/Operation/WatchFunctionalTest.php +++ b/tests/Operation/WatchFunctionalTest.php @@ -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) { } $this->assertFalse($changeStream->valid()); diff --git a/tests/UnifiedSpecTests/Constraint/Matches.php b/tests/UnifiedSpecTests/Constraint/Matches.php index eea67a153..579240d69 100644 --- a/tests/UnifiedSpecTests/Constraint/Matches.php +++ b/tests/UnifiedSpecTests/Constraint/Matches.php @@ -368,6 +368,7 @@ private function doToString() return 'matches ' . $this->exporter()->export($this->value); } + /** @psalm-return never-return */ private static function failAt(string $message, string $keyPath): void { $prefix = empty($keyPath) ? '' : sprintf('Field path "%s": ', $keyPath);