Skip to content

Commit 8805353

Browse files
committed
covert topmost call
1 parent 8590dd3 commit 8805353

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
6+
7+
final class HandleMultiples extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testSomething(): void
10+
{
11+
$this->createMock('someClass');
12+
$this->createMock('someClass');
13+
$this->createMock('someClass');
14+
$this->createMock('someClass');
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
declare(strict_types=1);
23+
24+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
25+
26+
final class HandleMultiples extends \PHPUnit\Framework\TestCase
27+
{
28+
public function testSomething(): void
29+
{
30+
}
31+
}
32+
33+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
6+
7+
final class HandleNestedCall extends \PHPUnit\Framework\TestCase
8+
{
9+
public function testSomething(): void
10+
{
11+
$this->createMock('someClass')
12+
->method('some')
13+
->willReturn(100);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\Fixture;
24+
25+
final class HandleNestedCall extends \PHPUnit\Framework\TestCase
26+
{
27+
public function testSomething(): void
28+
{
29+
}
30+
}
31+
32+
?>

rules/CodeQuality/Rector/ClassMethod/RemoveStandaloneCreateMockRector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public function refactor(Node $node): ?Node
8484
continue;
8585
}
8686

87-
$methodCall = $stmt->expr;
88-
if (! $this->isName($methodCall->name, 'createMock')) {
87+
$topmostCall = $this->resolveTopmostCall($stmt->expr);
88+
if (! $this->isName($topmostCall->name, 'createMock')) {
8989
continue;
9090
}
9191

@@ -99,4 +99,14 @@ public function refactor(Node $node): ?Node
9999

100100
return null;
101101
}
102+
103+
private function resolveTopmostCall(MethodCall $methodCall): MethodCall
104+
{
105+
$currentMethodCall = $methodCall;
106+
while ($currentMethodCall->var instanceof MethodCall) {
107+
$currentMethodCall = $currentMethodCall->var;
108+
}
109+
110+
return $currentMethodCall;
111+
}
102112
}

0 commit comments

Comments
 (0)