-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDuringMethodCallRector.php
119 lines (100 loc) · 3.52 KB
/
DuringMethodCallRector.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
use Rector\PhpSpecToPHPUnit\Naming\PhpSpecRenaming;
use Rector\PhpSpecToPHPUnit\NodeAnalyzer\DuringAndRelatedMethodCallMatcher;
use Rector\PhpSpecToPHPUnit\NodeFactory\ExpectExceptionMethodCallFactory;
use Rector\PhpSpecToPHPUnit\ValueObject\DuringAndRelatedMethodCall;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PhpSpecToPHPUnit\Tests\Rector\ClassMethod\ShouldThrowAndInstantiationOrderRector\ShouldThrowAndInstantiationOrderRectorTest
*/
final class DuringMethodCallRector extends AbstractRector
{
public function __construct(
private readonly DuringAndRelatedMethodCallMatcher $duringAndRelatedMethodCallMatcher,
private readonly ExpectExceptionMethodCallFactory $expectExceptionMethodCallFactory,
private readonly PhpSpecRenaming $phpSpecRenaming,
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$testedObject = $this->phpSpecRenaming->resolveTestedObject($node);
$hasChanged = false;
foreach ($node->getMethods() as $classMethod) {
if (! $classMethod->isPublic() || $classMethod->stmts === null) {
continue;
}
// special case, @see https://johannespichler.com/writing-custom-phpspec-matchers/
if ($this->isNames($node, PhpSpecMethodName::RESERVED_CLASS_METHOD_NAMES)) {
continue;
}
foreach ($classMethod->stmts as $key => $stmt) {
$duringAndRelatedMethodCall = $this->duringAndRelatedMethodCallMatcher->match(
$stmt,
PhpSpecMethodName::DURING
);
if (! $duringAndRelatedMethodCall instanceof DuringAndRelatedMethodCall) {
continue;
}
$newStmts = $this->expectExceptionMethodCallFactory->createExpectExceptionStmts(
$duringAndRelatedMethodCall
);
$newStmts[] = $this->expectExceptionMethodCallFactory->createMethodCallStmt(
$duringAndRelatedMethodCall,
$testedObject
);
array_splice($classMethod->stmts, $key, 1, $newStmts);
$hasChanged = true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Split shouldThrow() and during() method to expected exception and method call', [
new CodeSample(
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should()
{
$this->shouldThrow(ValidationException::class)->during('someMethod');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class DuringMethodSpec extends ObjectBehavior
{
public function is_should()
{
$this->expectException(ValidationException::class);
$this->someMethod();
}
}
CODE_SAMPLE
),
]);
}
}