|
8 | 8 | use PhpParser\Node\Stmt; |
9 | 9 | use PhpParser\Node\Stmt\Class_; |
10 | 10 | use PhpParser\Node\Stmt\Nop; |
11 | | -use Rector\ChangesReporting\ValueObject\RectorWithLineChange; |
12 | 11 | use Rector\Contract\Rector\HTMLAverseRectorInterface; |
13 | | -use Rector\PhpParser\Enum\NodeGroup; |
14 | 12 | use Rector\PhpParser\Node\BetterNodeFinder; |
15 | | -use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; |
| 13 | +use Rector\PhpParser\Node\FileNode; |
16 | 14 | use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
17 | 15 | use Rector\Rector\AbstractRector; |
18 | 16 | use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder; |
19 | | -use Rector\ValueObject\Application\File; |
20 | 17 | use Rector\ValueObject\PhpVersion; |
21 | 18 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
22 | 19 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
@@ -67,97 +64,39 @@ public function test() |
67 | 64 | } |
68 | 65 |
|
69 | 66 | /** |
70 | | - * @param Stmt[] $nodes |
71 | | - * @return Stmt[]|null |
| 67 | + * @return array<class-string<Node>> |
72 | 68 | */ |
73 | | - public function beforeTraverse(array $nodes): ?array |
| 69 | + public function getNodeTypes(): array |
74 | 70 | { |
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 | + } |
87 | 73 |
|
| 74 | + /** |
| 75 | + * @param FileNode $node |
| 76 | + */ |
| 77 | + public function refactor(Node $node): ?FileNode |
| 78 | + { |
88 | 79 | // when first stmt is Declare_, verify if there is strict_types definition already, |
89 | 80 | // 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)) { |
100 | 82 | return null; |
101 | 83 | } |
102 | 84 |
|
103 | | - if (! $this->hasPHPUnitTestClass($nodes)) { |
| 85 | + if (! $this->hasPHPUnitTestClass($node->stmts)) { |
104 | 86 | return null; |
105 | 87 | } |
106 | 88 |
|
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); |
115 | 91 |
|
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; |
138 | 93 | } |
139 | 94 |
|
140 | 95 | public function provideMinPhpVersion(): int |
141 | 96 | { |
142 | 97 | return PhpVersion::PHP_70; |
143 | 98 | } |
144 | 99 |
|
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 | | - |
161 | 100 | /** |
162 | 101 | * @param Stmt[] $nodes |
163 | 102 | */ |
|
0 commit comments