Skip to content

Commit 3c9d919

Browse files
committed
Analyse extractor with PHPStan
1 parent dc9494b commit 3c9d919

File tree

4 files changed

+193
-8
lines changed

4 files changed

+193
-8
lines changed

extractor/composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
"php": "^7.4 || ^8.0",
44
"symfony/console": "^6.0.0",
55
"symfony/finder": "^6.0",
6-
"nikic/php-parser": "^4.10"
6+
"nikic/php-parser": "^4.10",
7+
"phpstan/phpstan": "^1.5",
8+
"phpstan/phpstan-php-parser": "^1.1",
9+
"phpstan/extension-installer": "^1.1"
10+
},
11+
"config": {
12+
"allow-plugins": {
13+
"phpstan/extension-installer": true
14+
}
715
}
816
}

extractor/composer.lock

Lines changed: 156 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extractor/extract.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ public function __construct(
3434
$this->printer = $printer;
3535
}
3636

37-
protected function configure()
37+
protected function configure(): void
3838
{
3939
$this->setName('extract');
4040
}
4141

4242
protected function execute(InputInterface $input, OutputInterface $output): int
4343
{
4444
$ourStubsDir = realpath(__DIR__ . '/../stubs');
45+
if ($ourStubsDir === false) {
46+
throw new \LogicException('Invalid stubs path');
47+
}
4548
$this->clearOldStubs($ourStubsDir);
4649
$srcDir = realpath(__DIR__ . '/../php-src');
50+
if ($srcDir === false) {
51+
throw new \LogicException('Invalid php-src path');
52+
}
4753
$finder = new Finder();
4854
$finder->files()->in($srcDir)->name('*.stub.php')
4955
->exclude('ext/skeleton');
@@ -134,6 +140,8 @@ public function enterNode(Node $node)
134140
} else {
135141
throw new \Exception(sprintf('Unhandled node type %s in %s on line %s.', get_class($node), $this->stubPath, $node->getLine()));
136142
}
143+
144+
return null;
137145
}
138146

139147
/**
@@ -146,23 +154,33 @@ public function getStmts(): array
146154
};
147155

148156
$nodeTraverser->addVisitor($visitor);
149-
$nodeTraverser->traverse($this->parser->parse(file_get_contents($stubPath)));
157+
158+
$stubContents = file_get_contents($stubPath);
159+
if ($stubContents === false) {
160+
throw new \LogicException('Could not read stub');
161+
}
162+
$ast = $this->parser->parse($stubContents);
163+
if ($ast === null) {
164+
throw new \LogicException('AST cannot be null');
165+
}
166+
$nodeTraverser->traverse($ast);
150167

151168
$stmts = $visitor->getStmts();
152169
$classes = [];
153170
$functions = [];
154171
foreach ($stmts as $stmt) {
172+
if (!$stmt instanceof Node\Stmt\Class_ && !$stmt instanceof Node\Stmt\Interface_ && !$stmt instanceof Node\Stmt\Trait_ && !$stmt instanceof Node\Stmt\Function_) {
173+
throw new \Exception(sprintf('Unhandled node type %s in %s on line %s.', get_class($stmt), $stubPath, $stmt->getLine()));
174+
}
155175
$namespacedName = $stmt->namespacedName->toString();
156176
$pathPart = 'stubs/' . dirname($relativeStubPath) . '/' . str_replace('\\', '/', $namespacedName) . '.php';
157177
$targetStubPath = __DIR__ . '/../' . $pathPart;
158178

159179
if ($stmt instanceof Node\Stmt\Class_ || $stmt instanceof Node\Stmt\Interface_ || $stmt instanceof Node\Stmt\Trait_) {
160180
$classes[strtolower($namespacedName)] = $pathPart;
161181
$stmt = $this->filterClassPhpDocs($stmt);
162-
} elseif ($stmt instanceof Node\Stmt\Function_) {
163-
$functions[strtolower($namespacedName)] = $pathPart;
164182
} else {
165-
throw new \Exception(sprintf('Unhandled node type %s in %s on line %s.', get_class($stmt), $stubPath, $stmt->getLine()));
183+
$functions[strtolower($namespacedName)] = $pathPart;
166184
}
167185

168186
if (strpos($namespacedName, '\\') !== false) {
@@ -228,7 +246,7 @@ class Php8StubsMap
228246
{
229247
230248
public const CLASSES = %s;
231-
249+
232250
public const FUNCTIONS = %s;
233251
234252
}

extractor/phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 7
3+
paths:
4+
- extract.php

0 commit comments

Comments
 (0)