diff --git a/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/on_static_closure.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/on_static_closure.php.inc new file mode 100644 index 00000000..b6cc6177 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/on_static_closure.php.inc @@ -0,0 +1,47 @@ +createMock('SomeClass') + ->expects($this->once()) + ->method('someMethod') + ->with($this->callback(static function (array $args): bool { + return count($args) === 2 && $args[0] === 'correct'; + })); + } +} + +?> +----- +createMock('SomeClass') + ->expects($this->once()) + ->method('someMethod') + ->with($this->callback(static function (array $args): bool { + self::assertCount(2, $args); + self::assertSame('correct', $args[0]); + return true; + })); + } +} + +?> diff --git a/rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php b/rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php index 9b25d553..49354c12 100644 --- a/rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php +++ b/rules/CodeQuality/NodeFactory/FromBinaryAndAssertExpressionsFactory.php @@ -13,6 +13,7 @@ use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\Int_; @@ -33,14 +34,14 @@ public function __construct( * @param Expr[] $exprs * @return Stmt[] */ - public function create(array $exprs): array + public function create(array $exprs, bool $isStaticClosure = false): array { $assertMethodCalls = []; foreach ($exprs as $expr) { // implicit bool compare if ($expr instanceof MethodCall) { - $assertMethodCalls[] = $this->nodeFactory->createMethodCall('this', 'assertTrue', [$expr]); + $assertMethodCalls[] = $this->createAssertMethodCall($isStaticClosure, 'assertTrue', [$expr]); continue; } @@ -51,8 +52,8 @@ public function create(array $exprs): array $dimExpr = $expr->getArgs()[0] ->value; - $assertMethodCalls[] = $this->nodeFactory->createMethodCall( - 'this', + $assertMethodCalls[] = $this->createAssertMethodCall( + $isStaticClosure, 'assertArrayHasKey', [$dimExpr, $variableExpr] ); @@ -62,9 +63,9 @@ public function create(array $exprs): array if ($expr instanceof Isset_) { foreach ($expr->vars as $issetVariable) { - if ($issetVariable instanceof ArrayDimFetch) { - $assertMethodCalls[] = $this->nodeFactory->createMethodCall( - 'this', + if ($issetVariable instanceof ArrayDimFetch && $issetVariable->dim instanceof Expr) { + $assertMethodCalls[] = $this->createAssertMethodCall( + $isStaticClosure, 'assertArrayHasKey', [$issetVariable->dim, $issetVariable->var] ); @@ -86,8 +87,8 @@ public function create(array $exprs): array $classNameExpr = $expr->class; } - $assertMethodCalls[] = $this->nodeFactory->createMethodCall( - 'this', + $assertMethodCalls[] = $this->createAssertMethodCall( + $isStaticClosure, 'assertInstanceOf', [$classNameExpr, $expr->expr] ); @@ -102,8 +103,8 @@ public function create(array $exprs): array ->value; // create assertCount() - $assertMethodCalls[] = $this->nodeFactory->createMethodCall( - 'this', + $assertMethodCalls[] = $this->createAssertMethodCall( + $isStaticClosure, 'assertCount', [$expr->right, $countedExpr] ); @@ -116,8 +117,8 @@ public function create(array $exprs): array } // create assertSame() - $assertMethodCalls[] = $this->nodeFactory->createMethodCall( - 'this', + $assertMethodCalls[] = $this->createAssertMethodCall( + $isStaticClosure, $expr instanceof Identical ? 'assertSame' : 'assertEquals', [$expr->right, $expr->left] ); @@ -141,4 +142,16 @@ public function create(array $exprs): array return $stmts; } + + /** + * @param Expr[] $args + */ + private function createAssertMethodCall(bool $isStaticClosure, string $method, array $args): MethodCall|StaticCall + { + if ($isStaticClosure) { + return new StaticCall(new Name('self'), $method, $this->nodeFactory->createArgs($args)); + } + + return $this->nodeFactory->createMethodCall('this', $method, $args); + } } diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index 02b5d843..5b9e7ae0 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -88,7 +88,7 @@ public function refactor(Node $node): ?Node $hasAnnotation = false; foreach(NewLineSplitter::split($docComment->getText()) as $row) { - if (in_array(trim($row), ['*@test', '* @test'])) { + if (in_array(trim($row), ['*@test', '* @test'], true)) { $hasAnnotation = true; break; } diff --git a/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php b/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php index a7766975..dffafec4 100644 --- a/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php +++ b/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php @@ -125,7 +125,10 @@ public function refactor(Node $node): MethodCall|null continue; } - $assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs); + $innerFunctionLike = $argAndFunctionLike->getFunctionLike(); + $isStaticClosure = $innerFunctionLike instanceof Closure && $innerFunctionLike->static; + + $assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs, $isStaticClosure); if ($assertExprStmts === []) { continue; } @@ -134,7 +137,6 @@ public function refactor(Node $node): MethodCall|null // last si return true; $assertExprStmts[] = new Return_($this->nodeFactory->createTrue()); - $innerFunctionLike = $argAndFunctionLike->getFunctionLike(); if ($innerFunctionLike instanceof Closure) { $innerFunctionLike->stmts = array_merge($nonReturnCallbackStmts, $assertExprStmts);