Skip to content

Commit bd462a6

Browse files
authored
Use more readable ifs on will return in WithConsecutiveRector (#380)
1 parent 58ed580 commit bd462a6

File tree

5 files changed

+79
-41
lines changed

5 files changed

+79
-41
lines changed

rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_on_consecutive_without_expects.php.inc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ final class CombineWithOnConsecutiveWithoutExcepts extends TestCase
3131
{
3232
$matcher = $this->exactly(2);
3333
$this->userServiceMock->expects($matcher)->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
34-
match ($matcher->numberOfInvocations()) {
35-
1 => self::assertEquals([1, 2], $parameters),
36-
2 => self::assertEquals([3, 4], $parameters),
37-
};
38-
return match ($matcher->numberOfInvocations()) {
39-
1 => 5,
40-
2 => 6,
41-
};
34+
if ($matcher->numberOfInvocations() === 1) {
35+
$this->assertSame([1, 2], $parameters);
36+
return 5;
37+
}
38+
if ($matcher->numberOfInvocations() === 2) {
39+
$this->assertSame([3, 4], $parameters);
40+
return 6;
41+
}
4242
});
4343
}
4444
}

rules-tests/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector/Fixture/combine_with_will_return_on_consecutive_calls.php.inc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ final class CombineWithWillReturnOnConsecutiveCalls extends TestCase
3333
$matcher = self::exactly(2);
3434
$this->userServiceMock->expects($matcher)
3535
->method('prepare')->willReturnCallback(function (...$parameters) use ($matcher) {
36-
match ($matcher->numberOfInvocations()) {
37-
1 => self::assertEquals([1, 2], $parameters),
38-
2 => self::assertEquals([3, 4], $parameters),
39-
};
40-
return match ($matcher->numberOfInvocations()) {
41-
1 => 5,
42-
2 => 6,
43-
};
36+
if ($matcher->numberOfInvocations() === 1) {
37+
$this->assertSame([1, 2], $parameters);
38+
return 5;
39+
}
40+
if ($matcher->numberOfInvocations() === 2) {
41+
$this->assertSame([3, 4], $parameters);
42+
return 6;
43+
}
4444
});
4545
}
4646
}

rules/PHPUnit100/Rector/StmtsAwareInterface/WithConsecutiveRector.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
use PhpParser\Node\Expr\ArrayDimFetch;
1111
use PhpParser\Node\Expr\Assign;
1212
use PhpParser\Node\Expr\Closure;
13-
use PhpParser\Node\Expr\Match_;
1413
use PhpParser\Node\Expr\MethodCall;
1514
use PhpParser\Node\Expr\StaticCall;
1615
use PhpParser\Node\Expr\Variable;
1716
use PhpParser\Node\Identifier;
18-
use PhpParser\Node\MatchArm;
1917
use PhpParser\Node\Param;
2018
use PhpParser\Node\Scalar\LNumber;
2119
use PhpParser\Node\Stmt;
@@ -27,7 +25,7 @@
2725
use Rector\PhpParser\Node\BetterNodeFinder;
2826
use Rector\PHPUnit\Enum\ConsecutiveVariable;
2927
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
30-
use Rector\PHPUnit\NodeFactory\MatcherInvocationCountMethodCallNodeFactory;
28+
use Rector\PHPUnit\NodeFactory\ConsecutiveIfsFactory;
3129
use Rector\PHPUnit\NodeFactory\WithConsecutiveMatchFactory;
3230
use Rector\Rector\AbstractRector;
3331
use Rector\ValueObject\PhpVersion;
@@ -49,7 +47,7 @@ public function __construct(
4947
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
5048
private readonly BetterNodeFinder $betterNodeFinder,
5149
private readonly WithConsecutiveMatchFactory $withConsecutiveMatchFactory,
52-
private readonly MatcherInvocationCountMethodCallNodeFactory $matcherInvocationCountMethodCallNodeFactory,
50+
private readonly ConsecutiveIfsFactory $consecutiveIfsFactory,
5351
) {
5452
}
5553

@@ -142,9 +140,15 @@ public function refactor(Node $node)
142140
$returnStmts[] = $this->createWillReturnArgument($willReturnArgument);
143141
}
144142

143+
$areIfsPreferred = false;
144+
145145
$willReturnOnConsecutiveCallsArgument = $this->findMethodCall($node, 'willReturnOnConsecutiveCalls');
146146
if ($willReturnOnConsecutiveCallsArgument instanceof MethodCall) {
147-
$returnStmts[] = $this->createReturnMatch($willReturnOnConsecutiveCallsArgument);
147+
$returnStmts = $this->consecutiveIfsFactory->createCombinedIfs(
148+
$withConsecutiveMethodCall,
149+
$willReturnOnConsecutiveCallsArgument
150+
);
151+
$areIfsPreferred = true;
148152
}
149153

150154
$willThrowException = $this->findMethodCall($node, 'willThrowException');
@@ -195,6 +199,7 @@ public function refactor(Node $node)
195199
$referenceVariable,
196200
$expectsCall,
197201
$node,
202+
$areIfsPreferred
198203
);
199204
}
200205

@@ -304,11 +309,13 @@ private function refactorToWillReturnCallback(
304309
Expr|Variable|null $referenceVariable,
305310
StaticCall|MethodCall $expectsCall,
306311
Expression $expression,
312+
bool $areIfsPreferred
307313
): array {
308314
$closure = $this->withConsecutiveMatchFactory->createClosure(
309315
$withConsecutiveMethodCall,
310316
$returnStmts,
311317
$referenceVariable,
318+
$areIfsPreferred
312319
);
313320

314321
$withConsecutiveMethodCall->name = new Identifier('willReturnCallback');
@@ -424,16 +431,4 @@ private function createWillReturnArgument(MethodCall $willReturnArgumentMethodCa
424431

425432
return new Return_(new ArrayDimFetch($parametersVariable, $firstArgs->value));
426433
}
427-
428-
private function createReturnMatch(MethodCall $willReturnOnConsecutiveCallsMethodCall): Return_
429-
{
430-
$numberOfInvocationsMethodCall = $this->matcherInvocationCountMethodCallNodeFactory->create();
431-
432-
$matchArms = [];
433-
foreach ($willReturnOnConsecutiveCallsMethodCall->getArgs() as $key => $arg) {
434-
$matchArms[] = new MatchArm([new LNumber($key + 1)], $arg->value);
435-
}
436-
437-
return new Return_(new Match_($numberOfInvocationsMethodCall, $matchArms));
438-
}
439434
}

src/NodeFactory/ConsecutiveIfsFactory.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PhpParser\Node\Scalar\LNumber;
1818
use PhpParser\Node\Stmt\Expression;
1919
use PhpParser\Node\Stmt\If_;
20+
use PhpParser\Node\Stmt\Return_;
2021
use Rector\Exception\NotImplementedYetException;
2122
use Rector\Exception\ShouldNotHappenException;
2223
use Rector\NodeNameResolver\NodeNameResolver;
@@ -30,6 +31,40 @@ public function __construct(
3031
) {
3132
}
3233

34+
/**
35+
* @return If_[]
36+
*/
37+
public function createCombinedIfs(
38+
MethodCall $withConsecutiveMethodCall,
39+
MethodCall $willReturnOnConsecutiveCallsArgument
40+
): array {
41+
$ifs = [];
42+
$matcherMethodCall = $this->matcherInvocationCountMethodCallNodeFactory->create();
43+
44+
$willReturnArgs = $willReturnOnConsecutiveCallsArgument->getArgs();
45+
46+
foreach ($withConsecutiveMethodCall->getArgs() as $key => $withConsecutiveArg) {
47+
if (! $withConsecutiveArg->value instanceof Array_) {
48+
throw new ShouldNotHappenException();
49+
}
50+
51+
$ifStmts = [];
52+
$args = [new Arg($withConsecutiveArg->value), new Arg(new Variable('parameters'))];
53+
$ifStmts[] = new Expression(new MethodCall(new Variable('this'), 'assertSame', $args));
54+
55+
$willReturnArg = $willReturnArgs[$key] ?? null;
56+
if ($willReturnArg instanceof Arg) {
57+
$ifStmts[] = new Return_($willReturnArg->value);
58+
}
59+
60+
$ifs[] = new If_(new Identical($matcherMethodCall, new LNumber($key + 1)), [
61+
'stmts' => $ifStmts,
62+
]);
63+
}
64+
65+
return $ifs;
66+
}
67+
3368
/**
3469
* @return If_[]
3570
*/
@@ -52,7 +87,9 @@ public function createIfs(MethodCall $withConsecutiveMethodCall): array
5287
}
5388

5489
if (! $assertArrayItem->value instanceof MethodCall) {
55-
throw new NotImplementedYetException();
90+
$args = [new Arg($assertArrayItem), new Arg(new Variable('parameters'))];
91+
$ifStmts[] = new Expression(new MethodCall(new Variable('this'), 'assertSame', $args));
92+
continue;
5693
}
5794

5895
$assertMethodCall = $assertArrayItem->value;

src/NodeFactory/WithConsecutiveMatchFactory.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpParser\Node\Scalar\LNumber;
2222
use PhpParser\Node\Stmt;
2323
use PhpParser\Node\Stmt\Expression;
24+
use PhpParser\Node\Stmt\If_;
2425
use PhpParser\NodeFinder;
2526
use Rector\NodeNameResolver\NodeNameResolver;
2627
use Rector\PHPUnit\Enum\ConsecutiveVariable;
@@ -43,17 +44,22 @@ public function __construct(
4344
public function createClosure(
4445
MethodCall $withConsecutiveMethodCall,
4546
array $returnStmts,
46-
Variable|Expr|null $referenceVariable
47+
Variable|Expr|null $referenceVariable,
48+
bool $areIfsPreferred
4749
): Closure {
4850
$matcherVariable = new Variable(ConsecutiveVariable::MATCHER);
4951
$usedVariables = $this->usedVariablesResolver->resolveUsedVariables($withConsecutiveMethodCall, $returnStmts);
5052

51-
$matchOrIfs = $this->createParametersMatch($withConsecutiveMethodCall);
52-
53-
if (is_array($matchOrIfs)) {
54-
$closureStmts = array_merge($matchOrIfs, $returnStmts);
53+
if ($areIfsPreferred) {
54+
$closureStmts = $returnStmts;
5555
} else {
56-
$closureStmts = [new Expression($matchOrIfs), ...$returnStmts];
56+
$matchOrIfs = $this->createParametersMatch($withConsecutiveMethodCall);
57+
58+
if (is_array($matchOrIfs)) {
59+
$closureStmts = array_merge($matchOrIfs, $returnStmts);
60+
} else {
61+
$closureStmts = [new Expression($matchOrIfs), ...$returnStmts];
62+
}
5763
}
5864

5965
$parametersParam = new Param(new Variable(ConsecutiveVariable::PARAMETERS));
@@ -68,7 +74,7 @@ public function createClosure(
6874
}
6975

7076
/**
71-
* @return Match_|MethodCall|Stmt\If_[]
77+
* @return Match_|MethodCall|If_[]
7278
*/
7379
public function createParametersMatch(MethodCall $withConsecutiveMethodCall): Match_|MethodCall|array
7480
{

0 commit comments

Comments
 (0)