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
Expand Up @@ -45,9 +45,10 @@ final class InlineUsedInCalls extends TestCase

public function testAnother()
{
$anotherObject = new NotRelevantClass($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
$someStub = $this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class);
$anotherObject = new NotRelevantClass($someStub);

$this->runThis($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
$this->runThis($someStub);
}

private function runThis($object)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;

final class ReuseVariableOnMultipleUses extends TestCase
{
private Stub $formatter;

protected function setUp(): void
{
$this->formatter = $this->createStub(NotRelevantClass::class);
}

public function testAnother()
{
$handler = new NotRelevantClass($this->formatter);

$this->assertSame(spl_object_id($this->formatter), spl_object_id($handler));
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\Stub;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;

final class ReuseVariableOnMultipleUses extends TestCase
{
protected function setUp(): void
{
}

public function testAnother()
{
$formatterStub = $this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class);
$handler = new NotRelevantClass($formatterStub);

$this->assertSame(spl_object_id($formatterStub), spl_object_id($handler));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\StubPropertyResolver;
use Rector\PHPUnit\CodeQuality\NodeFinder\PropertyFetchUsageFinder;
Expand All @@ -39,6 +40,7 @@ public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly PropertyFetchUsageFinder $propertyFetchUsageFinder,
private readonly StubPropertyResolver $stubPropertyResolver,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -149,7 +151,32 @@ public function refactor(Node $node): ?Node

// 3. replace property fetch calls, with createStub()
$stubClassName = $propertyNamesToStubClasses[$propertyName];
$this->traverseNodesWithCallable($node->getMethods(), function (Node $node) use (
foreach ($node->getMethods() as $classMethod) {
$this->refactorClassMethod($classMethod, $propertyName, $stubClassName);
}
}

if ($hasChanged === false) {
return null;
}

return $node;
}

private function refactorClassMethod(ClassMethod $classMethod, string $propertyName, string $stubClassName): void
{
$propertyFetches = $this->betterNodeFinder->find(
(array) $classMethod->stmts,
fn (Node $node): bool => $node instanceof PropertyFetch && $this->isName($node->name, $propertyName)
);

if ($propertyFetches === []) {
return;
}

// single use → inline directly
if (count($propertyFetches) === 1) {
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
$propertyName,
$stubClassName
): ?MethodCall {
Expand All @@ -161,19 +188,74 @@ public function refactor(Node $node): ?Node
return null;
}

$classConstFetch = new ClassConstFetch(new FullyQualified($stubClassName), 'class');

return new MethodCall(new Variable('this'), new Identifier('createStub'), [
new Arg($classConstFetch),
]);
return $this->createCreateStubMethodCall($stubClassName);
});

return;
}

if ($hasChanged === false) {
return null;
// multiple uses → assign to a local variable above first use and reuse it
$variableName = $this->resolveVariableName($propertyName);

$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
$propertyName,
$variableName
): ?Variable {
if (! $node instanceof PropertyFetch) {
return null;
}

if (! $this->isName($node->name, $propertyName)) {
return null;
}

return new Variable($variableName);
});

$assignExpression = new Expression(
new Assign(new Variable($variableName), $this->createCreateStubMethodCall($stubClassName))
);

$this->insertBeforeFirstVariableUse($classMethod, $variableName, $assignExpression);
}

private function insertBeforeFirstVariableUse(
ClassMethod $classMethod,
string $variableName,
Expression $assignExpression
): void {
$stmts = (array) $classMethod->stmts;

foreach ($stmts as $key => $stmt) {
$firstVariable = $this->betterNodeFinder->findFirst(
$stmt,
fn (Node $node): bool => $node instanceof Variable && $this->isName($node, $variableName)
);

if (! $firstVariable instanceof Variable) {
continue;
}

array_splice($stmts, $key, 0, [$assignExpression]);
$classMethod->stmts = $stmts;
return;
}
}

return $node;
private function createCreateStubMethodCall(string $stubClassName): MethodCall
{
$classConstFetch = new ClassConstFetch(new FullyQualified($stubClassName), 'class');

return new MethodCall(new Variable('this'), new Identifier('createStub'), [new Arg($classConstFetch)]);
}

private function resolveVariableName(string $propertyName): string
{
if (str_ends_with(strtolower($propertyName), 'stub')) {
return $propertyName;
}

return $propertyName . 'Stub';
}

private function removeSetupPropertyFetchByPropertyName(ClassMethod $setUpClassMethod, string $propertyName): void
Expand Down
Loading