diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index dceab9f1..07712b30 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -50,7 +50,8 @@ jobs: - uses: shivammathur/setup-php@v2 with: - php-version: 8.3 + # use php 8.5 to allow test (void) cast syntax + php-version: 8.5 coverage: none - uses: "ramsey/composer-install@v2" diff --git a/config/set/downgrade-php85.php b/config/set/downgrade-php85.php index bac19e15..b2b2b876 100644 --- a/config/set/downgrade-php85.php +++ b/config/set/downgrade-php85.php @@ -4,6 +4,7 @@ use Rector\Config\RectorConfig; use Rector\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector; +use Rector\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector; use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector; use Rector\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector; use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector; @@ -18,6 +19,7 @@ DowngradeArrayFirstLastRector::class, DowngradeFinalPropertyPromotionRector::class, DowngradePipeOperatorRector::class, + DowngradeVoidCastRector::class, ]); // https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_driver_specific_pdo_constants_and_methods diff --git a/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/DowngradeVoidCastRectorTest.php b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/DowngradeVoidCastRectorTest.php new file mode 100644 index 00000000..51fd6f39 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/DowngradeVoidCastRectorTest.php @@ -0,0 +1,30 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..8bbe5cd8 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/fixture.php.inc @@ -0,0 +1,41 @@ +foo(); + } +} + +?> +----- +foo(); + } +} + +?> diff --git a/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/skip_different_func_call.php.inc b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/skip_different_func_call.php.inc new file mode 100644 index 00000000..36aa6ce8 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/Fixture/skip_different_func_call.php.inc @@ -0,0 +1,15 @@ + diff --git a/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/config/configured_rule.php b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/config/configured_rule.php new file mode 100644 index 00000000..1566854b --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeVoidCastRector::class); +}; diff --git a/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php b/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php index dac20d57..1f8f75e2 100644 --- a/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php +++ b/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php @@ -118,6 +118,10 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $ { $lastPosition = array_key_last($array); + if ($lastPosition === null) { + return null; + } + $last = $array[$lastPosition]; if (! $this->followedByCommaAnalyzer->isFollowed($this->file, $last)) { return null; diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php index 2879d3a3..dd96ee9a 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradePropertyPromotionRector.php @@ -112,7 +112,7 @@ private function getOldComments(ClassMethod $constructorClassMethod): array { $oldComments = []; foreach ($constructorClassMethod->params as $param) { - $oldComments[$this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS); + $oldComments[(string) $this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS); } return $oldComments; diff --git a/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php b/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php new file mode 100644 index 00000000..931b9f32 --- /dev/null +++ b/rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php @@ -0,0 +1,84 @@ +expr instanceof Void_) { + return null; + } + + $scope = ScopeFetcher::fetch($node); + $variable = new Variable($this->variableNaming->createCountedValueName('_void', $scope)); + + // the assign is needed to avoid warning + // see https://3v4l.org/ie68D#v8.5.3 vs https://3v4l.org/nLc5J#v8.5.3 + $node->expr = new Assign( + $variable, + $node->expr->expr + ); + return $node; + } +}