Skip to content

Commit 9d4e7b8

Browse files
authored
PHPLIB-1141: Setup rector in CI (#1088)
* Setup rector config * Setup rector in CI * Merge rector into static analysis job * Disable bugged rules * Fix BucketFunctionalTest::testOpenDownloadStreamByNameShouldRequireFilenameAndRevisionToExist
1 parent b4909e8 commit 9d4e7b8

File tree

9 files changed

+32
-18
lines changed

9 files changed

+32
-18
lines changed

.github/workflows/static-analysis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ jobs:
6565

6666
- name: "Run Psalm"
6767
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)"
68+
69+
- name: "Run Rector"
70+
run: "vendor/bin/rector --ansi --dry-run"

examples/aggregate.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function MongoDB\BSON\fromPHP;
1212
use function MongoDB\BSON\toRelaxedExtendedJSON;
1313
use function printf;
14-
use function rand;
14+
use function random_int;
1515

1616
require __DIR__ . '/../vendor/autoload.php';
1717

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

3030
for ($i = 0; $i < 100; $i++) {
31-
$documents[] = ['randomValue' => rand(0, 1000)];
31+
$documents[] = ['randomValue' => random_int(0, 1000)];
3232
}
3333

3434
$collection->insertMany($documents);

phpcs.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<file>examples</file>
1414
<file>tests</file>
1515
<file>tools</file>
16+
<file>rector.php</file>
1617

1718
<!-- Target minimum supported PHP version -->
1819
<config name="php_version" value="70200"/>

rector.php

+18-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
use Rector\Config\RectorConfig;
44
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
5+
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
6+
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
7+
use Rector\Set\ValueObject\LevelSetList;
58

69
return static function (RectorConfig $rectorConfig): void {
710
$rectorConfig->paths([
@@ -11,13 +14,20 @@
1114
__DIR__ . '/tools',
1215
]);
1316

14-
/**
15-
* All classes are public API by default, unless marked with @internal.
16-
*/
17-
$rectorConfig->ruleWithConfiguration(RemoveAnnotationRector::class, ['api']);
17+
// Modernize code
18+
$rectorConfig->sets([LevelSetList::UP_TO_PHP_72]);
19+
20+
$rectorConfig->skip([
21+
// Falsely detect unassigned variables in code paths stopped by PHPUnit\Framework\Assert::markTestSkipped()
22+
AddDefaultValueForUndefinedVariableRector::class => [
23+
__DIR__ . '/tests/',
24+
],
25+
// @see https://github.com/phpstan/phpstan-src/pull/2429
26+
RemoveExtraParametersRector::class => [
27+
__DIR__ . '/src/Operation/',
28+
],
29+
]);
1830

19-
// define sets of rules
20-
// $rectorConfig->sets([
21-
// LevelSetList::UP_TO_PHP_72
22-
// ]);
31+
// All classes are public API by default, unless marked with @internal.
32+
$rectorConfig->ruleWithConfiguration(RemoveAnnotationRector::class, ['api']);
2333
};

tests/FunctionsTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function provideTypeMapValues()
210210
'Array field path converted to array' => [
211211
[
212212
'root' => 'object',
213-
'array' => 'MongoDB\Model\BSONArray',
213+
'array' => BSONArray::class,
214214
'fieldPaths' => [
215215
'field' => 'array',
216216
'field.$' => 'object',
@@ -219,22 +219,22 @@ public function provideTypeMapValues()
219219
],
220220
[
221221
'root' => 'object',
222-
'array' => 'MongoDB\Model\BSONArray',
222+
'array' => BSONArray::class,
223223
'fieldPaths' => ['nested' => 'array'],
224224
],
225225
'field.$',
226226
],
227227
'Array field path without root key' => [
228228
[
229229
'root' => 'object',
230-
'array' => 'MongoDB\Model\BSONArray',
230+
'array' => BSONArray::class,
231231
'fieldPaths' => [
232232
'field' => 'array',
233233
'field.$.nested' => 'array',
234234
],
235235
],
236236
[
237-
'array' => 'MongoDB\Model\BSONArray',
237+
'array' => BSONArray::class,
238238
'fieldPaths' => ['nested' => 'array'],
239239
],
240240
'field.$',

tests/GridFS/BucketFunctionalTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public function testOpenDownloadStreamByNameShouldRequireFilenameAndRevisionToEx
549549
$this->bucket->uploadFromStream('filename', $this->createStream('bar'));
550550

551551
$this->expectException(FileNotFoundException::class);
552-
$this->bucket->openDownloadStream($filename, ['revision' => $revision]);
552+
$this->bucket->openDownloadStreamByName($filename, ['revision' => $revision]);
553553
}
554554

555555
public function testOpenUploadStream(): void

tests/GridFS/UnusableStream.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function register($protocol = 'unusable'): void
2020
stream_wrapper_unregister($protocol);
2121
}
2222

23-
stream_wrapper_register($protocol, static::class, STREAM_IS_URL);
23+
stream_wrapper_register($protocol, self::class, STREAM_IS_URL);
2424
}
2525

2626
public function stream_close(): void

tests/Operation/WatchFunctionalTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,7 @@ public function testResumeTokenNotFoundDoesNotAdvanceKey(): void
11071107
try {
11081108
$changeStream->next();
11091109
$this->fail('Exception for missing resume token was not thrown');
1110-
} catch (ResumeTokenException $e) {
1111-
} catch (ServerException $e) {
1110+
} catch (ResumeTokenException | ServerException $e) {
11121111
}
11131112

11141113
$this->assertFalse($changeStream->valid());

tests/UnifiedSpecTests/Constraint/Matches.php

+1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ private function doToString()
368368
return 'matches ' . $this->exporter()->export($this->value);
369369
}
370370

371+
/** @psalm-return never-return */
371372
private static function failAt(string $message, string $keyPath): void
372373
{
373374
$prefix = empty($keyPath) ? '' : sprintf('Field path "%s": ', $keyPath);

0 commit comments

Comments
 (0)