Skip to content

Commit 63a8471

Browse files
authored
[CodeQuality] Reuse local stub variable when property used 2+ times in single method (#702)
1 parent 41cf82b commit 63a8471

3 files changed

Lines changed: 145 additions & 11 deletions

File tree

‎rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/inline_used_in_calls.php.inc‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ final class InlineUsedInCalls extends TestCase
4545

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

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

5354
private function runThis($object)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\MockObject\Stub;
7+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;
8+
9+
final class ReuseVariableOnMultipleUses extends TestCase
10+
{
11+
private Stub $formatter;
12+
13+
protected function setUp(): void
14+
{
15+
$this->formatter = $this->createStub(NotRelevantClass::class);
16+
}
17+
18+
public function testAnother()
19+
{
20+
$handler = new NotRelevantClass($this->formatter);
21+
22+
$this->assertSame(spl_object_id($this->formatter), spl_object_id($handler));
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;
31+
32+
use PHPUnit\Framework\TestCase;
33+
use PHPUnit\Framework\MockObject\Stub;
34+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;
35+
36+
final class ReuseVariableOnMultipleUses extends TestCase
37+
{
38+
protected function setUp(): void
39+
{
40+
}
41+
42+
public function testAnother()
43+
{
44+
$formatterStub = $this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class);
45+
$handler = new NotRelevantClass($formatterStub);
46+
47+
$this->assertSame(spl_object_id($formatterStub), spl_object_id($handler));
48+
}
49+
}
50+
51+
?>

‎rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php‎

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\Node\Stmt\Expression;
2020
use PhpParser\Node\Stmt\Property;
2121
use PHPStan\Type\ObjectType;
22+
use Rector\PhpParser\Node\BetterNodeFinder;
2223
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
2324
use Rector\PHPUnit\CodeQuality\NodeAnalyser\StubPropertyResolver;
2425
use Rector\PHPUnit\CodeQuality\NodeFinder\PropertyFetchUsageFinder;
@@ -39,6 +40,7 @@ public function __construct(
3940
private readonly PropertyFetchFinder $propertyFetchFinder,
4041
private readonly PropertyFetchUsageFinder $propertyFetchUsageFinder,
4142
private readonly StubPropertyResolver $stubPropertyResolver,
43+
private readonly BetterNodeFinder $betterNodeFinder,
4244
) {
4345
}
4446

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

150152
// 3. replace property fetch calls, with createStub()
151153
$stubClassName = $propertyNamesToStubClasses[$propertyName];
152-
$this->traverseNodesWithCallable($node->getMethods(), function (Node $node) use (
154+
foreach ($node->getMethods() as $classMethod) {
155+
$this->refactorClassMethod($classMethod, $propertyName, $stubClassName);
156+
}
157+
}
158+
159+
if ($hasChanged === false) {
160+
return null;
161+
}
162+
163+
return $node;
164+
}
165+
166+
private function refactorClassMethod(ClassMethod $classMethod, string $propertyName, string $stubClassName): void
167+
{
168+
$propertyFetches = $this->betterNodeFinder->find(
169+
(array) $classMethod->stmts,
170+
fn (Node $node): bool => $node instanceof PropertyFetch && $this->isName($node->name, $propertyName)
171+
);
172+
173+
if ($propertyFetches === []) {
174+
return;
175+
}
176+
177+
// single use → inline directly
178+
if (count($propertyFetches) === 1) {
179+
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
153180
$propertyName,
154181
$stubClassName
155182
): ?MethodCall {
@@ -161,19 +188,74 @@ public function refactor(Node $node): ?Node
161188
return null;
162189
}
163190

164-
$classConstFetch = new ClassConstFetch(new FullyQualified($stubClassName), 'class');
165-
166-
return new MethodCall(new Variable('this'), new Identifier('createStub'), [
167-
new Arg($classConstFetch),
168-
]);
191+
return $this->createCreateStubMethodCall($stubClassName);
169192
});
193+
194+
return;
170195
}
171196

172-
if ($hasChanged === false) {
173-
return null;
197+
// multiple uses → assign to a local variable above first use and reuse it
198+
$variableName = $this->resolveVariableName($propertyName);
199+
200+
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (
201+
$propertyName,
202+
$variableName
203+
): ?Variable {
204+
if (! $node instanceof PropertyFetch) {
205+
return null;
206+
}
207+
208+
if (! $this->isName($node->name, $propertyName)) {
209+
return null;
210+
}
211+
212+
return new Variable($variableName);
213+
});
214+
215+
$assignExpression = new Expression(
216+
new Assign(new Variable($variableName), $this->createCreateStubMethodCall($stubClassName))
217+
);
218+
219+
$this->insertBeforeFirstVariableUse($classMethod, $variableName, $assignExpression);
220+
}
221+
222+
private function insertBeforeFirstVariableUse(
223+
ClassMethod $classMethod,
224+
string $variableName,
225+
Expression $assignExpression
226+
): void {
227+
$stmts = (array) $classMethod->stmts;
228+
229+
foreach ($stmts as $key => $stmt) {
230+
$firstVariable = $this->betterNodeFinder->findFirst(
231+
$stmt,
232+
fn (Node $node): bool => $node instanceof Variable && $this->isName($node, $variableName)
233+
);
234+
235+
if (! $firstVariable instanceof Variable) {
236+
continue;
237+
}
238+
239+
array_splice($stmts, $key, 0, [$assignExpression]);
240+
$classMethod->stmts = $stmts;
241+
return;
174242
}
243+
}
175244

176-
return $node;
245+
private function createCreateStubMethodCall(string $stubClassName): MethodCall
246+
{
247+
$classConstFetch = new ClassConstFetch(new FullyQualified($stubClassName), 'class');
248+
249+
return new MethodCall(new Variable('this'), new Identifier('createStub'), [new Arg($classConstFetch)]);
250+
}
251+
252+
private function resolveVariableName(string $propertyName): string
253+
{
254+
if (str_ends_with(strtolower($propertyName), 'stub')) {
255+
return $propertyName;
256+
}
257+
258+
return $propertyName . 'Stub';
177259
}
178260

179261
private function removeSetupPropertyFetchByPropertyName(ClassMethod $setUpClassMethod, string $propertyName): void

0 commit comments

Comments
 (0)