From bdf146ad9bd5e0c759855d853c75505ec0195653 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Nov 2024 01:58:04 +0700 Subject: [PATCH 1/8] Use rector-src#dev-upgrade-to-php-parser5-and-phpstan-2 --- composer.json | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index a0834909..45f30783 100644 --- a/composer.json +++ b/composer.json @@ -7,18 +7,14 @@ "php": ">=8.2" }, "require-dev": { - "rector/rector-src": "dev-main", + "rector/rector-src": "dev-upgrade-to-php-parser5-and-phpstan-2", "phpunit/phpunit": "^10.5", - "phpstan/phpstan": "^1.12", - "symplify/phpstan-rules": "^13.0", - "symplify/phpstan-extensions": "^11.4", + "phpstan/phpstan": "^2.0", "symplify/easy-coding-standard": "^12.3", "phpstan/extension-installer": "^1.4", - "phpstan/phpstan-webmozart-assert": "^1.2", "symplify/vendor-patches": "^11.3", "tracy/tracy": "^2.10", "tomasvotruba/class-leak": "^1.2", - "rector/type-perfect": "^1.0", "rector/swiss-knife": "^1.0" }, "autoload": { From 8a371800086498e3ceabd24de103eb2ada1ca4ee Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 19 Nov 2024 18:59:04 +0000 Subject: [PATCH 2/8] [ci-review] Rector Rectify --- .../Rector/StmtsAwareInterface/WithConsecutiveRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 12fded67..5a14a5dd 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -221,7 +221,7 @@ public function refactor(Node $node) */ private function refactorToWillReturnCallback( MethodCall $withConsecutiveMethodCall, - ?Stmt $returnStmt, + Return_|Throw_|null $returnStmt, Expr|Variable|null $referenceVariable, StaticCall|MethodCall $expectsCall, Expression $expression, From cabeaee8d9b8ee07aa9671dfb1561e0782b08437 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Nov 2024 02:01:15 +0700 Subject: [PATCH 3/8] update node class usage --- .../NodeFactory/NestedClosureAssertFactory.php | 8 ++++---- .../CreateMockToAnonymousClassRector.php | 3 ++- .../DataProviderArrayItemsNewLinedRector.php | 2 +- .../Class_/SetUpBeforeClassToSetUpRector.php | 9 +++++---- .../Rector/Class_/TestWithToDataProviderRector.php | 5 +++-- ...meFloatParameterToSpecificMethodsTypeRector.php | 4 ++-- .../Rector/MethodCall/AssertEqualsToSameRector.php | 4 ++-- .../Rector/MethodCall/AssertRegExpRector.php | 4 ++-- .../NarrowSingleWillReturnCallbackRector.php | 4 ++-- .../NodeFactory/WillReturnCallbackFactory.php | 6 +++--- .../StmtsAwareInterface/WithConsecutiveRector.php | 6 +++--- src/NodeFactory/ConsecutiveIfsFactory.php | 14 +++++++------- 12 files changed, 36 insertions(+), 33 deletions(-) diff --git a/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php b/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php index cf9f44a0..8831324a 100644 --- a/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php +++ b/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\NodeFactory; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -16,7 +17,6 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use Rector\PHPUnit\Enum\ConsecutiveVariable; @@ -59,7 +59,7 @@ public function create(MethodCall $assertMethodCall, int $assertKey): array $stmts = [new Expression($callbackAssign)]; - $parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey)); + $parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey)); $callbackFuncCall = new FuncCall($callbackVariable, [new Arg($parametersArrayDimFetch)]); // add assert true to the callback @@ -77,7 +77,7 @@ private function createAssertSameParameters(Expr $comparedExpr, int $assertKey): // use assert same directly instead $args = [ new Arg($comparedExpr), - new Arg(new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey))), + new Arg(new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey))), ]; $assertSameMethodCall = new MethodCall(new Variable('this'), new Identifier('assertSame'), $args); @@ -90,7 +90,7 @@ private function createAssertSameParameters(Expr $comparedExpr, int $assertKey): */ private function createAssertNotEmpty(int $assertKey, string $emptyMethodName): array { - $arrayDimFetch = new ArrayDimFetch(new Variable(ConsecutiveVariable::PARAMETERS), new LNumber($assertKey)); + $arrayDimFetch = new ArrayDimFetch(new Variable(ConsecutiveVariable::PARAMETERS), new Int_($assertKey)); $assertEmptyMethodCall = new MethodCall(new Variable('this'), new Identifier($emptyMethodName), [ new Arg($arrayDimFetch), diff --git a/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php b/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php index 1aaf160d..e427593b 100644 --- a/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; +use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -246,7 +247,7 @@ private function createMockedClassMethod(MethodCall $rootMethodCall, MethodCall ->value; return new ClassMethod($methodName, [ - 'flags' => Class_::MODIFIER_PUBLIC, + 'flags' => Modifiers::PUBLIC, 'stmts' => [new Return_($returnedExpr)], ]); } diff --git a/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php b/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php index 09d6222d..b1ef8c66 100644 --- a/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php @@ -4,9 +4,9 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; +use PhpParser\Node\ArrayItem; use PhpParser\Node; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; use Rector\NodeTypeResolver\Node\AttributeKey; diff --git a/rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.php b/rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.php index bba3be3f..94f3e20e 100644 --- a/rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.php +++ b/rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; +use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\PropertyFetch; @@ -134,13 +135,13 @@ public function refactor(Node $node): ?Node } // remove static flag - $setUpBeforeClassMethod->flags -= Class_::MODIFIER_STATIC; + $setUpBeforeClassMethod->flags -= Modifiers::STATIC; // remove public flag - $setUpBeforeClassMethod->flags -= Class_::MODIFIER_PUBLIC; + $setUpBeforeClassMethod->flags -= Modifiers::PUBLIC; // make protected - $setUpBeforeClassMethod->flags += Class_::MODIFIER_PROTECTED; + $setUpBeforeClassMethod->flags += Modifiers::PROTECTED; $setUpBeforeClassMethod->name = new Identifier('setUp'); @@ -150,7 +151,7 @@ public function refactor(Node $node): ?Node } if ($this->isNames($property, $changedPropertyNames)) { - $property->flags -= Class_::MODIFIER_STATIC; + $property->flags -= Modifiers::STATIC; } } diff --git a/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php b/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php index 1d6d2724..b75060b2 100644 --- a/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php +++ b/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php @@ -4,12 +4,13 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; +use PhpParser\Node\ArrayItem; +use PhpParser\Modifiers; use Nette\Utils\Json; use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; @@ -174,7 +175,7 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): v } $providerMethod = new ClassMethod($dataProviderName); - $providerMethod->flags = Class_::MODIFIER_PUBLIC; + $providerMethod->flags = Modifiers::PUBLIC; $providerMethod->stmts[] = new Return_($returnValue); $this->classInsertManipulator->addAsFirstMethod($class, $providerMethod); } diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php index 7cf6f44b..2204930e 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php @@ -4,12 +4,12 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\DNumber; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\PHPUnit\NodeFactory\AssertCallFactory; use Rector\Rector\AbstractRector; @@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node $args = $node->getArgs(); $firstValue = $args[0]->value; - if (! $firstValue instanceof DNumber) { + if (! $firstValue instanceof Float_) { return null; } diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 9ea25ff1..650b18eb 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -4,12 +4,12 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Scalar\Encapsed; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; @@ -152,7 +152,7 @@ private function isScalarOrEnumValue(Expr $expr): bool return true; } - if ($expr instanceof Encapsed) { + if ($expr instanceof InterpolatedString) { return true; } diff --git a/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php b/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php index 5ce28cf6..f98934b4 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ConstFetch; @@ -12,7 +13,6 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\Expression; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Exception\ShouldNotHappenException; @@ -159,7 +159,7 @@ public function refactor(Node $node): ?Node private function resolveOldCondition(Expr $expr): int { - if ($expr instanceof LNumber) { + if ($expr instanceof Int_) { return $expr->value; } diff --git a/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php b/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php index c78550a5..02242b08 100644 --- a/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php +++ b/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -12,7 +13,6 @@ use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use Rector\PHPUnit\CodeQuality\ValueObject\MatchAndReturnMatch; @@ -204,7 +204,7 @@ private function matchSingleMatchArmBodyWithConditionOne(Match_ $match): ?Expr private function isNumberOne(Expr $expr): bool { - if (! $expr instanceof LNumber) { + if (! $expr instanceof Int_) { return false; } diff --git a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php index ed8eea76..24783f53 100644 --- a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php +++ b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php @@ -4,17 +4,17 @@ namespace Rector\PHPUnit\PHPUnit100\NodeFactory; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\ClosureUse; use PhpParser\BuilderFactory; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\Minus; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\ClosureUse; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Param; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use Rector\PHPUnit\Enum\ConsecutiveVariable; @@ -80,7 +80,7 @@ private function createAssertSameDimFetch(Arg $firstArg, Variable $variable): Me $currentValueArrayDimFetch = new ArrayDimFetch($firstArg->value, new Minus( $matcherCountMethodCall, - new LNumber(1) + new Int_(1) )); $compareArgs = [new Arg($currentValueArrayDimFetch), new Arg($variable)]; diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 5a14a5dd..78f16267 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\PHPUnit100\Rector\StmtsAwareInterface; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -15,7 +16,6 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Param; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; @@ -188,8 +188,8 @@ public function refactor(Node $node) if (! $expectsCall instanceof MethodCall && ! $expectsCall instanceof StaticCall) { // fallback to default by case count - $lNumber = new LNumber(\count($withConsecutiveMethodCall->args)); - $expectsCall = new MethodCall(new Variable('this'), new Identifier('exactly'), [new Arg($lNumber)]); + $int = new Int_(\count($withConsecutiveMethodCall->args)); + $expectsCall = new MethodCall(new Variable('this'), new Identifier('exactly'), [new Arg($int)]); } // 2. does willReturnCallback() exist? just merge them together diff --git a/src/NodeFactory/ConsecutiveIfsFactory.php b/src/NodeFactory/ConsecutiveIfsFactory.php index c2a92185..8d7078e8 100644 --- a/src/NodeFactory/ConsecutiveIfsFactory.php +++ b/src/NodeFactory/ConsecutiveIfsFactory.php @@ -4,16 +4,16 @@ namespace Rector\PHPUnit\NodeFactory; +use PhpParser\Node\ArrayItem; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; -use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; @@ -49,7 +49,7 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num } if (! $assertArrayItem->value instanceof MethodCall) { - $parametersDimFetch = new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey)); + $parametersDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey)); $args = [new Arg($assertArrayItem), new Arg($parametersDimFetch)]; $ifStmts[] = new Expression(new MethodCall(new Variable('this'), 'assertSame', $args)); continue; @@ -67,7 +67,7 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num } else { $args = [ new Arg($assertMethodCall), - new Arg(new ArrayDimFetch(new Variable('parameters'), new LNumber($assertKey))), + new Arg(new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey))), ]; $assertSameMethodCall = new MethodCall(new Variable('this'), new Identifier( @@ -88,7 +88,7 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num // @todo improve in time if ($identicalCompare->left instanceof Variable) { - $parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new LNumber( + $parametersArrayDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_( 0 )); @@ -107,7 +107,7 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num throw new NotImplementedYetException(); } - $ifs[] = new If_(new Identical($numberOfInvocationsMethodCall, new LNumber($key + 1)), [ + $ifs[] = new If_(new Identical($numberOfInvocationsMethodCall, new Int_($key + 1)), [ 'stmts' => $ifStmts, ]); } @@ -121,7 +121,7 @@ private function createAssertMethodCall( int $parameterPositionKey ): Expression { $assertMethodCall->name = new Identifier('assertEquals'); - $parametersArrayDimFetch = new ArrayDimFetch($parametersVariable, new LNumber($parameterPositionKey)); + $parametersArrayDimFetch = new ArrayDimFetch($parametersVariable, new Int_($parameterPositionKey)); $assertMethodCall->args[] = new Arg($parametersArrayDimFetch); From 97d42bf8d3388c513c4ebde96ec7a0d0ab78fe97 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 19 Nov 2024 19:01:56 +0000 Subject: [PATCH 4/8] [ci-review] Rector Rectify --- rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php | 2 +- .../ClassMethod/DataProviderArrayItemsNewLinedRector.php | 2 +- .../Rector/Class_/TestWithToDataProviderRector.php | 4 ++-- ...sOrAssertSameFloatParameterToSpecificMethodsTypeRector.php | 2 +- .../Rector/MethodCall/AssertEqualsToSameRector.php | 2 +- rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php | 2 +- .../MethodCall/NarrowSingleWillReturnCallbackRector.php | 2 +- rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php | 4 ++-- .../Rector/StmtsAwareInterface/WithConsecutiveRector.php | 2 +- src/NodeFactory/ConsecutiveIfsFactory.php | 4 ++-- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php b/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php index 8831324a..a844601d 100644 --- a/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php +++ b/rules/CodeQuality/NodeFactory/NestedClosureAssertFactory.php @@ -4,7 +4,6 @@ namespace Rector\PHPUnit\CodeQuality\NodeFactory; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; @@ -17,6 +16,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use Rector\PHPUnit\Enum\ConsecutiveVariable; diff --git a/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php b/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php index b1ef8c66..4d2186ff 100644 --- a/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php @@ -4,8 +4,8 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; -use PhpParser\Node\ArrayItem; use PhpParser\Node; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; diff --git a/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php b/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php index b75060b2..7dfa483a 100644 --- a/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php +++ b/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php @@ -4,11 +4,11 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; -use PhpParser\Node\ArrayItem; -use PhpParser\Modifiers; use Nette\Utils\Json; use Nette\Utils\Strings; +use PhpParser\Modifiers; use PhpParser\Node; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ConstFetch; diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php index 2204930e..4ffa593b 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector.php @@ -4,12 +4,12 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; -use PhpParser\Node\Scalar\Float_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Name; +use PhpParser\Node\Scalar\Float_; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\PHPUnit\NodeFactory\AssertCallFactory; use Rector\Rector\AbstractRector; diff --git a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php index 650b18eb..4c60861f 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php @@ -4,12 +4,12 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; -use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Scalar\InterpolatedString; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; diff --git a/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php b/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php index f98934b4..d8e62a7a 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertRegExpRector.php @@ -4,7 +4,6 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ConstFetch; @@ -13,6 +12,7 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\Expression; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Exception\ShouldNotHappenException; diff --git a/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php b/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php index 02242b08..77d844c2 100644 --- a/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php +++ b/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php @@ -4,7 +4,6 @@ namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -13,6 +12,7 @@ use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use Rector\PHPUnit\CodeQuality\ValueObject\MatchAndReturnMatch; diff --git a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php index 24783f53..321a21b6 100644 --- a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php +++ b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php @@ -4,10 +4,9 @@ namespace Rector\PHPUnit\PHPUnit100\NodeFactory; -use PhpParser\Node\Scalar\Int_; -use PhpParser\Node\ClosureUse; use PhpParser\BuilderFactory; use PhpParser\Node\Arg; +use PhpParser\Node\ClosureUse; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\Minus; @@ -15,6 +14,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Param; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use Rector\PHPUnit\Enum\ConsecutiveVariable; diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 78f16267..a925ccfa 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -4,7 +4,6 @@ namespace Rector\PHPUnit\PHPUnit100\Rector\StmtsAwareInterface; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; @@ -16,6 +15,7 @@ use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Param; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; diff --git a/src/NodeFactory/ConsecutiveIfsFactory.php b/src/NodeFactory/ConsecutiveIfsFactory.php index 8d7078e8..8db94997 100644 --- a/src/NodeFactory/ConsecutiveIfsFactory.php +++ b/src/NodeFactory/ConsecutiveIfsFactory.php @@ -4,9 +4,8 @@ namespace Rector\PHPUnit\NodeFactory; -use PhpParser\Node\ArrayItem; -use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Arg; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\ArrowFunction; @@ -14,6 +13,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; From 0ac8d98e3f30c276f0c0f0e0f53bcd2a9b76b04e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Nov 2024 02:04:03 +0700 Subject: [PATCH 5/8] fix throw as expr --- .../Rector/StmtsAwareInterface/WithConsecutiveRector.php | 6 +++--- src/NodeFactory/ConsecutiveIfsFactory.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index a925ccfa..888fb3a5 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -19,7 +19,7 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Throw_; +use PhpParser\Node\Expr\Throw_; use Rector\Exception\ShouldNotHappenException; use Rector\PHPUnit\Enum\ConsecutiveMethodName; use Rector\PHPUnit\Enum\ConsecutiveVariable; @@ -166,7 +166,7 @@ public function refactor(Node $node) if ($willThrowException instanceof MethodCall) { $this->methodCallRemover->removeMethodCall($node, ConsecutiveMethodName::WILL_THROW_EXCEPTION); $expr = $this->getFirstArgValue($willThrowException); - $returnStmt = new Throw_($expr); + $returnStmt = new Expression(new Throw_($expr)); } $willReturnReferenceArgument = $this->methodCallNodeFinder->findByName( @@ -221,7 +221,7 @@ public function refactor(Node $node) */ private function refactorToWillReturnCallback( MethodCall $withConsecutiveMethodCall, - Return_|Throw_|null $returnStmt, + Return_|Expression|null $returnStmt, Expr|Variable|null $referenceVariable, StaticCall|MethodCall $expectsCall, Expression $expression, diff --git a/src/NodeFactory/ConsecutiveIfsFactory.php b/src/NodeFactory/ConsecutiveIfsFactory.php index 8db94997..e0cfbfce 100644 --- a/src/NodeFactory/ConsecutiveIfsFactory.php +++ b/src/NodeFactory/ConsecutiveIfsFactory.php @@ -50,7 +50,7 @@ public function createIfs(MethodCall $withConsecutiveMethodCall, MethodCall $num if (! $assertArrayItem->value instanceof MethodCall) { $parametersDimFetch = new ArrayDimFetch(new Variable('parameters'), new Int_($assertKey)); - $args = [new Arg($assertArrayItem), new Arg($parametersDimFetch)]; + $args = [new Arg($assertArrayItem->value), new Arg($parametersDimFetch)]; $ifStmts[] = new Expression(new MethodCall(new Variable('this'), 'assertSame', $args)); continue; } From edaa099b4928246584618570351a569aa57ce82c Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 19 Nov 2024 19:04:59 +0000 Subject: [PATCH 6/8] [ci-review] Rector Rectify --- .../Rector/StmtsAwareInterface/WithConsecutiveRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 888fb3a5..e2065134 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Param; @@ -19,7 +20,6 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Expr\Throw_; use Rector\Exception\ShouldNotHappenException; use Rector\PHPUnit\Enum\ConsecutiveMethodName; use Rector\PHPUnit\Enum\ConsecutiveVariable; From b3247646ee38289699a73fdece3ba9a74d0e79dd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Nov 2024 02:16:30 +0700 Subject: [PATCH 7/8] fix phpstan --- composer.json | 2 +- phpstan.neon | 38 ++++++++++++++++--- .../NamedArgumentForDataProviderRector.php | 12 ++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 45f30783..97fcc841 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "@docs", "phpunit" ], - "phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify", + "phpstan": "vendor/bin/phpstan analyse --ansi", "check-cs": "vendor/bin/ecs check --ansi", "class-leak": "vendor/bin/class-leak check config src rules --skip-suffix \"Rector\"", "fix-cs": "vendor/bin/ecs check --fix --ansi", diff --git a/phpstan.neon b/phpstan.neon index c84a4e8c..1a7cae5d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -19,12 +19,13 @@ parameters: - */Fixture/* - */Expected/* - # see https://github.com/rectorphp/type-perfect/ - type_perfect: - no_mixed: true - null_over_false: true - narrow_param: true - narrow_return: true +# to be enabled later once rector upgraded to use phpstan v2 +# # see https://github.com/rectorphp/type-perfect/ +# type_perfect: +# no_mixed: true +# null_over_false: true +# narrow_param: true +# narrow_return: true ignoreErrors: # phpstan false positive @@ -34,3 +35,28 @@ parameters: - '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#' - '#PhpParser\\Node\\Stmt\\Expression is not generic#' + + # more advanced usage, but not always working + # see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110 + - '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#' + + - + identifier: instanceof.alwaysTrue + + - + identifier: argument.type + + # follow parent signature + - + identifier: return.unusedType + + + # phpstan instanceof + - + identifier: phpstanApi.instanceofAssumption + + - + identifier: phpstanApi.varTagAssumption + + - + identifier: assign.propertyType diff --git a/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php b/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php index 74c54e6d..c669a6c7 100644 --- a/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php +++ b/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php @@ -218,7 +218,7 @@ private function refactorArrayKey(Array_ $array, array $dataProviderNameMapping) $allArrayKeyNames = []; foreach ($array->items as $arrayItem) { - if ($arrayItem?->key instanceof String_) { + if ($arrayItem->key instanceof String_) { $needToSetAllKeyNames = true; $allArrayKeyNames[] = $arrayItem->key->value; } @@ -230,10 +230,6 @@ private function refactorArrayKey(Array_ $array, array $dataProviderNameMapping) } foreach ($array->items as $arrayIndex => $arrayItem) { - if ($arrayItem === null) { - continue; - } - if (! isset($dataProviderNameMapping[$arrayIndex])) { continue; } @@ -271,13 +267,13 @@ private function extractDataProviderArrayItem(ClassMethod $classMethod): iterabl $dataProviderTestCases = $stmt->expr; foreach ($dataProviderTestCases->items as $dataProviderTestCase) { - $arrayItem = $dataProviderTestCase?->value; + $arrayItem = $dataProviderTestCase->value; if ($arrayItem instanceof Array_) { yield $arrayItem; } - $variableName = $arrayItem === null ? null : $this->getName($arrayItem); + $variableName = $this->getName($arrayItem); if ( $arrayItem instanceof Variable && $variableName !== null @@ -285,7 +281,7 @@ private function extractDataProviderArrayItem(ClassMethod $classMethod): iterabl ) { $dataProviderList = $resolvedVariables[$variableName]; foreach ($dataProviderList->items as $dataProviderItem) { - if ($dataProviderItem?->value instanceof Array_) { + if ($dataProviderItem->value instanceof Array_) { yield $dataProviderItem->value; } } From 211e4e7bce3cb424581823b9b0ae43c5e9028cb5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 20 Nov 2024 18:12:16 +0700 Subject: [PATCH 8/8] require dev phpstan webmozart v2 and fix phsptan message return --- composer.json | 3 ++- phpstan.neon | 7 ++----- .../Rector/StmtsAwareInterface/WithConsecutiveRector.php | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 97fcc841..03c925fe 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "symplify/vendor-patches": "^11.3", "tracy/tracy": "^2.10", "tomasvotruba/class-leak": "^1.2", - "rector/swiss-knife": "^1.0" + "rector/swiss-knife": "^1.0", + "phpstan/phpstan-webmozart-assert": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index 1a7cae5d..70ebfee0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -46,11 +46,6 @@ parameters: - identifier: argument.type - # follow parent signature - - - identifier: return.unusedType - - # phpstan instanceof - identifier: phpstanApi.instanceofAssumption @@ -60,3 +55,5 @@ parameters: - identifier: assign.propertyType + + - '#::provideMinPhpVersion\(\) never returns \d+ so it can be removed from the return type#' diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index e2065134..4614cf11 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -106,8 +106,9 @@ public function getNodeTypes(): array /** * @param Expression $node + * @return null|Stmt[]|Expression */ - public function refactor(Node $node) + public function refactor(Node $node): null|array|Expression { if (! $this->testsNodeAnalyzer->isInTestClass($node)) { return null;