Skip to content

Commit eb7b7b6

Browse files
committed
use FileNode in DeclareStrictTypesTestsRector
1 parent 50cec58 commit eb7b7b6

File tree

3 files changed

+18
-83
lines changed

3 files changed

+18
-83
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"phpstan/phpstan-webmozart-assert": "^2.0",
1515
"phpunit/phpunit": "^11.5",
1616
"rector/jack": "^0.4.0",
17-
"rector/rector-src": "dev-main",
17+
"rector/rector-src": "dev-tv-rector-avoid-before-traverser",
1818
"rector/swiss-knife": "^1.0",
1919
"rector/type-perfect": "^2.1",
2020
"symplify/phpstan-extensions": "^12.0",

phpstan.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ parameters:
1717

1818
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
1919
typeAliases:
20-
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\CustomNode\FileWithoutNamespace
20+
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\FileNode
2121

2222
scanDirectories:
2323
- stubs
@@ -49,13 +49,3 @@ parameters:
4949
message: '#Property PhpParser\\Node\\Identifier\:\:\$name \(non\-empty\-string\) does not accept string#'
5050
path: rules/CodeQuality/Rector/ClassMethod/ReplaceTestFunctionPrefixWithAttributeRector.php
5151

52-
53-
# special case for namespace file
54-
-
55-
identifier: rector.noOnlyNullReturnInRefactor
56-
path: rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php
57-
58-
-
59-
identifier: method.parentMethodFinalByPhpDoc
60-
path: rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php
61-

rules/CodeQuality/Rector/StmtsAwareInterface/DeclareStrictTypesTestsRector.php

Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
use PhpParser\Node\Stmt;
99
use PhpParser\Node\Stmt\Class_;
1010
use PhpParser\Node\Stmt\Nop;
11-
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
1211
use Rector\Contract\Rector\HTMLAverseRectorInterface;
13-
use Rector\PhpParser\Enum\NodeGroup;
1412
use Rector\PhpParser\Node\BetterNodeFinder;
15-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
13+
use Rector\PhpParser\Node\FileNode;
1614
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1715
use Rector\Rector\AbstractRector;
1816
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
19-
use Rector\ValueObject\Application\File;
2017
use Rector\ValueObject\PhpVersion;
2118
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
2219
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -67,93 +64,41 @@ public function test()
6764
}
6865

6966
/**
70-
* @param Stmt[] $nodes
71-
* @return Stmt[]|null
67+
* @return array<class-string<Node>>
7268
*/
73-
public function beforeTraverse(array $nodes): ?array
69+
public function getNodeTypes(): array
7470
{
75-
parent::beforeTraverse($nodes);
76-
77-
if ($this->shouldSkipNodes($nodes, $this->file)) {
78-
return null;
79-
}
80-
81-
/** @var Node $rootStmt */
82-
$rootStmt = current($nodes);
71+
return [FileNode::class];
72+
}
8373

74+
/**
75+
* @param FileNode $node
76+
*/
77+
public function refactor(Node $node): ?FileNode
78+
{
8479
// when first stmt is Declare_, verify if there is strict_types definition already,
8580
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
86-
if ($rootStmt instanceof FileWithoutNamespace) {
87-
$currentNode = current($rootStmt->stmts);
88-
if (! $currentNode instanceof Stmt) {
89-
return null;
90-
}
91-
92-
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($currentNode)) {
93-
return null;
94-
}
95-
} elseif ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
81+
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($node)) {
9682
return null;
9783
}
9884

99-
if (! $this->hasPHPUnitTestClass($nodes)) {
85+
if (! $this->hasPHPUnitTestClass($node->stmts)) {
10086
return null;
10187
}
10288

103-
$rectorWithLineChange = new RectorWithLineChange(self::class, $rootStmt->getStartLine());
104-
$this->file->addRectorClassWithLine($rectorWithLineChange);
105-
106-
if ($rootStmt instanceof FileWithoutNamespace) {
107-
$stmts = [$this->nodeFactory->createDeclaresStrictType()];
108-
$stmts = array_merge($stmts, [new Nop()]);
109-
$stmts = array_merge($stmts, $rootStmt->stmts);
110-
$rootStmt->stmts = $stmts;
111-
112-
return [$rootStmt];
113-
}
114-
115-
return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
116-
}
117-
118-
/**
119-
* @return array<class-string<Node>>
120-
*/
121-
public function getNodeTypes(): array
122-
{
123-
return NodeGroup::STMTS_AWARE;
124-
}
125-
126-
/**
127-
* @param StmtsAware $node
128-
*/
129-
public function refactor(Node $node): null
130-
{
13189
// workaround, as Rector now only hooks to specific nodes, not arrays
13290
// avoid traversing, as we already handled in beforeTraverse()
133-
return null;
91+
$declareStrictTypes = $this->nodeFactory->createDeclaresStrictType();
92+
$node->stmts = array_merge([$declareStrictTypes, new Nop()], $node->stmts);
93+
94+
return $node;
13495
}
13596

13697
public function provideMinPhpVersion(): int
13798
{
13899
return PhpVersion::PHP_70;
139100
}
140101

141-
/**
142-
* @param Stmt[] $nodes
143-
*/
144-
private function shouldSkipNodes(array $nodes, File $file): bool
145-
{
146-
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $file->getFilePath())) {
147-
return true;
148-
}
149-
150-
if (str_starts_with($file->getFileContent(), '#!')) {
151-
return true;
152-
}
153-
154-
return $nodes === [];
155-
}
156-
157102
/**
158103
* @param Stmt[] $nodes
159104
*/

0 commit comments

Comments
 (0)