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

voku / Simple-PHP-Code-Parser / 5709606856

pending completion
5709606856

push

github

voku
[+]: ci: run test with PHP 8.2

1305 of 1592 relevant lines covered (81.97%)

7.61 hits per line

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

87.83
/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\Lexer\Emulative;
9
use PhpParser\NodeTraverser;
10
use PhpParser\NodeVisitor\NameResolver;
11
use PhpParser\ParserFactory;
12
use React\Filesystem\Node\FileInterface;
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-v4-';
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(
5✔
44
            $code,
5✔
45
            $autoloaderProjectPaths
5✔
46
        );
5✔
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);
1✔
62

63
        return self::getPhpFiles(
1✔
64
            (string) $reflectionClass->getFileName(),
1✔
65
            $autoloaderProjectPaths
1✔
66
        );
1✔
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
        foreach ($autoloaderProjectPaths as $projectPath) {
20✔
84
            if (\file_exists($projectPath) && \is_file($projectPath)) {
×
85
                /** @noinspection PhpIncludeInspection */
86
                require_once $projectPath;
×
87
            } elseif (\file_exists($projectPath . '/vendor/autoload.php')) {
×
88
                /** @noinspection PhpIncludeInspection */
89
                require_once $projectPath . '/vendor/autoload.php';
×
90
            } elseif (\file_exists($projectPath . '/../vendor/autoload.php')) {
×
91
                /** @noinspection PhpIncludeInspection */
92
                require_once $projectPath . '/../vendor/autoload.php';
×
93
            }
94
        }
95
        \restore_error_handler();
20✔
96

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

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

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

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

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

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

138
        $pathTmp = null;
20✔
139
        if (\is_file($pathOrCode)) {
20✔
140
            $pathTmp = \realpath(\pathinfo($pathOrCode, \PATHINFO_DIRNAME));
14✔
141
        } elseif (\is_dir($pathOrCode)) {
6✔
142
            $pathTmp = \realpath($pathOrCode);
1✔
143
        }
144

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

152
            self::mergeInheritdocData(
16✔
153
                $classTmp,
16✔
154
                $classesTmp,
16✔
155
                $interfaces,
16✔
156
                $parserContainer
16✔
157
            );
16✔
158
        }
159
        unset($classTmp);
20✔
160

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

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

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

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

189
        return $parserContainer;
20✔
190
    }
191

192
    /**
193
     * @param string                                               $phpCode
194
     * @param string|null                                          $fileName
195
     * @param \voku\SimplePhpParser\Parsers\Helper\ParserContainer $parserContainer
196
     * @param \voku\SimplePhpParser\Parsers\Visitors\ASTVisitor    $visitor
197
     *
198
     * @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer|\voku\SimplePhpParser\Parsers\Helper\ParserErrorHandler
199
     */
200
    public static function process(
201
        string $phpCode,
202
        ?string $fileName,
203
        ParserContainer $parserContainer,
204
        ASTVisitor $visitor
205
    ) {
206
        $parser = (new ParserFactory())->create(
20✔
207
            ParserFactory::PREFER_PHP7,
20✔
208
            new Emulative(
20✔
209
                [
20✔
210
                    'usedAttributes' => [
20✔
211
                        'comments',
20✔
212
                        'startLine',
20✔
213
                        'endLine',
20✔
214
                        'startTokenPos',
20✔
215
                        'endTokenPos',
20✔
216
                    ],
20✔
217
                ]
20✔
218
            )
20✔
219
        );
20✔
220

221
        $errorHandler = new ParserErrorHandler();
20✔
222

223
        $nameResolver = new NameResolver(
20✔
224
            $errorHandler,
20✔
225
            [
20✔
226
                'preserveOriginalNames' => true,
20✔
227
            ]
20✔
228
        );
20✔
229

230
        /** @var \PhpParser\Node[]|null $parsedCode */
231
        $parsedCode = $parser->parse($phpCode, $errorHandler);
20✔
232

233
        if ($parsedCode === null) {
20✔
234
            return $errorHandler;
×
235
        }
236

237
        $visitor->fileName = $fileName;
20✔
238

239
        $traverser = new NodeTraverser();
20✔
240
        $traverser->addVisitor(new ParentConnector());
20✔
241
        $traverser->addVisitor($nameResolver);
20✔
242
        $traverser->addVisitor($visitor);
20✔
243
        $traverser->traverse($parsedCode);
20✔
244

245
        return $parserContainer;
20✔
246
    }
247

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

269
        // fallback
270
        if (\count($fileExtensions) === 0) {
20✔
271
            $fileExtensions = ['.php'];
20✔
272
        }
273

274
        if (\is_file($pathOrCode)) {
20✔
275
            $phpFileIterators = [new SplFileInfo($pathOrCode)];
14✔
276
        } elseif (\is_dir($pathOrCode)) {
6✔
277
            $phpFileIterators = new RecursiveIteratorIterator(
1✔
278
                new RecursiveDirectoryIterator($pathOrCode, FilesystemIterator::SKIP_DOTS)
1✔
279
            );
1✔
280
        } else {
281
            $cacheKey = self::CACHE_KEY_HELPER . \md5($pathOrCode);
5✔
282

283
            $phpCodes[$cacheKey]['content'] = $pathOrCode;
5✔
284
            $phpCodes[$cacheKey]['fileName'] = null;
5✔
285
        }
286

287
        $cache = new Cache(null, null, false);
20✔
288

289
        $phpFileArray = [];
20✔
290
        foreach ($phpFileIterators as $fileOrCode) {
20✔
291
            $path = $fileOrCode->getRealPath();
15✔
292
            if (!$path) {
15✔
293
                continue;
×
294
            }
295

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

301
                    break;
15✔
302
                }
303
            }
304
            if ($fileExtensionFound === false) {
15✔
305
                continue;
×
306
            }
307

308
            foreach ($pathExcludeRegex as $regex) {
15✔
309
                if (\preg_match($regex, $path)) {
1✔
310
                    continue 2;
1✔
311
                }
312
            }
313

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

321
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
5✔
322
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
5✔
323

324
                continue;
5✔
325
            }
326

327
            $phpFileArray[$cacheKey] = $path;
11✔
328
        }
329

330
        $phpFileArrayChunks = \array_chunk($phpFileArray, Utils::getCpuCores(), true);
20✔
331
        foreach ($phpFileArrayChunks as $phpFileArrayChunk) {
20✔
332
            $filesystem = \React\Filesystem\Factory::create();
11✔
333

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

351
            $phpFilePromiseResponses = await(all($phpFilePromises));
11✔
352
            foreach ($phpFilePromiseResponses as $response) {
11✔
353
                $response['content'] = await($response['content']);
11✔
354

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

359
                $cache->setItem($response['cacheKey'], $response);
11✔
360

361
                $phpCodes[$response['cacheKey']]['content'] = $response['content'];
11✔
362
                $phpCodes[$response['cacheKey']]['fileName'] = $response['fileName'];
11✔
363
            }
364
        }
365

366
        return $phpCodes;
20✔
367
    }
368

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

386
            if (!$property->is_inheritdoc) {
9✔
387
                continue;
9✔
388
            }
389

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

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

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

410
            $parentMethod = $classes[$class->parentClass]->properties[$property->name];
×
411

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

426
        foreach ($class->methods as &$method) {
16✔
427
            if (!$method->is_inheritdoc) {
15✔
428
                continue;
14✔
429
            }
430

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

444
                if (!isset($interfaces[$interfaceStr])) {
7✔
445
                    continue;
×
446
                }
447

448
                if (!isset($interfaces[$interfaceStr]->methods[$method->name])) {
7✔
449
                    continue;
5✔
450
                }
451

452
                $interfaceMethod = $interfaces[$interfaceStr]->methods[$method->name];
4✔
453

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

465
                    if ($key === 'parameters') {
4✔
466
                        $parameterCounter = 0;
4✔
467
                        foreach ($value as &$parameter) {
4✔
468
                            ++$parameterCounter;
4✔
469

470
                            \assert($parameter instanceof \voku\SimplePhpParser\Model\PHPParameter);
471

472
                            $interfaceMethodParameter = null;
4✔
473
                            $parameterCounterInterface = 0;
4✔
474
                            foreach ($interfaceMethod->parameters as $parameterInterface) {
4✔
475
                                ++$parameterCounterInterface;
4✔
476

477
                                if ($parameterCounterInterface === $parameterCounter) {
4✔
478
                                    $interfaceMethodParameter = $parameterInterface;
4✔
479
                                }
480
                            }
481

482
                            if (!$interfaceMethodParameter) {
4✔
483
                                continue;
×
484
                            }
485

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

505
            if (!isset($classes[$class->parentClass])) {
7✔
506
                continue;
4✔
507
            }
508

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

513
            $parentMethod = $classes[$class->parentClass]->methods[$method->name];
4✔
514

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

526
                if ($key === 'parameters') {
4✔
527
                    $parameterCounter = 0;
4✔
528
                    foreach ($value as &$parameter) {
4✔
529
                        ++$parameterCounter;
4✔
530

531
                        \assert($parameter instanceof \voku\SimplePhpParser\Model\PHPParameter);
532

533
                        $parentMethodParameter = null;
4✔
534
                        $parameterCounterParent = 0;
4✔
535
                        foreach ($parentMethod->parameters as $parameterParent) {
4✔
536
                            ++$parameterCounterParent;
4✔
537

538
                            if ($parameterCounterParent === $parameterCounter) {
4✔
539
                                $parentMethodParameter = $parameterParent;
4✔
540
                            }
541
                        }
542

543
                        if (!$parentMethodParameter) {
4✔
544
                            continue;
×
545
                        }
546

547
                        foreach ($parameter as $keyInner => &$valueInner) {
4✔
548
                            if (
549
                                $valueInner === null
4✔
550
                                &&
551
                                $parentMethodParameter->{$keyInner} !== null
4✔
552
                                &&
553
                                \stripos($keyInner, 'type') !== false
4✔
554
                            ) {
555
                                $valueInner = $parentMethodParameter->{$keyInner};
4✔
556
                            }
557
                        }
558
                    }
559
                }
560
            }
561
        }
562
    }
563
}
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