Skip to content

PHPLIB-1141: Setup rector #1088

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

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

4 changes: 2 additions & 2 deletions examples/aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 😉

}

$collection->insertMany($documents);
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<file>examples</file>
<file>tests</file>
<file>tools</file>
<file>rector.php</file>

<!-- Target minimum supported PHP version -->
<config name="php_version" value="70200"/>
Expand Down
26 changes: 18 additions & 8 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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
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?

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']);
};
8 changes: 4 additions & 4 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -219,22 +219,22 @@ public function provideTypeMapValues()
],
[
'root' => 'object',
'array' => 'MongoDB\Model\BSONArray',
'array' => BSONArray::class,
'fieldPaths' => ['nested' => 'array'],
],
'field.$',
],
'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.$',
Expand Down
2 changes: 1 addition & 1 deletion tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/GridFS/UnusableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}

public function stream_close(): void
Expand Down
3 changes: 1 addition & 2 deletions tests/Operation/WatchFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

$this->assertFalse($changeStream->valid());
Expand Down
1 change: 1 addition & 0 deletions tests/UnifiedSpecTests/Constraint/Matches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

private static function failAt(string $message, string $keyPath): void
{
$prefix = empty($keyPath) ? '' : sprintf('Field path "%s": ', $keyPath);
Expand Down