Skip to content

Commit 49b15f6

Browse files
committed
add array item support to CreateStubOverCreateMockArgRector
1 parent 680912f commit 49b15f6

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

rules-tests/CodeQuality/Rector/CallLike/DirectInstanceOverMockArgRector/Fixture/handle_static_call.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
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\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class HandleArrayItem extends TestCase
8+
{
9+
public function testThat()
10+
{
11+
$items = [
12+
$this->createMock(\stdClass::class),
13+
];
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class HandleArrayItem extends TestCase
26+
{
27+
public function testThat()
28+
{
29+
$items = [
30+
$this->createStub(\stdClass::class),
31+
];
32+
}
33+
}
34+
35+
?>

rules/PHPUnit120/Rector/CallLike/CreateStubOverCreateMockArgRector.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\PHPUnit\PHPUnit120\Rector\CallLike;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\ArrayItem;
89
use PhpParser\Node\Expr\MethodCall;
910
use PhpParser\Node\Expr\New_;
1011
use PhpParser\Node\Expr\StaticCall;
@@ -25,7 +26,7 @@ final class CreateStubOverCreateMockArgRector extends AbstractRector
2526
public function getRuleDefinition(): RuleDefinition
2627
{
2728
return new RuleDefinition(
28-
'Use createStub() over createMock() when used as argument and does not add any mock requirements',
29+
'Use createStub() over createMock() when used as argument or array value and does not add any mock requirements',
2930
[
3031
new CodeSample(
3132
<<<'CODE_SAMPLE'
@@ -69,13 +70,13 @@ private function someMethod($someClass)
6970
*/
7071
public function getNodeTypes(): array
7172
{
72-
return [StaticCall::class, MethodCall::class, New_::class];
73+
return [StaticCall::class, MethodCall::class, New_::class, ArrayItem::class];
7374
}
7475

7576
/**
76-
* @param MethodCall|StaticCall|New_ $node
77+
* @param MethodCall|StaticCall|New_|ArrayItem $node
7778
*/
78-
public function refactor(Node $node): MethodCall|StaticCall|New_|null
79+
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|null
7980
{
8081
$scope = ScopeFetcher::fetch($node);
8182
if (! $scope->isInClass()) {
@@ -87,6 +88,21 @@ public function refactor(Node $node): MethodCall|StaticCall|New_|null
8788
return null;
8889
}
8990

91+
if ($node instanceof ArrayItem) {
92+
if (! $node->value instanceof MethodCall) {
93+
return null;
94+
}
95+
96+
$methodCall = $node->value;
97+
if (! $this->isName($methodCall->name, 'createMock')) {
98+
return null;
99+
}
100+
101+
$methodCall->name = new Identifier('createStub');
102+
103+
return $node;
104+
}
105+
90106
$hasChanges = false;
91107

92108
foreach ($node->getArgs() as $arg) {

0 commit comments

Comments
 (0)