Skip to content

Commit 3fa71d2

Browse files
samsonasikTomasVotruba
authored andcommitted
Fix double assign
* [PHPUnit 12] Skip first class callable on CreateStubOverCreateMockArgRector * fix
1 parent 7a318b2 commit 3fa71d2

File tree

6 files changed

+94
-14
lines changed

6 files changed

+94
-14
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass;
7+
8+
final class HopAroundInCall extends TestCase
9+
{
10+
public function test()
11+
{
12+
$someMock = $this->createMock(AnotherClass::class);
13+
14+
$anotherClass = new AnotherClass(1, 2, 3, $someMock);
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass;
26+
27+
final class HopAroundInCall extends TestCase
28+
{
29+
public function test()
30+
{
31+
$anotherClass = new AnotherClass(1, 2, 3, $this->createMock(AnotherClass::class));
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MultipleUsesAsNoConnection extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->createMock(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BareCreateMockAssignToDirectUseRector\Source\AnotherClass::class);
12+
13+
$this->useMock($someMock);
14+
$this->useMockAgain($someMock);
15+
}
16+
17+
private function useMock($someMock)
18+
{
19+
}
20+
21+
private function useMockAgain($someMock)
22+
{
23+
}
24+
}
25+
26+
?>

rules-tests/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector/Source/AnotherClass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
final class AnotherClass
88
{
9+
public function __construct(...$various)
10+
{
11+
12+
}
913
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Utils\PHPStan\Tests\Rule\PHPUnit\StubOverMockArgRule\Source\AnyClass;
7+
8+
class SkipFirstClassCallable extends TestCase
9+
{
10+
public function testThat()
11+
{
12+
AnyClass::staticRun(...);
13+
}
14+
}

rules/CodeQuality/Rector/ClassMethod/BareCreateMockAssignToDirectUseRector.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,20 @@ public function refactor(Node $node): ?Node
128128
$assign = $stmt->expr;
129129

130130
$instanceArg = $this->assignedMocksCollector->matchCreateMockArgAssignedToVariable($assign);
131-
if (! $instanceArg instanceof Arg) {
131+
if ($instanceArg instanceof Arg && $assign->var instanceof Variable && $this->isName(
132+
$assign->var,
133+
$variableName
134+
)) {
135+
// 1. remove assign
136+
unset($node->stmts[$key]);
137+
$hasChanged = true;
138+
$variablesToMethodCalls[$variableName] = $assign->expr;
132139
continue;
133140
}
141+
}
134142

135-
if (! $assign->var instanceof Variable) {
136-
continue;
137-
}
138-
139-
if (! $this->isName($assign->var, $variableName)) {
140-
continue;
141-
}
142-
143-
// 1. remove assign
144-
unset($node->stmts[$key]);
145-
$hasChanged = true;
146-
147-
$variablesToMethodCalls[$variableName] = $assign->expr;
143+
// nothing to processy yet
144+
if ($variablesToMethodCalls === []) {
148145
continue;
149146
}
150147

rules/PHPUnit120/Rector/CallLike/CreateStubOverCreateMockArgRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null
105105

106106
$hasChanges = false;
107107

108+
if ($node->isFirstClassCallable()) {
109+
return null;
110+
}
111+
108112
foreach ($node->getArgs() as $arg) {
109113
if (! $arg->value instanceof MethodCall) {
110114
continue;

0 commit comments

Comments
 (0)