Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CombineWithWillReturnOnConsecutiveCallsVariable extends TestCase
{
public function test()
{
$title = new \stdClass();

$this->manager->expects($this->exactly(2))
->method('getFieldDefinitions')
->withConsecutive(['first'], ['second'])
->willReturnOnConsecutiveCalls(['title' => $title], []);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CombineWithWillReturnOnConsecutiveCallsVariable extends TestCase
{
public function test()
{
$title = new \stdClass();
$matcher = $this->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 [];
}
});
}
}

?>
7 changes: 6 additions & 1 deletion rules/PHPUnit100/NodeFactory/WillReturnCallbackFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ private function refactorToWillReturnCallback(
$withConsecutiveMethodCall,
$returnStmt,
$referenceVariable,
$willReturnOnConsecutiveMethodCall,
);

$withConsecutiveMethodCall->name = new Identifier(ConsecutiveMethodName::WILL_RETURN_CALLBACK);
Expand Down
11 changes: 9 additions & 2 deletions src/NodeFactory/UsedVariablesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading