Passed
Push — master ( 813f0f...a309d2 )
by butschster
09:30 queued 12s
created

EnumLocator::getEnums()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
ccs 13
cts 14
cp 0.9286
rs 8.8333
cc 7
nc 10
nop 1
crap 7.0178
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tokenizer;
6
7
use Spiral\Tokenizer\Exception\LocatorException;
8
9
/**
10
 * Can locate enums in a specified directory.
11
 */
12
final class EnumLocator extends AbstractLocator implements EnumsInterface
13
{
14
    public const INJECTOR = EnumLocatorInjector::class;
15
16 7
    public function getEnums(object|string|null $target = null): array
17
    {
18 7
        if (!empty($target)) {
19 4
            $target = new \ReflectionClass($target);
20
        }
21
22 7
        $result = [];
23 7
        foreach ($this->availableEnums() as $enum) {
24
            try {
25 7
                $reflection = $this->enumReflection($enum);
26 6
            } catch (LocatorException $e) {
27 6
                if ($this->debug) {
28
                    throw $e;
29
                }
30
31
                //Ignoring
32 6
                continue;
33
            }
34
35 7
            if (!$this->isTargeted($reflection, $target) || $reflection->isInterface()) {
36 4
                continue;
37
            }
38
39 7
            $result[$reflection->getName()] = $reflection;
40
        }
41
42 7
        return $result;
43
    }
44
45
    /**
46
     * Enums available in finder scope.
47
     *
48
     * @return class-string[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string[].
Loading history...
49
     */
50 7
    protected function availableEnums(): array
51
    {
52 7
        $enums = [];
53
54 7
        foreach ($this->availableReflections() as $reflection) {
55 7
            $enums = \array_merge($enums, $reflection->getEnums());
56
        }
57
58 7
        return $enums;
59
    }
60
61
    /**
62
     * Check if given enum targeted by locator.
63
     *
64
     * @param \ReflectionClass|null $target
65
     */
66 7
    protected function isTargeted(\ReflectionEnum $enum, \ReflectionClass $target = null): bool
0 ignored issues
show
Bug introduced by
The type ReflectionEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
67
    {
68 7
        if (empty($target)) {
69 3
            return true;
70
        }
71
72 4
        if (!$target->isTrait()) {
73
            //Target is interface or class
74 3
            return $enum->isSubclassOf($target) || $enum->getName() === $target->getName();
75
        }
76
77
        // Checking using traits
78 1
        return \in_array($target->getName(), $this->fetchTraits($enum->getName()));
79
    }
80
}
81