Skip to content

Commit 194351c

Browse files
[dx] add stmts aware deprecation notice in getNodeTypes() (#7692)
* [dx] add stmts aware deprecation notice in getNodeTypes() * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 32a4549 commit 194351c

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ parameters:
266266

267267
# BC layer on Rector deprecation
268268
-
269-
path: src/Configuration/RectorConfigBuilder.php
269+
paths:
270+
- src/Configuration/RectorConfigBuilder.php
271+
- src/Reporting/DeprecatedRulesReporter.php
270272
identifier: classConstant.deprecatedInterface
271273
- '#Class Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace implements deprecated interface Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface#'
272274

src/Console/Command/ProcessCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170170

171171
$this->deprecatedRulesReporter->reportDeprecatedRules();
172172
$this->deprecatedRulesReporter->reportDeprecatedSkippedRules();
173+
$this->deprecatedRulesReporter->reportDeprecatedNodeTypes();
173174

174175
$this->missConfigurationReporter->reportSkippedNeverRegisteredRules();
175176

src/DependencyInjection/LazyContainerFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
use Rector\PHPStanStaticTypeMapper\TypeMapper\VoidTypeMapper;
166166
use Rector\PostRector\Application\PostFileProcessor;
167167
use Rector\Rector\AbstractRector;
168+
use Rector\Reporting\DeprecatedRulesReporter;
168169
use Rector\Skipper\Skipper\Skipper;
169170
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
170171
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
@@ -428,6 +429,10 @@ public function create(): RectorConfig
428429
->needs('$rectors')
429430
->giveTagged(RectorInterface::class);
430431

432+
$rectorConfig->when(DeprecatedRulesReporter::class)
433+
->needs('$rectors')
434+
->giveTagged(RectorInterface::class);
435+
431436
$rectorConfig->singleton(FileProcessor::class);
432437
$rectorConfig->singleton(PostFileProcessor::class);
433438

src/Reporting/DeprecatedRulesReporter.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44

55
namespace Rector\Reporting;
66

7+
use Rector\PhpParser\Enum\NodeGroup;
78
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
89
use Rector\Configuration\Option;
910
use Rector\Configuration\Parameter\SimpleParameterProvider;
11+
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
12+
use Rector\Contract\Rector\RectorInterface;
1013
use Symfony\Component\Console\Style\SymfonyStyle;
1114

1215
final readonly class DeprecatedRulesReporter
1316
{
17+
/**
18+
* @param RectorInterface[] $rectors
19+
*/
1420
public function __construct(
15-
private SymfonyStyle $symfonyStyle
21+
private SymfonyStyle $symfonyStyle,
22+
private array $rectors
1623
) {
1724
}
1825

@@ -48,4 +55,33 @@ public function reportDeprecatedSkippedRules(): void
4855
$this->symfonyStyle->warning(sprintf('Skipped rule "%s" is deprecated', $skippedRectorRule));
4956
}
5057
}
58+
59+
public function reportDeprecatedNodeTypes(): void
60+
{
61+
// helper property to avoid reporting multiple times
62+
static $reportedClasses = [];
63+
64+
foreach ($this->rectors as $rector) {
65+
if (! in_array(StmtsAwareInterface::class, $rector->getNodeTypes())) {
66+
continue;
67+
}
68+
69+
// already reported, skip
70+
if (in_array($rector::class, $reportedClasses, true)) {
71+
continue;
72+
}
73+
74+
$reportedClasses[] = $rector::class;
75+
76+
$this->symfonyStyle->warning(sprintf(
77+
'Rector rule "%s" uses StmtsAwareInterface that is now deprecated.%sUse "%s::%s" instead.%sSee %s for more',
78+
$rector::class,
79+
PHP_EOL,
80+
NodeGroup::class,
81+
'STMTS_AWARE',
82+
PHP_EOL . PHP_EOL,
83+
'https://github.com/rectorphp/rector-src/pull/7679'
84+
));
85+
}
86+
}
5187
}

0 commit comments

Comments
 (0)