-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConsecutiveMockExpectationRector.php
105 lines (91 loc) · 3.15 KB
/
ConsecutiveMockExpectationRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
use Rector\PhpSpecToPHPUnit\NodeAnalyzer\ConsecutiveMethodCallMatcher;
use Rector\PhpSpecToPHPUnit\NodeFactory\WillReturnMapMethodCallFactory;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PhpSpecToPHPUnit\Tests\Rector\ClassMethod\ConsecutiveMockExpectationRector\ConsecutiveMockExpectationRectorTest
*/
final class ConsecutiveMockExpectationRector extends AbstractRector
{
public function __construct(
private readonly ConsecutiveMethodCallMatcher $consecutiveMethodCallMatcher,
private readonly WillReturnMapMethodCallFactory $willReturnMapMethodCallFactory,
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->isPublic() || $node->stmts === null) {
return null;
}
// special case, @see https://johannespichler.com/writing-custom-phpspec-matchers/
if ($this->isNames($node, PhpSpecMethodName::RESERVED_CLASS_METHOD_NAMES)) {
return null;
}
$methodNamesConsecutiveMethodCalls = $this->consecutiveMethodCallMatcher->matchInClassMethod($node);
if ($methodNamesConsecutiveMethodCalls === []) {
return null;
}
foreach ($methodNamesConsecutiveMethodCalls as $methodNameConsecutiveMethodCall) {
$willReturnMapMethodCall = $this->willReturnMapMethodCallFactory->create($methodNameConsecutiveMethodCall);
// replace with single->willReturnMap()
array_splice(
$node->stmts,
$methodNameConsecutiveMethodCall->getFirstStmtKey(),
$methodNameConsecutiveMethodCall->getMethodCallCount(),
[new Expression($willReturnMapMethodCall)]
);
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Merge consecutive mock expectations to single ->willReturnMap() call', [
new CodeSample(
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should(MockedType $mockedType)
{
$mockedType->set('first_key')->shouldReturn(100);
$mockedType->set('second_key')->shouldReturn(200);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should(MockedType $mockedType)
{
$mockedType->expects($this->exactly(2))->method('set')
->willReturnMap([
['first_key', 100],
['second_key', 200],
]);
}
}
CODE_SAMPLE
),
]);
}
}