@@ -34,16 +34,22 @@ public function __construct(
34
34
$ this ->printer = $ printer ;
35
35
}
36
36
37
- protected function configure ()
37
+ protected function configure (): void
38
38
{
39
39
$ this ->setName ('extract ' );
40
40
}
41
41
42
42
protected function execute (InputInterface $ input , OutputInterface $ output ): int
43
43
{
44
44
$ ourStubsDir = realpath (__DIR__ . '/../stubs ' );
45
+ if ($ ourStubsDir === false ) {
46
+ throw new \LogicException ('Invalid stubs path ' );
47
+ }
45
48
$ this ->clearOldStubs ($ ourStubsDir );
46
49
$ srcDir = realpath (__DIR__ . '/../php-src ' );
50
+ if ($ srcDir === false ) {
51
+ throw new \LogicException ('Invalid php-src path ' );
52
+ }
47
53
$ finder = new Finder ();
48
54
$ finder ->files ()->in ($ srcDir )->name ('*.stub.php ' )
49
55
->exclude ('ext/skeleton ' );
@@ -134,6 +140,8 @@ public function enterNode(Node $node)
134
140
} else {
135
141
throw new \Exception (sprintf ('Unhandled node type %s in %s on line %s. ' , get_class ($ node ), $ this ->stubPath , $ node ->getLine ()));
136
142
}
143
+
144
+ return null ;
137
145
}
138
146
139
147
/**
@@ -146,23 +154,33 @@ public function getStmts(): array
146
154
};
147
155
148
156
$ 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 );
150
167
151
168
$ stmts = $ visitor ->getStmts ();
152
169
$ classes = [];
153
170
$ functions = [];
154
171
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
+ }
155
175
$ namespacedName = $ stmt ->namespacedName ->toString ();
156
176
$ pathPart = 'stubs/ ' . dirname ($ relativeStubPath ) . '/ ' . str_replace ('\\' , '/ ' , $ namespacedName ) . '.php ' ;
157
177
$ targetStubPath = __DIR__ . '/../ ' . $ pathPart ;
158
178
159
179
if ($ stmt instanceof Node \Stmt \Class_ || $ stmt instanceof Node \Stmt \Interface_ || $ stmt instanceof Node \Stmt \Trait_) {
160
180
$ classes [strtolower ($ namespacedName )] = $ pathPart ;
161
181
$ stmt = $ this ->filterClassPhpDocs ($ stmt );
162
- } elseif ($ stmt instanceof Node \Stmt \Function_) {
163
- $ functions [strtolower ($ namespacedName )] = $ pathPart ;
164
182
} 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 ;
166
184
}
167
185
168
186
if (strpos ($ namespacedName , '\\' ) !== false ) {
@@ -228,7 +246,7 @@ class Php8StubsMap
228
246
{
229
247
230
248
public const CLASSES = %s;
231
-
249
+
232
250
public const FUNCTIONS = %s;
233
251
234
252
}
0 commit comments