-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathDeprecatedRulesReporter.php
More file actions
108 lines (91 loc) · 3.63 KB
/
DeprecatedRulesReporter.php
File metadata and controls
108 lines (91 loc) · 3.63 KB
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
<?php
declare(strict_types=1);
namespace Rector\Reporting;
use Rector\PhpParserNode\FileNode;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\PhpParser\Enum\NodeGroup;
use ReflectionMethod;
use Symfony\Component\Console\Style\SymfonyStyle;
final readonly class DeprecatedRulesReporter
{
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private SymfonyStyle $symfonyStyle,
private array $rectors
) {
}
public function reportDeprecatedRules(): void
{
/** @var string[] $registeredRectorRules */
$registeredRectorRules = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES);
foreach ($registeredRectorRules as $registeredRectorRule) {
if (! is_a($registeredRectorRule, DeprecatedInterface::class, true)) {
continue;
}
$this->symfonyStyle->warning(
sprintf(
'Registered rule "%s" is deprecated and will be removed. Upgrade your config to use another rule or remove it',
$registeredRectorRule
)
);
}
}
public function reportDeprecatedSkippedRules(): void
{
/** @var string[] $skippedRectorRules */
$skippedRectorRules = SimpleParameterProvider::provideArrayParameter(Option::SKIPPED_RECTOR_RULES);
foreach ($skippedRectorRules as $skippedRectorRule) {
if (! is_a($skippedRectorRule, DeprecatedInterface::class, true)) {
continue;
}
$this->symfonyStyle->warning(sprintf('Skipped rule "%s" is deprecated', $skippedRectorRule));
}
}
public function reportDeprecatedRectorUnsupportedMethods(): void
{
// to be added in related PR
if (! class_exists(FileNode::class)) {
return;
}
foreach ($this->rectors as $rector) {
$beforeTraverseMethodReflection = new ReflectionMethod($rector, 'beforeTraverse');
if ($beforeTraverseMethodReflection->getDeclaringClass()->getName() === $rector::class) {
$this->symfonyStyle->warning(sprintf(
'Rector rule "%s" uses deprecated "beforeTraverse" method. It should not be used, as will be marked as final. Not part of RectorInterface contract. Use "%s" to hook into file-level changes instead.',
$rector::class,
FileNode::class
));
}
}
}
public function reportDeprecatedNodeTypes(): void
{
// helper property to avoid reporting multiple times
static $reportedClasses = [];
foreach ($this->rectors as $rector) {
if (! in_array(StmtsAwareInterface::class, $rector->getNodeTypes())) {
continue;
}
// already reported, skip
if (in_array($rector::class, $reportedClasses, true)) {
continue;
}
$reportedClasses[] = $rector::class;
$this->symfonyStyle->warning(sprintf(
'Rector rule "%s" uses StmtsAwareInterface that is now deprecated.%sUse "%s::%s" instead.%sSee %s for more',
$rector::class,
PHP_EOL,
NodeGroup::class,
'STMTS_AWARE',
PHP_EOL . PHP_EOL,
'https://github.com/rectorphp/rector-src/pull/7679'
));
}
}
}