forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplicitPhpErrorApiRector.php
142 lines (124 loc) · 4.02 KB
/
ExplicitPhpErrorApiRector.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
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\PHPUnit90\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFactory\AssertCallFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.0.md
* @changelog https://github.com/sebastianbergmann/phpunit/commit/1ba2e3e1bb091acda3139f8a9259fa8161f3242d
*
* @see \Rector\PHPUnit\Tests\PHPUnit90\Rector\MethodCall\ExplicitPhpErrorApiRector\ExplicitPhpErrorApiRectorTest
*/
final class ExplicitPhpErrorApiRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const REPLACEMENTS = [
'PHPUnit\Framework\TestCase\Notice' => 'expectNotice',
'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation',
'PHPUnit\Framework\TestCase\Error' => 'expectError',
'PHPUnit\Framework\TestCase\Warning' => 'expectWarning',
];
public function __construct(
private readonly AssertCallFactory $assertCallFactory,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use explicit API for expecting PHP errors, warnings, and notices',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class);
$this->expectException(\PHPUnit\Framework\TestCase\Error::class);
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class);
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->expectDeprecation();
$this->expectError();
$this->expectNotice();
$this->expectWarning();
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
}
/**
* @param MethodCall|StaticCall $node
*/
public function refactor(Node $node): null|MethodCall|StaticCall
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['expectException'])) {
return null;
}
foreach (self::REPLACEMENTS as $class => $method) {
$newNode = $this->replaceExceptionWith($node, $class, $method);
if ($newNode instanceof Node) {
return $newNode;
}
}
return null;
}
private function replaceExceptionWith(
MethodCall|StaticCall $node,
string $exceptionClass,
string $explicitMethod
): null|MethodCall|StaticCall {
if ($node->isFirstClassCallable()) {
return null;
}
if (! isset($node->getArgs()[0])) {
return null;
}
$firstArg = $node->getArgs()[0];
if (! $this->isClassConstReference($firstArg->value, $exceptionClass)) {
return null;
}
return $this->assertCallFactory->createCallWithName($node, $explicitMethod);
}
/**
* Detects "SomeClass::class"
*/
private function isClassConstReference(Expr $expr, string $className): bool
{
if (! $expr instanceof ClassConstFetch) {
return false;
}
if (! $this->isName($expr->name, 'class')) {
return false;
}
return $this->isName($expr->class, $className);
}
}