Skip to content

Commit 41cf82b

Browse files
authored
[CodeQuality] Skip DecorateWillReturnMapWithExpectsMockRector on non-test methods (#701)
1 parent 3f62270 commit 41cf82b

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Fixture;
6+
7+
final class SkipPrivateHelper extends \PHPUnit\Framework\TestCase
8+
{
9+
private function createSomeMock()
10+
{
11+
$someMock = $this->createMock(\stdClass::class);
12+
$someMock->method('some')->willReturnMap([1, 2]);
13+
14+
return $someMock;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\DecorateWillReturnMapWithExpectsMockRector\Fixture;
6+
7+
final class SkipProtectedNonTestMethod extends \PHPUnit\Framework\TestCase
8+
{
9+
protected function prepareSomeMock()
10+
{
11+
$someMock = $this->createMock(\stdClass::class);
12+
$someMock->method('some')->willReturnMap([1, 2]);
13+
}
14+
}

‎rules/CodeQuality/Rector/Expression/DecorateWillReturnMapWithExpectsMockRector.php‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use PhpParser\Node\Expr\Variable;
1212
use PhpParser\Node\Identifier;
1313
use PhpParser\Node\Scalar\Int_;
14+
use PhpParser\Node\Stmt\ClassMethod;
1415
use PhpParser\Node\Stmt\Expression;
1516
use PHPStan\Type\ObjectType;
16-
use Rector\PHPStan\ScopeFetcher;
17+
use Rector\PhpParser\Node\BetterNodeFinder;
1718
use Rector\PHPUnit\Enum\PHPUnitClassName;
19+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1820
use Rector\Rector\AbstractRector;
1921
use Rector\ValueObject\MethodName;
2022
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -25,6 +27,12 @@
2527
*/
2628
final class DecorateWillReturnMapWithExpectsMockRector extends AbstractRector
2729
{
30+
public function __construct(
31+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
32+
private readonly BetterNodeFinder $betterNodeFinder,
33+
) {
34+
}
35+
2836
public function getRuleDefinition(): RuleDefinition
2937
{
3038
return new RuleDefinition('Decorate willReturnMap() calls with expects on the mock object if missing', [
@@ -78,54 +86,76 @@ protected function setUp(): void
7886

7987
public function getNodeTypes(): array
8088
{
81-
return [Expression::class];
89+
return [ClassMethod::class];
8290
}
8391

8492
/**
85-
* @param Expression $node
86-
* @return Expression|null
93+
* @param ClassMethod $node
94+
* @return ClassMethod|null
8795
*/
8896
public function refactor(Node $node)
8997
{
90-
if (! $node->expr instanceof MethodCall) {
98+
// allowed as can be flexible
99+
if ($this->isName($node, MethodName::SET_UP)) {
91100
return null;
92101
}
93102

94-
$methodCall = $node->expr;
95-
if (! $this->isName($methodCall->name, 'willReturnMap')) {
103+
// only decorate inside test methods, skip helpers and other non-test methods
104+
if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) {
96105
return null;
97106
}
98107

99-
$scope = ScopeFetcher::fetch($node);
108+
$hasChanged = false;
100109

101-
// allowed as can be flexible
102-
if ($scope->getFunctionName() === MethodName::SET_UP) {
110+
/** @var Expression[] $expressions */
111+
$expressions = $this->betterNodeFinder->findInstancesOf($node, [Expression::class]);
112+
foreach ($expressions as $expression) {
113+
if ($this->refactorExpression($expression)) {
114+
$hasChanged = true;
115+
}
116+
}
117+
118+
if (! $hasChanged) {
103119
return null;
104120
}
105121

122+
return $node;
123+
}
124+
125+
private function refactorExpression(Expression $expression): bool
126+
{
127+
if (! $expression->expr instanceof MethodCall) {
128+
return false;
129+
}
130+
131+
$methodCall = $expression->expr;
132+
if (! $this->isName($methodCall->name, 'willReturnMap')) {
133+
return false;
134+
}
135+
106136
$topmostCall = $this->resolveTopmostCall($methodCall);
107137

108138
// already covered
109139
if ($this->isName($topmostCall->name, 'expects')) {
110-
return null;
140+
return false;
111141
}
112142

113143
if (! $this->isObjectType($topmostCall->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
114-
return null;
144+
return false;
115145
}
116146

117147
if ($methodCall->isFirstClassCallable()) {
118-
return null;
148+
return false;
119149
}
120150

121151
// count values in will map arg
122152
$willReturnMapArg = $methodCall->getArgs()[0] ?? null;
123153
if (! $willReturnMapArg instanceof Arg) {
124-
return null;
154+
return false;
125155
}
126156

127157
if (! $willReturnMapArg->value instanceof Array_) {
128-
return null;
158+
return false;
129159
}
130160

131161
$array = $willReturnMapArg->value;
@@ -141,7 +171,7 @@ public function refactor(Node $node)
141171
))]
142172
);
143173

144-
return $node;
174+
return true;
145175
}
146176

147177
private function resolveTopmostCall(MethodCall $methodCall): MethodCall

0 commit comments

Comments
 (0)