Skip to content

Commit a0f1aef

Browse files
committed
Extractor::extractAll() extracts only top-level classes and functions
1 parent 389aed1 commit a0f1aef

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

src/PhpGenerator/Extractor.php

+25-32
Original file line numberDiff line numberDiff line change
@@ -169,35 +169,6 @@ private function performReplacements(string $s, array $replacements): string
169169
public function extractAll(): PhpFile
170170
{
171171
$phpFile = new PhpFile;
172-
$namespace = '';
173-
$visitor = new class extends PhpParser\NodeVisitorAbstract {
174-
public $callback;
175-
176-
177-
public function enterNode(Node $node)
178-
{
179-
return ($this->callback)($node);
180-
}
181-
};
182-
183-
$visitor->callback = function (Node $node) use (&$namespace, $phpFile) {
184-
if ($node instanceof Node\Stmt\Class_ && !$node->name) {
185-
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
186-
}
187-
match (true) {
188-
$node instanceof Node\Stmt\DeclareDeclare
189-
&& $node->key->name === 'strict_types'
190-
&& $node->value instanceof Node\Scalar\LNumber => $phpFile->setStrictTypes((bool) $node->value->value),
191-
$node instanceof Node\Stmt\Namespace_ => $namespace = $node->name?->toString(),
192-
$node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($phpFile->addNamespace($namespace), $node),
193-
$node instanceof Node\Stmt\ClassLike => $this->addClassLikeToFile($phpFile, $node),
194-
$node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
195-
default => null,
196-
};
197-
if ($node instanceof Node\FunctionLike) {
198-
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
199-
}
200-
};
201172

202173
if (
203174
$this->statements
@@ -207,9 +178,31 @@ public function enterNode(Node $node)
207178
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
208179
}
209180

210-
$traverser = new PhpParser\NodeTraverser;
211-
$traverser->addVisitor($visitor);
212-
$traverser->traverse($this->statements);
181+
$namespaces = ['' => $this->statements];
182+
foreach ($this->statements as $node) {
183+
if ($node instanceof Node\Stmt\Declare_
184+
&& $node->declares[0] instanceof Node\Stmt\DeclareDeclare
185+
&& $node->declares[0]->key->name === 'strict_types'
186+
&& $node->declares[0]->value instanceof Node\Scalar\LNumber
187+
) {
188+
$phpFile->setStrictTypes((bool) $node->declares[0]->value->value);
189+
190+
} elseif ($node instanceof Node\Stmt\Namespace_) {
191+
$namespaces[$node->name->toString()] = $node->stmts;
192+
}
193+
}
194+
195+
foreach ($namespaces as $name => $nodes) {
196+
foreach ($nodes as $node) {
197+
match (true) {
198+
$node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($phpFile->addNamespace($name), $node),
199+
$node instanceof Node\Stmt\ClassLike => $this->addClassLikeToFile($phpFile, $node),
200+
$node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
201+
default => null,
202+
};
203+
}
204+
}
205+
213206
return $phpFile;
214207
}
215208

tests/PhpGenerator/fixtures/extractor.php

+6
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ function bar()
3737
{
3838
}
3939
}
40+
41+
if (false) {
42+
class Class2
43+
{
44+
}
45+
}

0 commit comments

Comments
 (0)