Skip to content

Commit 4af1f44

Browse files
committed
warn about deprecated type
1 parent 53d0947 commit 4af1f44

File tree

11 files changed

+44
-44
lines changed

11 files changed

+44
-44
lines changed

.github/workflows/code_analysis.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050

5151
-
5252
name: 'Finalize classes'
53-
run: vendor/bin/swiss-knife finalize-classes src tests rules --dry-run
53+
run: vendor/bin/swiss-knife finalize-classes src tests rules --dry-run --skip-file="src/PhpParser/Node/FileNode.php"
5454

5555
-
5656
name: 'Check before/after test fixture on no-changes'

phpstan.neon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,11 @@ parameters:
449449
message: '#Only abstract classes can be extended#'
450450
path: rules/Php81/Rector/Array_/FirstClassCallableRector.php
451451

452+
- '#Method Rector\\Tests\\Issues\\FileWithoutNamespaceCompat\\Rector\\SubscribedToFileWithoutNamespaceRector\:\:refactor\(\) should return Rector\\PhpParser\\Node\\FileNode but returns Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace#'
453+
452454
# BC layer for FileWithoutNamespace node
453455
- message: '#Use @see \\Rector\\PhpParser\\Node\\FileNode instead#'
454-
455-
456+
- '#Class Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace extends final class Rector\\PhpParser\\Node\\FileNode#'
456457
-
457458
path: src/PhpParser/Node/CustomNode/FileWithoutNamespace.php
458459
identifier: symplify.forbiddenExtendOfNonAbstractClass

rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Rector\PhpParser\Node\FileNode;
1212
use Rector\Rector\AbstractRector;
1313
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
14-
use Rector\ValueObject\Application\File;
1514
use Rector\ValueObject\PhpVersion;
1615
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1716
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -64,14 +63,12 @@ function someFunction(int $number)
6463
*/
6564
public function refactor(Node $node): ?FileNode
6665
{
67-
// parent::beforeTraverse($nodes);
68-
69-
if ($this->shouldSkipNodes($node->stmts, $this->file)) {
66+
// shebang files cannot have declare strict types
67+
if ($this->file->hasShebang()) {
7068
return null;
7169
}
7270

73-
// /** @var Node $rootStmt */
74-
// $rootStmt = current();
71+
// only add to namespaced files, as global namespace files are often included in other files
7572
if (! $node->isNamespaced()) {
7673
return null;
7774
}
@@ -82,13 +79,10 @@ public function refactor(Node $node): ?FileNode
8279
return null;
8380
}
8481

85-
// $rectorWithLineChange = new RectorWithLineChange(self::class, $rootStmt->getStartLine());
86-
// $this->file->addRectorClassWithLine($rectorWithLineChange);
87-
88-
$node->stmts = array_merge([$this->nodeFactory->createDeclaresStrictType(), new Nop()], $node->stmts);
82+
$declaresStrictType = $this->nodeFactory->createDeclaresStrictType();
83+
$node->stmts = array_merge([$declaresStrictType, new Nop()], $node->stmts);
8984

9085
return $node;
91-
// return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
9286
}
9387

9488
/**
@@ -103,21 +97,4 @@ public function provideMinPhpVersion(): int
10397
{
10498
return PhpVersion::PHP_70;
10599
}
106-
107-
/**
108-
* @param Stmt[] $nodes
109-
*/
110-
private function shouldSkipNodes(array $nodes, File $file): bool
111-
{
112-
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $file->getFilePath())) {
113-
return true;
114-
}
115-
116-
// shebang files cannot have declare strict types
117-
if (str_starts_with($file->getFileContent(), '#!')) {
118-
return true;
119-
}
120-
121-
return $nodes === [];
122-
}
123100
}

src/Console/Command/ProcessCommand.php

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

180180
$outputFormatter->report($processResult, $configuration);
181181

182+
// 4. Deprecations reporter
182183
$this->deprecatedRulesReporter->reportDeprecatedRules();
183184
$this->deprecatedRulesReporter->reportDeprecatedSkippedRules();
184185
$this->deprecatedRulesReporter->reportDeprecatedNodeTypes();

src/PhpParser/Node/CustomNode/FileWithoutNamespace.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ final class FileWithoutNamespace extends FileNode
1818
/**
1919
* @param Stmt[] $stmts
2020
*/
21-
public function __construct(
22-
public array $stmts,
23-
public array $attributes = []
24-
) {
25-
parent::__construct($stmts, $attributes);
21+
public function __construct(array $stmts)
22+
{
23+
parent::__construct($stmts);
2624
}
2725

2826
public function getType(): string

src/PhpParser/Node/FileNode.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class FileNode extends Stmt
2020
*/
2121
public function __construct(
2222
public array $stmts,
23-
array $attributes = []
2423
) {
2524
$firstStmt = $stmts[0] ?? null;
2625
$attributes = $firstStmt instanceof Node ? $firstStmt->getAttributes() : [];

src/PhpParser/NodeTraverser/RectorNodeTraverser.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ public function getVisitorsForNode(Node $node): array
7474
foreach ($this->visitors as $visitor) {
7575
foreach ($visitor->getNodeTypes() as $nodeType) {
7676
// BC layer matching
77-
if ($nodeType === FileWithoutNamespace::class) {
78-
if ($nodeClass === FileNode::class) {
79-
$this->visitorsPerNodeClass[$nodeClass][] = $visitor;
80-
continue;
81-
}
77+
if ($nodeType === FileWithoutNamespace::class && $nodeClass === FileNode::class) {
78+
$this->visitorsPerNodeClass[$nodeClass][] = $visitor;
79+
continue;
8280
}
8381

8482
if (is_a($nodeClass, $nodeType, true)) {

src/Reporting/DeprecatedRulesReporter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1111
use Rector\Contract\Rector\RectorInterface;
1212
use Rector\PhpParser\Enum\NodeGroup;
13+
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
14+
use Rector\PhpParser\Node\FileNode;
1315
use Symfony\Component\Console\Style\SymfonyStyle;
1416

1517
final readonly class DeprecatedRulesReporter
@@ -62,6 +64,11 @@ public function reportDeprecatedNodeTypes(): void
6264
static $reportedClasses = [];
6365

6466
foreach ($this->rectors as $rector) {
67+
if (in_array(FileWithoutNamespace::class, $rector->getNodeTypes(), true)) {
68+
$this->reportDeprecatedFileWithoutNamespace($rector);
69+
continue;
70+
}
71+
6572
if (! in_array(StmtsAwareInterface::class, $rector->getNodeTypes())) {
6673
continue;
6774
}
@@ -84,4 +91,16 @@ public function reportDeprecatedNodeTypes(): void
8491
));
8592
}
8693
}
94+
95+
private function reportDeprecatedFileWithoutNamespace(RectorInterface $rector): void
96+
{
97+
$this->symfonyStyle->warning(sprintf(
98+
'Node type "%s" is deprecated and will be removed. Use "%s" in the "%s" rule instead instead.%sSee %s for upgrade path',
99+
FileWithoutNamespace::class,
100+
FileNode::class,
101+
$rector::class,
102+
PHP_EOL . PHP_EOL,
103+
'https://github.com/rectorphp/rector-src/blob/main/UPGRADING.md'
104+
));
105+
}
87106
}

src/ValueObject/Application/File.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ public function getFileNode(): ?FileNode
218218

219219
return null;
220220
}
221+
222+
public function hasShebang(): bool
223+
{
224+
return str_starts_with($this->getFileContent(), '#!');
225+
}
221226
}

tests/Issues/FileWithoutNamespaceCompat/Rector/SubscribedToFileWithoutNamespaceRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Tests\Issues\FileWithoutNamespaceCompat\Rector;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Name;
89
use PhpParser\Node\Stmt\Function_;
910
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
1011
use Rector\PhpParser\Node\FileNode;
@@ -30,7 +31,7 @@ public function refactor(Node $node): FileNode
3031
{
3132
$function = new Function_('someFunction');
3233
// required for PHPStan scope resolver refresh
33-
$function->namespacedName = new Node\Name('someFunction');
34+
$function->namespacedName = new Name('someFunction');
3435

3536
$node->stmts[] = $function;
3637

0 commit comments

Comments
 (0)