From b9d2c1b3519878c49ea4275e820a1250049eb1bb Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 9 Nov 2026 17:15:40 +0100 Subject: [PATCH] [PHPUnit100] Add willReturnOnConsecutiveCalls() variables to closure use() in WithConsecutiveRector --- ...turn_on_consecutive_calls_variable.php.inc | 49 +++++++++++++++++++ .../NodeFactory/WillReturnCallbackFactory.php | 7 ++- .../WithConsecutiveRector.php | 1 + src/NodeFactory/UsedVariablesResolver.php | 11 ++++- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_will_return_on_consecutive_calls_variable.php.inc diff --git a/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_will_return_on_consecutive_calls_variable.php.inc b/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_will_return_on_consecutive_calls_variable.php.inc new file mode 100644 index 00000000..ec136026 --- /dev/null +++ b/rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_will_return_on_consecutive_calls_variable.php.inc @@ -0,0 +1,49 @@ +manager->expects($this->exactly(2)) + ->method('getFieldDefinitions') + ->withConsecutive(['first'], ['second']) + ->willReturnOnConsecutiveCalls(['title' => $title], []); + } +} + +?> +----- +exactly(2); + + $this->manager->expects($matcher) + ->method('getFieldDefinitions')->willReturnCallback(function (...$parameters) use ($matcher, $title) { + if ($matcher->numberOfInvocations() === 1) { + $this->assertSame('first', $parameters[0]); + return ['title' => $title]; + } + if ($matcher->numberOfInvocations() === 2) { + $this->assertSame('second', $parameters[0]); + return []; + } + }); + } +} + +?> diff --git a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php index 321a21b6..74e6fe22 100644 --- a/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php +++ b/rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php @@ -36,9 +36,14 @@ public function createClosure( MethodCall $withConsecutiveMethodCall, ?Stmt $returnStmt, Variable|Expr|null $referenceVariable, + ?MethodCall $willReturnOnConsecutiveMethodCall = null, ): Closure { $matcherVariable = new Variable(ConsecutiveVariable::MATCHER); - $usedVariables = $this->usedVariablesResolver->resolveUsedVariables($withConsecutiveMethodCall, $returnStmt); + $usedVariables = $this->usedVariablesResolver->resolveUsedVariables( + $withConsecutiveMethodCall, + $returnStmt, + $willReturnOnConsecutiveMethodCall + ); $closureStmts = $this->createParametersMatch($withConsecutiveMethodCall); if ($returnStmt instanceof Stmt) { diff --git a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php index 29afa3b9..cdc3faf8 100644 --- a/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php +++ b/rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php @@ -242,6 +242,7 @@ private function refactorToWillReturnCallback( $withConsecutiveMethodCall, $returnStmt, $referenceVariable, + $willReturnOnConsecutiveMethodCall, ); $withConsecutiveMethodCall->name = new Identifier(ConsecutiveMethodName::WILL_RETURN_CALLBACK); diff --git a/src/NodeFactory/UsedVariablesResolver.php b/src/NodeFactory/UsedVariablesResolver.php index 2d292880..04e1c62e 100644 --- a/src/NodeFactory/UsedVariablesResolver.php +++ b/src/NodeFactory/UsedVariablesResolver.php @@ -23,10 +23,17 @@ public function __construct( /** * @return Variable[] */ - public function resolveUsedVariables(MethodCall $withConsecutiveMethodCall, ?Stmt $returnStmt): array - { + public function resolveUsedVariables( + MethodCall $withConsecutiveMethodCall, + ?Stmt $returnStmt, + ?MethodCall $willReturnOnConsecutiveMethodCall = null + ): array { $consecutiveArgs = $withConsecutiveMethodCall->getArgs(); + if ($willReturnOnConsecutiveMethodCall instanceof MethodCall) { + $consecutiveArgs = array_merge($consecutiveArgs, $willReturnOnConsecutiveMethodCall->getArgs()); + } + $stmtVariables = $returnStmt instanceof Stmt ? $this->resolveUniqueVariables([$returnStmt]) : []; return $this->resolveUniqueVariables(array_merge($consecutiveArgs, $stmtVariables));