Skip to content

Commit 0cd995e

Browse files
committed
[dx] warn about deprecated beforeTraverse() method once FileNode is ready
1 parent e39c3e1 commit 0cd995e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ parameters:
160160
- src/Validation/RectorConfigValidator.php
161161
# for phpunit version check
162162
- src/Testing/PHPUnit/AbstractLazyTestCase.php
163+
# future node class exists check
164+
- src/Reporting/DeprecatedRulesReporter.php
163165

164166
# use of internal phpstan classes
165167
-

src/Console/Command/ProcessCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
182182
$this->deprecatedRulesReporter->reportDeprecatedRules();
183183
$this->deprecatedRulesReporter->reportDeprecatedSkippedRules();
184184
$this->deprecatedRulesReporter->reportDeprecatedNodeTypes();
185+
$this->deprecatedRulesReporter->reportDeprecatedRectorUnsupportedMethods();
185186

186187
$this->missConfigurationReporter->reportSkippedNeverRegisteredRules();
187188

src/Reporting/DeprecatedRulesReporter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1111
use Rector\Contract\Rector\RectorInterface;
1212
use Rector\PhpParser\Enum\NodeGroup;
13+
use ReflectionMethod;
1314
use Symfony\Component\Console\Style\SymfonyStyle;
1415

1516
final readonly class DeprecatedRulesReporter
@@ -56,6 +57,25 @@ public function reportDeprecatedSkippedRules(): void
5657
}
5758
}
5859

60+
public function reportDeprecatedRectorUnsupportedMethods(): void
61+
{
62+
// to be added in related PR
63+
if (! class_exists(\Rector\PhpParserNode\FileNode::class)) {
64+
return;
65+
}
66+
67+
foreach ($this->rectors as $rector) {
68+
$beforeTraverseMethodReflection = new ReflectionMethod($rector, 'beforeTraverse');
69+
if ($beforeTraverseMethodReflection->getDeclaringClass()->getName() === $rector::class) {
70+
$this->symfonyStyle->warning(sprintf(
71+
'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.',
72+
$rector::class,
73+
\Rector\PhpParserNode\FileNode::class
74+
));
75+
}
76+
}
77+
}
78+
5979
public function reportDeprecatedNodeTypes(): void
6080
{
6181
// helper property to avoid reporting multiple times

0 commit comments

Comments
 (0)