-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRemoveShouldBeCalledRector.php
88 lines (74 loc) · 2.17 KB
/
RemoveShouldBeCalledRector.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
<?php
declare(strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Param;
use Rector\PhpSpecToPHPUnit\Enum\PhpSpecMethodName;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PhpSpecToPHPUnit\Tests\Rector\MethodCall\RemoveShouldBeCalledRector\RemoveShouldBeCalledRectorTest
*/
final class RemoveShouldBeCalledRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isName($node->name, PhpSpecMethodName::SHOULD_BE_CALLED)) {
// The shouldBeCalled() is implicit and not needed, handled by another rule
return $node->var;
}
if ($this->isName($node->name, PhpSpecMethodName::SHOULD_NOT_BE_CALLED)) {
// The shouldNeverBeCalled() is implicit and not needed, handled by another rule
return $node->var;
}
if ($this->isName($node->name, PhpSpecMethodName::WILL_RETURN) && $node->getArgs() === []) {
return $node->var;
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove shouldBeCalled() as implicit in PHPUnit, also empty willReturn() as no return is implicit in PHPUnit',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->run()->shouldBeCalled();
$this->go()->willReturn();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PhpSpec\ObjectBehavior;
class ResultSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->run();
$this->go();
}
}
CODE_SAMPLE
),
]
);
}
}