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,44 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\Fixture;

use PHPUnit\Framework\TestCase;

final class ReturnArrayWithVariableUse extends TestCase
{
public function test()
{
$initialStatements = [1, 2];

$this->createMock('SomeClass')
->method('someMethod')
->with($this->callback(function ($arg): bool {
return true;
}))
->willReturn([$initialStatements, []]);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\MergeWithCallableAndWillReturnRector\Fixture;

use PHPUnit\Framework\TestCase;

final class ReturnArrayWithVariableUse extends TestCase
{
public function test()
{
$initialStatements = [1, 2];

$this->createMock('SomeClass')
->method('someMethod')
->willReturnCallback(function ($arg) use ($initialStatements): array {
return [$initialStatements, []];
});
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Return_;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
Expand All @@ -29,6 +31,7 @@ public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ValueResolver $valueResolver,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -143,8 +146,8 @@ public function refactor(Node $node): MethodCall|null
$parentCaller->name = new Identifier('willReturnCallback');
$parentCaller->args = [new Arg($innerClosure)];

if ($returnedExpr instanceof Variable) {
$innerClosure->uses[] = new ClosureUse($returnedExpr);
foreach ($this->resolveExternalUses($innerClosure, $returnedExpr) as $closureUse) {
$innerClosure->uses[] = $closureUse;
}

$returnedExprType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnedExpr);
Expand All @@ -171,6 +174,55 @@ private function matchFirstArgCallbackMethodCall(MethodCall $withMethodCall): ?M
return $firstArgValue;
}

/**
* @return ClosureUse[]
*/
private function resolveExternalUses(Closure $innerClosure, Expr $returnedExpr): array
{
$paramNames = [];
foreach ($innerClosure->getParams() as $param) {
$paramNames[] = $this->getName($param);
}

$existingUseNames = [];
foreach ($innerClosure->uses as $existingUse) {
$existingUseNames[] = $this->getName($existingUse->var);
}

$closureUses = [];
$seenNames = [];

/** @var Variable[] $variables */
$variables = $this->betterNodeFinder->findInstancesOf($returnedExpr, [Variable::class]);
foreach ($variables as $variable) {
$variableName = $this->getName($variable);
if (! is_string($variableName)) {
continue;
}

if ($variableName === 'this') {
continue;
}

if (in_array($variableName, $paramNames, true)) {
continue;
}

if (in_array($variableName, $existingUseNames, true)) {
continue;
}

if (in_array($variableName, $seenNames, true)) {
continue;
}

$seenNames[] = $variableName;
$closureUses[] = new ClosureUse(new Variable($variableName));
}

return $closureUses;
}

private function isLastStmtReturnTrue(Closure $closure): bool
{
$lastStmt = $closure->stmts[count($closure->stmts) - 1];
Expand Down
Loading