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

voku / Simple-PHP-Code-Parser / 24277086119

11 Apr 2026 06:53AM UTC coverage: 82.886%. Remained the same
24277086119

push

github

web-flow
Merge pull request #64 from BrianHenryIE/fix-phpDocumentor-Reflection-deprecation-warning

Fix reflection-docblock deprecation warnings

1545 of 1864 relevant lines covered (82.89%)

91.03 hits per line

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

87.5
/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 React\Filesystem\Node\NodeInterface;
13
use RecursiveDirectoryIterator;
14
use RecursiveIteratorIterator;
15
use SplFileInfo;
16
use voku\cache\Cache;
17
use voku\SimplePhpParser\Model\PHPInterface;
18
use voku\SimplePhpParser\Parsers\Helper\ParserContainer;
19
use voku\SimplePhpParser\Parsers\Helper\ParserErrorHandler;
20
use voku\SimplePhpParser\Parsers\Helper\Utils;
21
use voku\SimplePhpParser\Parsers\Visitors\ASTVisitor;
22
use voku\SimplePhpParser\Parsers\Visitors\ParentConnector;
23
use function React\Async\await;
24
use function React\Promise\all;
25

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

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

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

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

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

101
        $phpCodes = self::getCode(
312✔
102
            $pathOrCode,
312✔
103
            $pathExcludeRegex,
312✔
104
            $fileExtensions
312✔
105
        );
312✔
106

107
        $parserContainer = new ParserContainer();
312✔
108
        $visitor = new ASTVisitor($parserContainer);
312✔
109

110
        $processResults = [];
312✔
111
        $phpCodesChunks = \array_chunk($phpCodes, Utils::getCpuCores(), true);
312✔
112

113
        foreach ($phpCodesChunks as $phpCodesChunk) {
312✔
114
            foreach ($phpCodesChunk as $codeAndFileName) {
312✔
115
                $processResults[] = self::process(
312✔
116
                    $codeAndFileName['content'],
312✔
117
                    $codeAndFileName['fileName'],
312✔
118
                    $parserContainer,
312✔
119
                    $visitor
312✔
120
                );
312✔
121
            }
122
        }
123

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

137
        $interfaces = $parserContainer->getInterfaces();
312✔
138
        foreach ($interfaces as &$interface) {
312✔
139
            $interface->parentInterfaces = $visitor->combineParentInterfaces($interface);
16✔
140
        }
141
        unset($interface);
312✔
142

143
        $pathTmp = null;
312✔
144
        if (\is_file($pathOrCode)) {
312✔
145
            $pathTmp = \realpath(\pathinfo($pathOrCode, \PATHINFO_DIRNAME));
220✔
146
        } elseif (\is_dir($pathOrCode)) {
92✔
147
            $pathTmp = \realpath($pathOrCode);
16✔
148
        }
149

150
        $classesTmp = &$parserContainer->getClassesByReference();
312✔
151
        foreach ($classesTmp as &$classTmp) {
312✔
152
            $classTmp->interfaces = Utils::flattenArray(
242✔
153
                $visitor->combineImplementedInterfaces($classTmp),
242✔
154
                false
242✔
155
            );
242✔
156

157
            self::mergeInheritdocData(
242✔
158
                $classTmp,
242✔
159
                $classesTmp,
242✔
160
                $interfaces,
242✔
161
                $parserContainer
242✔
162
            );
242✔
163
        }
164
        unset($classTmp);
312✔
165

166
        // remove properties / methods / classes from outside of the current file-path-scope
167
        if ($pathTmp) {
312✔
168
            $classesTmp2 = &$parserContainer->getClassesByReference();
236✔
169
            foreach ($classesTmp2 as $classKey => $classTmp2) {
236✔
170
                foreach ($classTmp2->constants as $constantKey => $constant) {
198✔
171
                    if ($constant->file && \strpos($constant->file, $pathTmp) === false) {
108✔
172
                        unset($classTmp2->constants[$constantKey]);
14✔
173
                    }
174
                }
175

176
                foreach ($classTmp2->properties as $propertyKey => $property) {
198✔
177
                    if ($property->file && \strpos($property->file, $pathTmp) === false) {
170✔
178
                        unset($classTmp2->properties[$propertyKey]);
16✔
179
                    }
180
                }
181

182
                foreach ($classTmp2->methods as $methodKey => $method) {
198✔
183
                    if ($method->file && \strpos($method->file, $pathTmp) === false) {
194✔
184
                        unset($classTmp2->methods[$methodKey]);
16✔
185
                    }
186
                }
187

188
                if ($classTmp2->file && \strpos($classTmp2->file, $pathTmp) === false) {
198✔
189
                    unset($classesTmp2[$classKey]);
16✔
190
                }
191
            }
192
        }
193

194
        return $parserContainer;
312✔
195
    }
196

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

213
        $errorHandler = new ParserErrorHandler();
312✔
214

215
        $nameResolver = new NameResolver(
312✔
216
            $errorHandler,
312✔
217
            [
312✔
218
                'preserveOriginalNames' => true,
312✔
219
            ]
312✔
220
        );
312✔
221

222
        /** @var \PhpParser\Node[]|null $parsedCode */
223
        $parsedCode = $parser->parse($phpCode, $errorHandler);
312✔
224

225
        if ($parsedCode === null) {
312✔
226
            return $errorHandler;
×
227
        }
228

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

240
        $visitor->fileName = $fileName;
312✔
241

242
        // Pass 2: extract model objects from the already-resolved AST.
243
        $traverser2 = new NodeTraverser();
312✔
244
        $traverser2->addVisitor($visitor);
312✔
245
        $traverser2->traverse($parsedCode);
312✔
246

247
        return $parserContainer;
312✔
248
    }
249

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

271
        // fallback
272
        if (\count($fileExtensions) === 0) {
312✔
273
            $fileExtensions = ['.php'];
312✔
274
        }
275

276
        if (\is_file($pathOrCode)) {
312✔
277
            $phpFileIterators = [new SplFileInfo($pathOrCode)];
220✔
278
        } elseif (\is_dir($pathOrCode)) {
92✔
279
            $phpFileIterators = new RecursiveIteratorIterator(
16✔
280
                new RecursiveDirectoryIterator($pathOrCode, FilesystemIterator::SKIP_DOTS)
16✔
281
            );
16✔
282
        } else {
283
            $cacheKey = self::CACHE_KEY_HELPER . \md5($pathOrCode);
76✔
284

285
            $phpCodes[$cacheKey]['content'] = $pathOrCode;
76✔
286
            $phpCodes[$cacheKey]['fileName'] = null;
76✔
287
        }
288

289
        $cache = new Cache(null, null, false);
312✔
290

291
        $phpFileArray = [];
312✔
292
        foreach ($phpFileIterators as $fileOrCode) {
312✔
293
            $path = $fileOrCode->getRealPath();
236✔
294
            if (!$path) {
236✔
295
                continue;
×
296
            }
297

298
            $fileExtensionFound = false;
236✔
299
            foreach ($fileExtensions as $fileExtension) {
236✔
300
                if (\substr($path, -\strlen($fileExtension)) === $fileExtension) {
236✔
301
                    $fileExtensionFound = true;
236✔
302

303
                    break;
236✔
304
                }
305
            }
306
            if ($fileExtensionFound === false) {
236✔
307
                continue;
×
308
            }
309

310
            foreach ($pathExcludeRegex as $regex) {
236✔
311
                if (\preg_match($regex, $path)) {
8✔
312
                    continue 2;
8✔
313
                }
314
            }
315

316
            $cacheKey = self::CACHE_KEY_HELPER . \md5($path) . '--' . \filemtime($path);
236✔
317
            if ($cache->getCacheIsReady() === true && $cache->existsItem($cacheKey)) {
236✔
318
                $response = $cache->getItem($cacheKey);
158✔
319
                /** @noinspection PhpSillyAssignmentInspection - helper for phpstan */
320
                /** @phpstan-var array{content: string, fileName: string, cacheKey: string} $response */
321
                $response = $response;
158✔
322

323
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
158✔
324
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
158✔
325

326
                continue;
158✔
327
            }
328

329
            $phpFileArray[$cacheKey] = $path;
94✔
330
        }
331

332
        $phpFileArrayChunks = \array_chunk($phpFileArray, Utils::getCpuCores(), true);
312✔
333
        foreach ($phpFileArrayChunks as $phpFileArrayChunk) {
312✔
334
            $filesystem = \React\Filesystem\Factory::create();
94✔
335

336
            foreach ($phpFileArrayChunk as $cacheKey => $path) {
94✔
337
                $phpFilePromises[] = $filesystem->detect($path)->then(
94✔
338
                    static function (NodeInterface $node) use ($path, $cacheKey): array {
94✔
339
                        if (!$node instanceof FileInterface) {
94✔
340
                            throw new \RuntimeException('Expected a file node for: ' . $path);
×
341
                        }
342

343
                        return [
94✔
344
                            'content'  => $node->getContents()->then(static function (string $contents): string {
94✔
345
                                return $contents;
94✔
346
                            }),
94✔
347
                            'fileName' => $path,
94✔
348
                            'cacheKey' => $cacheKey,
94✔
349
                        ];
94✔
350
                    },
94✔
351
                    function ($e) {
94✔
352
                        throw $e;
×
353
                    }
94✔
354
                );
94✔
355
            }
356

357
            /** @var list<array{content: \React\Promise\PromiseInterface<string>, fileName: string, cacheKey: string}> $phpFilePromiseResponses */
358
            $phpFilePromiseResponses = await(all($phpFilePromises));
94✔
359
            foreach ($phpFilePromiseResponses as $response) {
94✔
360
                $response['content'] = await($response['content']);
94✔
361

362
                assert(is_string($response['content']));
363
                assert(is_string($response['cacheKey']));
364
                assert($response['fileName'] === null || is_string($response['fileName']));
365

366
                @$cache->setItem($response['cacheKey'], $response);
94✔
367

368
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
94✔
369
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
94✔
370
            }
371
        }
372

373
        return $phpCodes;
312✔
374
    }
375

376
    /**
377
     * @param \voku\SimplePhpParser\Model\PHPClass   $class
378
     * @param \voku\SimplePhpParser\Model\PHPClass[] $classes
379
     * @param PHPInterface[]                         $interfaces
380
     * @param ParserContainer                        $parserContainer
381
     */
382
    private static function mergeInheritdocData(
383
        \voku\SimplePhpParser\Model\PHPClass $class,
384
        array $classes,
385
        array $interfaces,
386
        ParserContainer $parserContainer
387
    ): void {
388
        foreach ($class->properties as &$property) {
242✔
389
            if (!$class->parentClass) {
186✔
390
                break;
146✔
391
            }
392

393
            if (!$property->is_inheritdoc) {
80✔
394
                continue;
80✔
395
            }
396

397
            if (
398
                !isset($classes[$class->parentClass])
×
399
                &&
400
                \class_exists($class->parentClass, true)
×
401
            ) {
402
                $reflectionClassTmp = Utils::createClassReflectionInstance($class->parentClass);
×
403
                $classTmp = (new \voku\SimplePhpParser\Model\PHPClass($parserContainer))->readObjectFromReflection($reflectionClassTmp);
×
404
                if ($classTmp->name) {
×
405
                    $classes[$classTmp->name] = $classTmp;
×
406
                }
407
            }
408

409
            if (!isset($classes[$class->parentClass])) {
×
410
                continue;
×
411
            }
412

413
            if (!isset($classes[$class->parentClass]->properties[$property->name])) {
×
414
                continue;
×
415
            }
416

417
            $parentMethod = $classes[$class->parentClass]->properties[$property->name];
×
418
            self::mergeMissingTypeFields($property, $parentMethod);
×
419
        }
420
        unset($property);
242✔
421

422
        foreach ($class->methods as &$method) {
242✔
423
            if (!$method->is_inheritdoc) {
226✔
424
                continue;
218✔
425
            }
426

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

440
                if (!isset($interfaces[$interfaceStr])) {
62✔
441
                    continue;
×
442
                }
443

444
                if (!isset($interfaces[$interfaceStr]->methods[$method->name])) {
62✔
445
                    continue;
48✔
446
                }
447

448
                $interfaceMethod = $interfaces[$interfaceStr]->methods[$method->name];
38✔
449

450
                self::mergeMissingTypeFields($method, $interfaceMethod);
38✔
451
                $method->parameters = self::mergeMissingParameterTypeFields($method->parameters, $interfaceMethod->parameters);
38✔
452
            }
453

454
            if (!isset($classes[$class->parentClass])) {
62✔
455
                continue;
38✔
456
            }
457

458
            if (!isset($classes[$class->parentClass]->methods[$method->name])) {
40✔
459
                continue;
×
460
            }
461

462
            $parentMethod = $classes[$class->parentClass]->methods[$method->name];
40✔
463

464
            self::mergeMissingTypeFields($method, $parentMethod);
40✔
465
            $method->parameters = self::mergeMissingParameterTypeFields($method->parameters, $parentMethod->parameters);
40✔
466
        }
467
    }
468

469
    private static function mergeMissingTypeFields(object $target, object $source): void
470
    {
471
        foreach (\array_keys(\get_object_vars($target)) as $key) {
62✔
472
            if (\stripos($key, 'type') === false) {
62✔
473
                continue;
62✔
474
            }
475

476
            if ($target->{$key} === null && $source->{$key} !== null) {
62✔
477
                $target->{$key} = $source->{$key};
62✔
478
            }
479
        }
480
    }
481

482
    /**
483
     * @param array<string, \voku\SimplePhpParser\Model\PHPParameter> $targetParameters
484
     * @param array<string, \voku\SimplePhpParser\Model\PHPParameter> $sourceParameters
485
     *
486
     * @return array<string, \voku\SimplePhpParser\Model\PHPParameter>
487
     */
488
    private static function mergeMissingParameterTypeFields(array $targetParameters, array $sourceParameters): array
489
    {
490
        $sourceParameters = \array_values($sourceParameters);
62✔
491

492
        $position = 0;
62✔
493
        foreach ($targetParameters as $parameterName => $parameter) {
62✔
494
            $sourceParameter = $sourceParameters[$position] ?? null;
62✔
495
            ++$position;
62✔
496

497
            if ($sourceParameter === null) {
62✔
498
                continue;
×
499
            }
500

501
            self::mergeMissingTypeFields($parameter, $sourceParameter);
62✔
502
            $targetParameters[$parameterName] = $parameter;
62✔
503
        }
504

505
        return $targetParameters;
62✔
506
    }
507
}
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