-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShouldThrowAndInstantiationOrderRector.php
131 lines (110 loc) · 3.72 KB
/
ShouldThrowAndInstantiationOrderRector.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
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
use Rector\PhpSpecToPHPUnit\NodeAnalyzer\DuringAndRelatedMethodCallMatcher;
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 ShouldThrowAndInstantiationOrderRector extends AbstractRector
{
public function __construct(
private readonly DuringAndRelatedMethodCallMatcher $duringAndRelatedMethodCallMatcher,
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->isPublic()) {
return null;
}
// special case, @see https://johannespichler.com/writing-custom-phpspec-matchers/
if ($this->isNames($node, PhpSpecMethodName::RESERVED_CLASS_METHOD_NAMES)) {
return null;
}
$hasChanged = false;
foreach ((array) $node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
$duringAndRelatedMethodCall = $this->duringAndRelatedMethodCallMatcher->match(
$stmt,
PhpSpecMethodName::DURING_INSTANTIATION
);
if (! $duringAndRelatedMethodCall instanceof DuringAndRelatedMethodCall) {
continue;
}
$previousStmt = $node->stmts[$key - 1] ?? null;
if (! $previousStmt instanceof Stmt) {
continue;
}
// move previous expression here
$node->stmts[$key - 1] = $this->createExpectExceptionStmt(
$duringAndRelatedMethodCall->getExceptionMethodCall()
);
$node->stmts[$key] = $previousStmt;
$hasChanged = true;
break;
}
if (! $hasChanged) {
return null;
}
return $node;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Reorder and rename shouldThrow() method to mark before instantiation', [
new CodeSample(
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class RenameMethodTest extends ObjectBehavior
{
public function is_should()
{
$this->beConstructedThrough('create', [$data]);
$this->shouldThrow(ValidationException::class)->duringInstantiation();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class RenameMethodTest extends ObjectBehavior
{
public function is_should()
{
$this->expectException(ValidationException::class);
$this->beConstructedThrough('create', [$data]);
}
}
CODE_SAMPLE
),
]);
}
private function createExpectExceptionStmt(MethodCall $nestedMethodCall): Expression
{
$thisExpectExceptionMethodCall = new MethodCall(new Variable('this'), 'expectException');
$thisExpectExceptionMethodCall->args[] = new Arg($nestedMethodCall->getArgs()[0]->value);
return new Expression($thisExpectExceptionMethodCall);
}
}