• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / Simple-PHP-Code-Parser / 24273115812

11 Apr 2026 02:59AM UTC coverage: 82.014% (-1.3%) from 83.27%
24273115812

Pull #78

github

web-flow
Merge 7857155e9 into cc07ae5a0
Pull Request #78: Update dependencies: php-parser v5, phpdoc-parser v2, reflection-docblock v6, type-resolver v2, phpunit v11

26 of 33 new or added lines in 9 files covered. (78.79%)

24 existing lines in 2 files now uncovered.

1523 of 1857 relevant lines covered (82.01%)

43.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.4
/src/voku/SimplePhpParser/Parsers/PhpCodeParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace voku\SimplePhpParser\Parsers;
6

7
use FilesystemIterator;
8
use PhpParser\NodeTraverser;
9
use PhpParser\NodeVisitor\NameResolver;
10
use PhpParser\ParserFactory;
11
use React\Filesystem\Node\FileInterface;
12
use RecursiveDirectoryIterator;
13
use RecursiveIteratorIterator;
14
use SplFileInfo;
15
use voku\cache\Cache;
16
use voku\SimplePhpParser\Model\PHPInterface;
17
use voku\SimplePhpParser\Parsers\Helper\ParserContainer;
18
use voku\SimplePhpParser\Parsers\Helper\ParserErrorHandler;
19
use voku\SimplePhpParser\Parsers\Helper\Utils;
20
use voku\SimplePhpParser\Parsers\Visitors\ASTVisitor;
21
use voku\SimplePhpParser\Parsers\Visitors\ParentConnector;
22
use function React\Async\await;
23
use function React\Promise\all;
24

25
class PhpCodeParser
26
{
27
    /**
28
     * @internal
29
     */
30
    private const CACHE_KEY_HELPER = 'simple-php-code-parser-v6-';
31

32
    /**
33
     * @param string   $code
34
     * @param string[] $autoloaderProjectPaths
35
     *
36
     * @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
37
     */
38
    public static function getFromString(
39
        string $code,
40
        array $autoloaderProjectPaths = []
41
    ): ParserContainer {
42
        return self::getPhpFiles(
28✔
43
            $code,
28✔
44
            $autoloaderProjectPaths
28✔
45
        );
28✔
46
    }
47

48
    /**
49
     * @param string   $className
50
     * @param string[] $autoloaderProjectPaths
51
     *
52
     * @phpstan-param class-string $className
53
     *
54
     * @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
55
     */
56
    public static function getFromClassName(
57
        string $className,
58
        array $autoloaderProjectPaths = []
59
    ): ParserContainer {
60
        $reflectionClass = Utils::createClassReflectionInstance($className);
4✔
61

62
        return self::getPhpFiles(
4✔
63
            (string) $reflectionClass->getFileName(),
4✔
64
            $autoloaderProjectPaths
4✔
65
        );
4✔
66
    }
67

68
    /**
69
     * @param string   $pathOrCode
70
     * @param string[] $autoloaderProjectPaths
71
     * @param string[] $pathExcludeRegex
72
     * @param string[] $fileExtensions
73
     *
74
     * @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
75
     */
76
    public static function getPhpFiles(
77
        string $pathOrCode,
78
        array $autoloaderProjectPaths = [],
79
        array $pathExcludeRegex = [],
80
        array $fileExtensions = []
81
    ): ParserContainer {
82
        // Push a disposable handler so restore_error_handler() below will only
83
        // pop this one entry, leaving any pre-existing handlers (e.g. PHPUnit's)
84
        // intact on the stack.
85
        \set_error_handler(null);
146✔
86
        foreach ($autoloaderProjectPaths as $projectPath) {
146✔
NEW
87
            if (\file_exists($projectPath) && \is_file($projectPath)) {
×
NEW
88
                require_once $projectPath;
×
NEW
89
            } elseif (\file_exists($projectPath . '/vendor/autoload.php')) {
×
NEW
90
                require_once $projectPath . '/vendor/autoload.php';
×
NEW
91
            } elseif (\file_exists($projectPath . '/../vendor/autoload.php')) {
×
NEW
92
                require_once $projectPath . '/../vendor/autoload.php';
×
93
            }
94
        }
95
        \restore_error_handler();
146✔
96

97
        $phpCodes = self::getCode(
146✔
98
            $pathOrCode,
146✔
99
            $pathExcludeRegex,
146✔
100
            $fileExtensions
146✔
101
        );
146✔
102

103
        $parserContainer = new ParserContainer();
146✔
104
        $visitor = new ASTVisitor($parserContainer);
146✔
105

106
        $processResults = [];
146✔
107
        $phpCodesChunks = \array_chunk($phpCodes, Utils::getCpuCores(), true);
146✔
108

109
        foreach ($phpCodesChunks as $phpCodesChunk) {
146✔
110
            foreach ($phpCodesChunk as $codeAndFileName) {
146✔
111
                $processResults[] = self::process(
146✔
112
                    $codeAndFileName['content'],
146✔
113
                    $codeAndFileName['fileName'],
146✔
114
                    $parserContainer,
146✔
115
                    $visitor
146✔
116
                );
146✔
117
            }
118
        }
119

120
        foreach ($processResults as $response) {
146✔
121
            if ($response instanceof ParserContainer) {
146✔
122
                $parserContainer->setTraits($response->getTraits());
146✔
123
                $parserContainer->setClasses($response->getClasses());
146✔
124
                $parserContainer->setInterfaces($response->getInterfaces());
146✔
125
                $parserContainer->setEnums($response->getEnums());
146✔
126
                $parserContainer->setConstants($response->getConstants());
146✔
127
                $parserContainer->setFunctions($response->getFunctions());
146✔
128
            } elseif ($response instanceof ParserErrorHandler) {
×
129
                $parserContainer->setParseError($response);
×
130
            }
131
        }
132

133
        $interfaces = $parserContainer->getInterfaces();
146✔
134
        foreach ($interfaces as &$interface) {
146✔
135
            $interface->parentInterfaces = $visitor->combineParentInterfaces($interface);
8✔
136
        }
137
        unset($interface);
146✔
138

139
        $pathTmp = null;
146✔
140
        if (\is_file($pathOrCode)) {
146✔
141
            $pathTmp = \realpath(\pathinfo($pathOrCode, \PATHINFO_DIRNAME));
110✔
142
        } elseif (\is_dir($pathOrCode)) {
36✔
143
            $pathTmp = \realpath($pathOrCode);
8✔
144
        }
145

146
        $classesTmp = &$parserContainer->getClassesByReference();
146✔
147
        foreach ($classesTmp as &$classTmp) {
146✔
148
            $classTmp->interfaces = Utils::flattenArray(
111✔
149
                $visitor->combineImplementedInterfaces($classTmp),
111✔
150
                false
111✔
151
            );
111✔
152

153
            self::mergeInheritdocData(
111✔
154
                $classTmp,
111✔
155
                $classesTmp,
111✔
156
                $interfaces,
111✔
157
                $parserContainer
111✔
158
            );
111✔
159
        }
160
        unset($classTmp);
146✔
161

162
        // remove properties / methods / classes from outside of the current file-path-scope
163
        if ($pathTmp) {
146✔
164
            $classesTmp2 = &$parserContainer->getClassesByReference();
118✔
165
            foreach ($classesTmp2 as $classKey => $classTmp2) {
118✔
166
                foreach ($classTmp2->constants as $constantKey => $constant) {
99✔
167
                    if ($constant->file && \strpos($constant->file, $pathTmp) === false) {
54✔
168
                        unset($classTmp2->constants[$constantKey]);
6✔
169
                    }
170
                }
171

172
                foreach ($classTmp2->properties as $propertyKey => $property) {
99✔
173
                    if ($property->file && \strpos($property->file, $pathTmp) === false) {
85✔
174
                        unset($classTmp2->properties[$propertyKey]);
8✔
175
                    }
176
                }
177

178
                foreach ($classTmp2->methods as $methodKey => $method) {
99✔
179
                    if ($method->file && \strpos($method->file, $pathTmp) === false) {
97✔
180
                        unset($classTmp2->methods[$methodKey]);
8✔
181
                    }
182
                }
183

184
                if ($classTmp2->file && \strpos($classTmp2->file, $pathTmp) === false) {
99✔
185
                    unset($classesTmp2[$classKey]);
8✔
186
                }
187
            }
188
        }
189

190
        return $parserContainer;
146✔
191
    }
192

193
    /**
194
     * @param string                                               $phpCode
195
     * @param string|null                                          $fileName
196
     * @param \voku\SimplePhpParser\Parsers\Helper\ParserContainer $parserContainer
197
     * @param \voku\SimplePhpParser\Parsers\Visitors\ASTVisitor    $visitor
198
     *
199
     * @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer|\voku\SimplePhpParser\Parsers\Helper\ParserErrorHandler
200
     */
201
    public static function process(
202
        string $phpCode,
203
        ?string $fileName,
204
        ParserContainer $parserContainer,
205
        ASTVisitor $visitor
206
    ) {
207
        $parser = (new ParserFactory())->createForNewestSupportedVersion();
146✔
208

209
        $errorHandler = new ParserErrorHandler();
146✔
210

211
        $nameResolver = new NameResolver(
146✔
212
            $errorHandler,
146✔
213
            [
146✔
214
                'preserveOriginalNames' => true,
146✔
215
            ]
146✔
216
        );
146✔
217

218
        /** @var \PhpParser\Node[]|null $parsedCode */
219
        $parsedCode = $parser->parse($phpCode, $errorHandler);
146✔
220

221
        if ($parsedCode === null) {
146✔
222
            return $errorHandler;
×
223
        }
224

225
        $visitor->fileName = $fileName;
146✔
226

227
        // Pass 1: set parent attributes and fully resolve all names in the AST.
228
        // NameResolver modifies Name nodes in-place (converting them to FullyQualified),
229
        // so by the time ASTVisitor runs in pass 2, every type-hint Name node already
230
        // carries its fully-qualified form. This is necessary because ASTVisitor processes
231
        // class members (properties, methods) eagerly inside enterNode(Class_), before
232
        // the single-pass traverser would have had a chance to visit those child nodes.
233
        $traverser1 = new NodeTraverser();
146✔
234
        $traverser1->addVisitor(new ParentConnector());
146✔
235
        $traverser1->addVisitor($nameResolver);
146✔
236
        $traverser1->traverse($parsedCode);
146✔
237

238
        // Pass 2: extract model objects from the already-resolved AST.
239
        $traverser2 = new NodeTraverser();
146✔
240
        $traverser2->addVisitor($visitor);
146✔
241
        $traverser2->traverse($parsedCode);
146✔
242

243
        return $parserContainer;
146✔
244
    }
245

246
    /**
247
     * @param string   $pathOrCode
248
     * @param string[] $pathExcludeRegex
249
     * @param string[] $fileExtensions
250
     *
251
     * @return array
252
     *
253
     * @psalm-return array<string, array{content: string, fileName: null|string}>
254
     */
255
    private static function getCode(
256
        string $pathOrCode,
257
        array $pathExcludeRegex = [],
258
        array $fileExtensions = []
259
    ): array {
260
        // init
261
        $phpCodes = [];
146✔
262
        /** @var SplFileInfo[] $phpFileIterators */
263
        $phpFileIterators = [];
146✔
264
        /** @var \React\Promise\PromiseInterface[] $phpFilePromises */
265
        $phpFilePromises = [];
146✔
266

267
        // fallback
268
        if (\count($fileExtensions) === 0) {
146✔
269
            $fileExtensions = ['.php'];
146✔
270
        }
271

272
        if (\is_file($pathOrCode)) {
146✔
273
            $phpFileIterators = [new SplFileInfo($pathOrCode)];
110✔
274
        } elseif (\is_dir($pathOrCode)) {
36✔
275
            $phpFileIterators = new RecursiveIteratorIterator(
8✔
276
                new RecursiveDirectoryIterator($pathOrCode, FilesystemIterator::SKIP_DOTS)
8✔
277
            );
8✔
278
        } else {
279
            $cacheKey = self::CACHE_KEY_HELPER . \md5($pathOrCode);
28✔
280

281
            $phpCodes[$cacheKey]['content'] = $pathOrCode;
28✔
282
            $phpCodes[$cacheKey]['fileName'] = null;
28✔
283
        }
284

285
        $cache = new Cache(null, null, false);
146✔
286

287
        $phpFileArray = [];
146✔
288
        foreach ($phpFileIterators as $fileOrCode) {
146✔
289
            $path = $fileOrCode->getRealPath();
118✔
290
            if (!$path) {
118✔
291
                continue;
×
292
            }
293

294
            $fileExtensionFound = false;
118✔
295
            foreach ($fileExtensions as $fileExtension) {
118✔
296
                if (\substr($path, -\strlen($fileExtension)) === $fileExtension) {
118✔
297
                    $fileExtensionFound = true;
118✔
298

299
                    break;
118✔
300
                }
301
            }
302
            if ($fileExtensionFound === false) {
118✔
303
                continue;
×
304
            }
305

306
            foreach ($pathExcludeRegex as $regex) {
118✔
307
                if (\preg_match($regex, $path)) {
4✔
308
                    continue 2;
4✔
309
                }
310
            }
311

312
            $cacheKey = self::CACHE_KEY_HELPER . \md5($path) . '--' . \filemtime($path);
118✔
313
            if ($cache->getCacheIsReady() === true && $cache->existsItem($cacheKey)) {
118✔
314
                $response = $cache->getItem($cacheKey);
79✔
315
                /** @noinspection PhpSillyAssignmentInspection - helper for phpstan */
316
                /** @phpstan-var array{content: string, fileName: string, cacheKey: string} $response */
317
                $response = $response;
79✔
318

319
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
79✔
320
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
79✔
321

322
                continue;
79✔
323
            }
324

325
            $phpFileArray[$cacheKey] = $path;
47✔
326
        }
327

328
        $phpFileArrayChunks = \array_chunk($phpFileArray, Utils::getCpuCores(), true);
146✔
329
        foreach ($phpFileArrayChunks as $phpFileArrayChunk) {
146✔
330
            $filesystem = \React\Filesystem\Factory::create();
47✔
331

332
            foreach ($phpFileArrayChunk as $cacheKey => $path) {
47✔
333
                $phpFilePromises[] = $filesystem->detect($path)->then(
47✔
334
                    function (FileInterface $file) use ($path, $cacheKey) {
47✔
335
                        return [
47✔
336
                            'content'  => $file->getContents()->then(static function (string $contents) {
47✔
337
                                return $contents;
47✔
338
                            }),
47✔
339
                            'fileName' => $path,
47✔
340
                            'cacheKey' => $cacheKey,
47✔
341
                        ];
47✔
342
                    },
47✔
343
                    function ($e) {
47✔
344
                        throw $e;
×
345
                    }
47✔
346
                );
47✔
347
            }
348

349
            $phpFilePromiseResponses = await(all($phpFilePromises));
47✔
350
            foreach ($phpFilePromiseResponses as $response) {
47✔
351
                $response['content'] = await($response['content']);
47✔
352

353
                assert(is_string($response['content']));
354
                assert(is_string($response['cacheKey']));
355
                assert($response['fileName'] === null || is_string($response['fileName']));
356

357
                @$cache->setItem($response['cacheKey'], $response);
47✔
358

359
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
47✔
360
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
47✔
361
            }
362
        }
363

364
        return $phpCodes;
146✔
365
    }
366

367
    /**
368
     * @param \voku\SimplePhpParser\Model\PHPClass   $class
369
     * @param \voku\SimplePhpParser\Model\PHPClass[] $classes
370
     * @param PHPInterface[]                         $interfaces
371
     * @param ParserContainer                        $parserContainer
372
     */
373
    private static function mergeInheritdocData(
374
        \voku\SimplePhpParser\Model\PHPClass $class,
375
        array $classes,
376
        array $interfaces,
377
        ParserContainer $parserContainer
378
    ): void {
379
        foreach ($class->properties as &$property) {
111✔
380
            if (!$class->parentClass) {
93✔
381
                break;
73✔
382
            }
383

384
            if (!$property->is_inheritdoc) {
40✔
385
                continue;
40✔
386
            }
387

388
            if (
389
                !isset($classes[$class->parentClass])
×
390
                &&
391
                \class_exists($class->parentClass, true)
×
392
            ) {
393
                $reflectionClassTmp = Utils::createClassReflectionInstance($class->parentClass);
×
394
                $classTmp = (new \voku\SimplePhpParser\Model\PHPClass($parserContainer))->readObjectFromReflection($reflectionClassTmp);
×
395
                if ($classTmp->name) {
×
396
                    $classes[$classTmp->name] = $classTmp;
×
397
                }
398
            }
399

400
            if (!isset($classes[$class->parentClass])) {
×
401
                continue;
×
402
            }
403

404
            if (!isset($classes[$class->parentClass]->properties[$property->name])) {
×
405
                continue;
×
406
            }
407

408
            $parentMethod = $classes[$class->parentClass]->properties[$property->name];
×
409

410
            foreach ($property as $key => &$value) {
×
411
                if (
412
                    $value === null
×
413
                    &&
414
                    $parentMethod->{$key} !== null
×
415
                    &&
416
                    \stripos($key, 'type') !== false
×
417
                ) {
418
                    $value = $parentMethod->{$key};
×
419
                }
420
            }
421
        }
422
        unset($property, $value); /* @phpstan-ignore-line ? */
111✔
423

424
        foreach ($class->methods as &$method) {
111✔
425
            if (!$method->is_inheritdoc) {
105✔
426
                continue;
101✔
427
            }
428

429
            foreach ($class->interfaces as $interfaceStr) {
31✔
430
                if (
431
                    !isset($interfaces[$interfaceStr])
31✔
432
                    &&
433
                    \interface_exists($interfaceStr, true)
31✔
434
                ) {
435
                    $reflectionInterfaceTmp = Utils::createClassReflectionInstance($interfaceStr);
23✔
436
                    $interfaceTmp = (new PHPInterface($parserContainer))->readObjectFromReflection($reflectionInterfaceTmp);
23✔
437
                    if ($interfaceTmp->name) {
23✔
438
                        $interfaces[$interfaceTmp->name] = $interfaceTmp;
23✔
439
                    }
440
                }
441

442
                if (!isset($interfaces[$interfaceStr])) {
31✔
443
                    continue;
×
444
                }
445

446
                if (!isset($interfaces[$interfaceStr]->methods[$method->name])) {
31✔
447
                    continue;
24✔
448
                }
449

450
                $interfaceMethod = $interfaces[$interfaceStr]->methods[$method->name];
19✔
451

452
                foreach ($method as $key => &$value) {
19✔
453
                    if (
454
                        $value === null
19✔
455
                        &&
456
                        $interfaceMethod->{$key} !== null
19✔
457
                        &&
458
                        \stripos($key, 'type') !== false
19✔
459
                    ) {
460
                        $value = $interfaceMethod->{$key};
19✔
461
                    }
462

463
                    if ($key === 'parameters') {
19✔
464
                        $parameterCounter = 0;
19✔
465
                        foreach ($value as &$parameter) {
19✔
466
                            ++$parameterCounter;
19✔
467

468
                            \assert($parameter instanceof \voku\SimplePhpParser\Model\PHPParameter);
469

470
                            $interfaceMethodParameter = null;
19✔
471
                            $parameterCounterInterface = 0;
19✔
472
                            foreach ($interfaceMethod->parameters as $parameterInterface) {
19✔
473
                                ++$parameterCounterInterface;
19✔
474

475
                                if ($parameterCounterInterface === $parameterCounter) {
19✔
476
                                    $interfaceMethodParameter = $parameterInterface;
19✔
477
                                }
478
                            }
479

480
                            if (!$interfaceMethodParameter) {
19✔
481
                                continue;
×
482
                            }
483

484
                            foreach ($parameter as $keyInner => &$valueInner) {
19✔
485
                                if (
486
                                    $valueInner === null
19✔
487
                                    &&
488
                                    $interfaceMethodParameter->{$keyInner} !== null
19✔
489
                                    &&
490
                                    \stripos($keyInner, 'type') !== false
19✔
491
                                ) {
492
                                    $valueInner = $interfaceMethodParameter->{$keyInner};
19✔
493
                                }
494
                            }
495
                            unset($valueInner); /* @phpstan-ignore-line ? */
19✔
496
                        }
497
                        unset($parameter);
19✔
498
                    }
499
                }
500
                unset($value); /* @phpstan-ignore-line ? */
19✔
501
            }
502

503
            if (!isset($classes[$class->parentClass])) {
31✔
504
                continue;
19✔
505
            }
506

507
            if (!isset($classes[$class->parentClass]->methods[$method->name])) {
20✔
508
                continue;
×
509
            }
510

511
            $parentMethod = $classes[$class->parentClass]->methods[$method->name];
20✔
512

513
            foreach ($method as $key => &$value) {
20✔
514
                if (
515
                    $value === null
20✔
516
                    &&
517
                    $parentMethod->{$key} !== null
20✔
518
                    &&
519
                    \stripos($key, 'type') !== false
20✔
520
                ) {
521
                    $value = $parentMethod->{$key};
20✔
522
                }
523

524
                if ($key === 'parameters') {
20✔
525
                    $parameterCounter = 0;
20✔
526
                    foreach ($value as &$parameter) {
20✔
527
                        ++$parameterCounter;
20✔
528

529
                        \assert($parameter instanceof \voku\SimplePhpParser\Model\PHPParameter);
530

531
                        $parentMethodParameter = null;
20✔
532
                        $parameterCounterParent = 0;
20✔
533
                        foreach ($parentMethod->parameters as $parameterParent) {
20✔
534
                            ++$parameterCounterParent;
20✔
535

536
                            if ($parameterCounterParent === $parameterCounter) {
20✔
537
                                $parentMethodParameter = $parameterParent;
20✔
538
                            }
539
                        }
540

541
                        if (!$parentMethodParameter) {
20✔
542
                            continue;
×
543
                        }
544

545
                        foreach ($parameter as $keyInner => &$valueInner) {
20✔
546
                            if (
547
                                $valueInner === null
20✔
548
                                &&
549
                                $parentMethodParameter->{$keyInner} !== null
20✔
550
                                &&
551
                                \stripos($keyInner, 'type') !== false
20✔
552
                            ) {
553
                                $valueInner = $parentMethodParameter->{$keyInner};
20✔
554
                            }
555
                        }
556
                    }
557
                }
558
            }
559
        }
560
    }
561
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc