From 779d31d99647a83c1a6a1d1ca3509450dc59713c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 29 Jan 2026 17:29:09 +0100 Subject: [PATCH] skip setup method --- .../Fixture/skip_already_filled.php.inc | 15 +++++++++++++++ .../Fixture/skip_setup_as_can_be_any.php.inc | 15 +++++++++++++++ .../MethodCall/ExplicitMockExpectsCallRector.php | 7 +++++++ 3 files changed, 37 insertions(+) create mode 100644 rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_already_filled.php.inc create mode 100644 rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_setup_as_can_be_any.php.inc diff --git a/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_already_filled.php.inc b/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_already_filled.php.inc new file mode 100644 index 00000000..4f0cb435 --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_already_filled.php.inc @@ -0,0 +1,15 @@ +createMock(\stdClass::class); + + $someClass->expects($this->once())->method('some'); + } +} diff --git a/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_setup_as_can_be_any.php.inc b/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_setup_as_can_be_any.php.inc new file mode 100644 index 00000000..c2aee4ef --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector/Fixture/skip_setup_as_can_be_any.php.inc @@ -0,0 +1,15 @@ +createMock(\stdClass::class); + + $someClass->method('some'); + } +} diff --git a/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php b/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php index 6133cf80..7ae04e76 100644 --- a/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php +++ b/rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php @@ -10,9 +10,11 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; use PHPStan\Type\ObjectType; +use Rector\PHPStan\ScopeFetcher; use Rector\PHPUnit\Enum\PHPUnitClassName; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; +use Rector\ValueObject\MethodName; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -80,6 +82,11 @@ public function refactor(Node $node): Node|null return null; } + $scope = ScopeFetcher::fetch($node); + if ($scope->getFunctionName() === MethodName::SET_UP) { + return null; + } + if (! $node->var instanceof Variable && ! $node->var instanceof PropertyFetch) { return null; }