From 06aff226010c40d051c202e81595b6468322c990 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 1 Nov 2025 14:11:15 +0100 Subject: [PATCH] add sole instanceof support --- .../Fixture/handle_solo_instance.php.inc | 48 +++++++++++++++++++ .../Fixture/handle_solo_isset.php.inc | 48 +++++++++++++++++++ ...backIdenticalToStandaloneAssertsRector.php | 4 +- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_instance.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_isset.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_instance.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_instance.php.inc new file mode 100644 index 00000000..f9fc2353 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_instance.php.inc @@ -0,0 +1,48 @@ +createMock('AnyType') + ->expects($this->any()) + ->method('trans') + ->with( + $this->callback( + fn ($arg): bool => $arg instanceof \stdClass + ) + ); + } +} + +?> +----- +createMock('AnyType') + ->expects($this->any()) + ->method('trans') + ->with( + $this->callback( + function ($arg): bool { + $this->assertInstanceOf(\stdClass::class, $arg); + return true; + } + ) + ); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_isset.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_isset.php.inc new file mode 100644 index 00000000..61182878 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector/Fixture/handle_solo_isset.php.inc @@ -0,0 +1,48 @@ +createMock('AnyType') + ->expects($this->any()) + ->method('trans') + ->with( + $this->callback( + fn ($args): bool => isset($args['key']) + ) + ); + } +} + +?> +----- +createMock('AnyType') + ->expects($this->any()) + ->method('trans') + ->with( + $this->callback( + function ($args): bool { + $this->assertArrayHasKey('key', $args); + return true; + } + ) + ); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php b/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php index ba11a7ee..0d90f15f 100644 --- a/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php +++ b/rules/CodeQuality/Rector/MethodCall/WithCallbackIdenticalToStandaloneAssertsRector.php @@ -12,6 +12,8 @@ use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Expr\Instanceof_; +use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; @@ -116,7 +118,7 @@ public function refactor(Node $node): MethodCall|null $innerSoleExpr = $this->matchInnerSoleExpr($argAndFunctionLike->getFunctionLike()); if ($innerSoleExpr instanceof BooleanAnd) { $joinedExprs = $this->extractJoinedExprs($innerSoleExpr); - } elseif ($innerSoleExpr instanceof Identical) { + } elseif ($innerSoleExpr instanceof Identical || $innerSoleExpr instanceof Instanceof_ || $innerSoleExpr instanceof Isset_) { $joinedExprs = [$innerSoleExpr]; } else { return null;