Test Failed
Pull Request — master (#947)
by
unknown
08:47 queued 01:08
created

InterfaceLocator::availableInterfaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
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
    public function getInterfaces(string|null $target = null): array
17
    {
18
        if (! empty($target)) {
19
            $target = new \ReflectionClass($target);
20
        }
21
22
        $result = [];
23
        foreach ($this->availableInterfaces() as $interface) {
24
            try {
25
                $reflection = $this->classReflection($interface);
26
            } catch (LocatorException $e) {
27
                if ($this->debug) {
28
                    throw $e;
29
                }
30
31
                //Ignoring
32
                continue;
33
            }
34
35
            if (! $this->isTargeted($reflection, $target)) {
36
                continue;
37
            }
38
39
            $result[$reflection->getName()] = $reflection;
40
        }
41
42
        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
    protected function availableInterfaces(): array
51
    {
52
        $interfaces = [];
53
54
        foreach ($this->availableReflections() as $reflection) {
55
            $interfaces = \array_merge($interfaces, $reflection->getInterfaces());
56
        }
57
58
        return $interfaces;
59
    }
60
61
    /**
62
     * Check if given class targeted by locator.
63
     *
64
     * @param \ReflectionClass|null $target
65
     */
66
    protected function isTargeted(\ReflectionClass $class, \ReflectionClass $target = null): bool
67
    {
68
        if (empty($target)) {
69
            return true;
70
        }
71
72
        return $class->isSubclassOf($target) || $class->getName() === $target->getName();
73
    }
74
}
75