Passed
Pull Request — master (#38)
by Rustam
13:03
created

NativeClassifier::getAvailableDeclarations()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 18
c 0
b 0
f 0
nc 18
nop 0
dl 0
loc 38
ccs 18
cts 18
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Classifier;
6
7
/**
8
 * `NativeClassifier` is a classifier that finds classes using PHP's native function {@see get_declared_classes()}.
9
 */
10
final class NativeClassifier extends AbstractClassifier
11
{
12
    /**
13
     * @psalm-suppress UnresolvableInclude
14
     */
15 17
    protected function getAvailableDeclarations(): iterable
16
    {
17 17
        $files = $this->getFiles();
18
19 17
        foreach ($files as $file) {
20
            try {
21 17
                require_once $file;
22 1
            } catch (\Throwable) {
23
                // Ignore syntax errors
24
            }
25
        }
26
27 17
        $declarations = [...get_declared_classes(), ...get_declared_interfaces(), ...get_declared_traits()];
28
29 17
        $directories = $this->directories;
30 17
        $isWindows = DIRECTORY_SEPARATOR === '\\';
31
32 17
        if ($isWindows) {
33
            /**
34
             * @psalm-var string[] $directories
35
             */
36
            // @codeCoverageIgnoreStart
37
            $directories = str_replace('/', '\\', $directories);
38
            // @codeCoverageIgnoreEnd
39
        }
40
41 17
        foreach ($declarations as $declaration) {
42 17
            $reflectionClass = self::$reflectionsCache[$declaration] ??= new \ReflectionClass($declaration);
0 ignored issues
show
Bug introduced by
$declaration of type array|string[] is incompatible with the type object|string expected by parameter $objectOrClass of ReflectionClass::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $reflectionClass = self::$reflectionsCache[$declaration] ??= new \ReflectionClass(/** @scrutinizer ignore-type */ $declaration);
Loading history...
43
44 17
            $matchedDirs = array_filter(
45 17
                $directories,
46 17
                static fn($directory) => $reflectionClass->getFileName() && str_starts_with($reflectionClass->getFileName(), $directory)
47 17
            );
48
49 17
            if (count($matchedDirs) === 0) {
50 17
                continue;
51
            }
52 17
            yield $reflectionClass->getName();
53
        }
54
    }
55
}
56