ClassFinder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 69
ccs 37
cts 37
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirClasses() 0 32 5
A getFileClasses() 0 19 4
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Helper;
15
16
use Generator;
17
use InvalidArgumentException;
18
use RecursiveDirectoryIterator;
19
use RecursiveIteratorIterator;
20
use ReflectionClass;
21
use ReflectionException;
22
use RegexIterator;
23
use SplFileInfo;
24
25
use function get_declared_classes;
26
use function is_dir;
27
use function is_file;
28
use function realpath;
29
use function sprintf;
30
31
/**
32
 * @since 3.0.0
33
 */
34
final class ClassFinder
35
{
36
    /**
37
     * @return Generator<class-string, ReflectionClass<object>>
38
     *
39
     * @throws InvalidArgumentException
40
     * @throws ReflectionException
41
     */
42 5
    public static function getDirClasses(string $dirname): Generator
43
    {
44 5
        if (!is_dir($dirname)) {
45 1
            throw new InvalidArgumentException(sprintf(
46 1
                'The directory "%s" does not exist.',
47 1
                $dirname,
48 1
            ));
49
        }
50
51
        /** @var iterable<string, SplFileInfo> $files */
52 4
        $files = new RegexIterator(
53 4
            new RecursiveIteratorIterator(
54 4
                new RecursiveDirectoryIterator($dirname)
55 4
            ),
56 4
            '/\.php$/',
57 4
        );
58
59
        /** @var array<string, true> $filenames */
60 4
        $filenames = [];
61 4
        foreach ($files as $file) {
62 4
            $filename = $file->getRealPath();
63 4
            (static function (string $filename): void {
64 4
                require_once $filename;
65 4
            })($filename);
66
67 4
            $filenames[$filename] = true;
68
        }
69
70 4
        foreach (get_declared_classes() as $className) {
71 4
            $classReflection = new ReflectionClass($className);
72 4
            if (isset($filenames[$classReflection->getFileName()])) {
73 4
                yield $className => $classReflection;
74
            }
75
        }
76
    }
77
78
    /**
79
     * @return Generator<class-string, ReflectionClass<object>>
80
     *
81
     * @throws InvalidArgumentException
82
     * @throws ReflectionException
83
     */
84 2
    public static function getFileClasses(string $filename): Generator
85
    {
86 2
        if (!is_file($filename)) {
87 1
            throw new InvalidArgumentException(sprintf(
88 1
                'The file "%s" does not exist.',
89 1
                $filename,
90 1
            ));
91
        }
92
93
        /** @var non-empty-string $filename */
94 1
        $filename = realpath($filename);
95 1
        (static function (string $filename): void {
96 1
            require_once $filename;
97 1
        })($filename);
98
99 1
        foreach (get_declared_classes() as $className) {
100 1
            $classReflection = new ReflectionClass($className);
101 1
            if ($classReflection->getFileName() === $filename) {
102 1
                yield $className => $classReflection;
103
            }
104
        }
105
    }
106
}
107