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

InterfaceLocator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 23
c 1
b 0
f 0
dl 0
loc 61
ccs 22
cts 23
cp 0.9565
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A availableInterfaces() 0 9 2
A isTargeted() 0 7 3
A getInterfaces() 0 27 6
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 classes in a specified directory.
11
 */
12
final class InterfaceLocator extends AbstractLocator implements InterfacesInterface
13
{
14
    public const INJECTOR = InterfaceLocatorInjector::class;
15
16 5
    public function getInterfaces(string|null $target = null): array
17
    {
18 5
        if (!empty($target)) {
19 2
            $target = new \ReflectionClass($target);
20
        }
21
22 5
        $result = [];
23 5
        foreach ($this->availableInterfaces() as $interface) {
24
            try {
25 5
                $reflection = $this->classReflection($interface);
26 4
            } catch (LocatorException $e) {
27 4
                if ($this->debug) {
28
                    throw $e;
29
                }
30
31
                //Ignoring
32 4
                continue;
33
            }
34
35 5
            if (!$this->isTargeted($reflection, $target)) {
36 2
                continue;
37
            }
38
39 5
            $result[$reflection->getName()] = $reflection;
40
        }
41
42 5
        return $result;
43
    }
44
45
    /**
46
     * Classes 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 5
    protected function availableInterfaces(): array
51
    {
52 5
        $interfaces = [];
53
54 5
        foreach ($this->availableReflections() as $reflection) {
55 5
            $interfaces = \array_merge($interfaces, $reflection->getInterfaces());
56
        }
57
58 5
        return $interfaces;
59
    }
60
61
    /**
62
     * Check if given class targeted by locator.
63
     *
64
     * @param \ReflectionClass|null $target
65
     */
66 5
    protected function isTargeted(\ReflectionClass $class, \ReflectionClass $target = null): bool
67
    {
68 5
        if (empty($target)) {
69 3
            return true;
70
        }
71
72 2
        return $class->isSubclassOf($target) || $class->getName() === $target->getName();
73
    }
74
}
75