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

api-platform / core / 13586524635

28 Feb 2025 10:45AM UTC coverage: 7.289%. Remained the same
13586524635

push

github

soyuka
docs: v4.0.19

12436 of 170622 relevant lines covered (7.29%)

11.99 hits per line

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

91.18
/src/Metadata/Util/ReflectionClassRecursiveIterator.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Metadata\Util;
15

16
/**
17
 * Gets reflection classes for php files in the given directories.
18
 *
19
 * @author Antoine Bluchet <soyuka@gmail.com>
20
 *
21
 * @internal
22
 */
23
final class ReflectionClassRecursiveIterator
24
{
25
    /**
26
     * @var array<string, array<class-string, \ReflectionClass>>
27
     */
28
    private static array $localCache;
29

30
    private function __construct()
31
    {
32
    }
×
33

34
    /**
35
     * @return array<class-string, \ReflectionClass>
36
     */
37
    public static function getReflectionClassesFromDirectories(array $directories): array
38
    {
39
        $id = hash('xxh3', implode('', $directories));
10✔
40
        if (isset(self::$localCache[$id])) {
10✔
41
            return self::$localCache[$id];
4✔
42
        }
43

44
        $includedFiles = [];
6✔
45
        foreach ($directories as $path) {
6✔
46
            $iterator = new \RegexIterator(
6✔
47
                new \RecursiveIteratorIterator(
6✔
48
                    new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
6✔
49
                    \RecursiveIteratorIterator::LEAVES_ONLY
6✔
50
                ),
6✔
51
                '/^.+\.php$/i',
6✔
52
                \RecursiveRegexIterator::GET_MATCH
6✔
53
            );
6✔
54

55
            foreach ($iterator as $file) {
6✔
56
                $sourceFile = $file[0];
6✔
57

58
                if (!preg_match('(^phar:)i', (string) $sourceFile)) {
6✔
59
                    $sourceFile = realpath($sourceFile);
6✔
60
                }
61

62
                try {
63
                    require_once $sourceFile;
6✔
64
                } catch (\Throwable) {
×
65
                    // invalid PHP file (example: missing parent class)
66
                    continue;
×
67
                }
68

69
                $includedFiles[$sourceFile] = true;
6✔
70
            }
71
        }
72

73
        $sortedClasses = get_declared_classes();
6✔
74
        sort($sortedClasses);
6✔
75
        $sortedInterfaces = get_declared_interfaces();
6✔
76
        sort($sortedInterfaces);
6✔
77
        $declared = [...$sortedClasses, ...$sortedInterfaces];
6✔
78
        $ret = [];
6✔
79
        foreach ($declared as $className) {
6✔
80
            $reflectionClass = new \ReflectionClass($className);
6✔
81
            $sourceFile = $reflectionClass->getFileName();
6✔
82
            if (isset($includedFiles[$sourceFile])) {
6✔
83
                $ret[$className] = $reflectionClass;
6✔
84
            }
85
        }
86

87
        return self::$localCache[$id] = $ret;
6✔
88
    }
89
}
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

© 2025 Coveralls, Inc