Test Failed
Pull Request — master (#878)
by butschster
09:10 queued 55s
created

TargetClass::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tokenizer\Attribute;
6
7
use Spiral\Attributes\NamedArgumentConstructor;
8
use Spiral\Tokenizer\TokenizationListenerInterface;
9
use Spiral\Tokenizer\Traits\TargetTrait;
10
11
/**
12
 * When applied to a listener, this attribute will instruct the tokenizer to listen for classes that are extending or
13
 * implementing the given class or have the given trait.
14
 * @see TokenizationListenerInterface
15
 */
16
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE), NamedArgumentConstructor]
17
final class TargetClass implements ListenerDefinitionInterface
18
{
19
    use TargetTrait;
20
21
    /**
22
     * @param class-string $class
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...
23
     * @param non-empty-string|null $scope
24
     */
25
    public function __construct(
26
        public readonly string $class,
27
        public readonly ?string $scope = null,
28
    ) {
29
    }
30
31
    public function filter(array $classes): \Generator
32
    {
33
        $target = new \ReflectionClass($this->class);
34
35
        foreach ($classes as $class) {
36
            if (!$target->isTrait()) {
37
                if ($class->isSubclassOf($target) || $class->getName() === $target->getName()) {
38
                    yield $class->getName();
39
                }
40
41
                continue;
42
            }
43
44
            // Checking using traits
45
            if (\in_array($target->getName(), $this->fetchTraits($class->getName()), true)) {
46
                yield $class->getName();
47
            }
48
        }
49
    }
50
51
    public function getCacheKey(): string
52
    {
53
        return \md5($this->class . $this->scope);
54
    }
55
56
    public function getScope(): ?string
57
    {
58
        return $this->scope;
59
    }
60
}
61