Skip to content

Commit 4fa695b

Browse files
authored
use FileNode in DeclareStrictTypesTestsRector (#599)
1 parent 6d2b1ba commit 4fa695b

File tree

2 files changed

+16
-87
lines changed

2 files changed

+16
-87
lines changed

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: 15 additions & 76 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,97 +64,39 @@ 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->file->containsHTML()) {
78-
return null;
79-
}
80-
81-
if ($this->shouldSkipNodes($nodes, $this->file)) {
82-
return null;
83-
}
84-
85-
/** @var Node $rootStmt */
86-
$rootStmt = current($nodes);
71+
return [FileNode::class];
72+
}
8773

74+
/**
75+
* @param FileNode $node
76+
*/
77+
public function refactor(Node $node): ?FileNode
78+
{
8879
// when first stmt is Declare_, verify if there is strict_types definition already,
8980
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
90-
if ($rootStmt instanceof FileWithoutNamespace) {
91-
$currentNode = current($rootStmt->stmts);
92-
if (! $currentNode instanceof Stmt) {
93-
return null;
94-
}
95-
96-
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($currentNode)) {
97-
return null;
98-
}
99-
} elseif ($this->declareStrictTypeFinder->hasDeclareStrictTypes($rootStmt)) {
81+
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($node)) {
10082
return null;
10183
}
10284

103-
if (! $this->hasPHPUnitTestClass($nodes)) {
85+
if (! $this->hasPHPUnitTestClass($node->stmts)) {
10486
return null;
10587
}
10688

107-
$rectorWithLineChange = new RectorWithLineChange(self::class, $rootStmt->getStartLine());
108-
$this->file->addRectorClassWithLine($rectorWithLineChange);
109-
110-
if ($rootStmt instanceof FileWithoutNamespace) {
111-
$stmts = [$this->nodeFactory->createDeclaresStrictType()];
112-
$stmts = array_merge($stmts, [new Nop()]);
113-
$stmts = array_merge($stmts, $rootStmt->stmts);
114-
$rootStmt->stmts = $stmts;
89+
$declareStrictTypes = $this->nodeFactory->createDeclaresStrictType();
90+
$node->stmts = array_merge([$declareStrictTypes, new Nop()], $node->stmts);
11591

116-
return [$rootStmt];
117-
}
118-
119-
return [$this->nodeFactory->createDeclaresStrictType(), new Nop(), ...$nodes];
120-
}
121-
122-
/**
123-
* @return array<class-string<Node>>
124-
*/
125-
public function getNodeTypes(): array
126-
{
127-
return NodeGroup::STMTS_AWARE;
128-
}
129-
130-
/**
131-
* @param StmtsAware $node
132-
*/
133-
public function refactor(Node $node): null
134-
{
135-
// workaround, as Rector now only hooks to specific nodes, not arrays
136-
// avoid traversing, as we already handled in beforeTraverse()
137-
return null;
92+
return $node;
13893
}
13994

14095
public function provideMinPhpVersion(): int
14196
{
14297
return PhpVersion::PHP_70;
14398
}
14499

145-
/**
146-
* @param Stmt[] $nodes
147-
*/
148-
private function shouldSkipNodes(array $nodes, File $file): bool
149-
{
150-
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $file->getFilePath())) {
151-
return true;
152-
}
153-
154-
if (str_starts_with($file->getFileContent(), '#!')) {
155-
return true;
156-
}
157-
158-
return $nodes === [];
159-
}
160-
161100
/**
162101
* @param Stmt[] $nodes
163102
*/

0 commit comments

Comments
 (0)