AnnotationLocator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 95
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findClasses() 0 6 3
A findMethods() 0 7 4
A __construct() 0 4 1
A findProperties() 0 7 4
A withTargets() 0 6 1
A getTargets() 0 9 3
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Annotations;
13
14
use Spiral\Attributes\AnnotationReader;
15
use Spiral\Attributes\Factory;
16
use Spiral\Attributes\ReaderInterface;
17
use Spiral\Core\Container\SingletonInterface;
18
use Spiral\Tokenizer\ClassesInterface;
19
20
/**
21
 * Locate all available annotations for methods, classes and properties across all the codebase.
22
 *
23
 * @deprecated since v2.9. Please, use combination of {@see ClassesInterface} and {@see ReaderInterface}
24
 */
25
final class AnnotationLocator implements SingletonInterface
26
{
27
    /** @var ClassesInterface */
28
    private $classLocator;
29
30
    /** @var ReaderInterface */
31
    private $reader;
32
33
    /** @var array */
34
    private $targets = [];
35
36
    /**
37
     * AnnotationLocator constructor.
38
     *
39
     * @param ReaderInterface|null $reader
40
     * @throws \Doctrine\Common\Annotations\AnnotationException
41
     */
42
    public function __construct(ClassesInterface $classLocator, ReaderInterface $reader = null)
43
    {
44
        $this->classLocator = $classLocator;
45
        $this->reader = $reader ?? (new Factory())->create();
46
    }
47
48
    /**
49
     * Limit locator to only specific class types.
50
     */
51
    public function withTargets(array $targets): self
52
    {
53
        $locator = clone $this;
54
        $locator->targets = $targets;
55
56
        return $locator;
57
    }
58
59
    /**
60
     * Find all classes with given annotation.
61
     *
62
     * @return iterable|AnnotatedClass[]
63
     */
64
    public function findClasses(string $annotation): iterable
65
    {
66
        foreach ($this->getTargets() as $target) {
67
            $found = $this->reader->firstClassMetadata($target, $annotation);
68
            if ($found !== null) {
69
                yield new AnnotatedClass($target, $found);
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new Spiral\Annotat...dClass($target, $found) returns the type Generator which is incompatible with the documented return type Spiral\Annotations\AnnotatedClass[]|iterable.
Loading history...
Deprecated Code introduced by
The class Spiral\Annotations\AnnotatedClass has been deprecated: since v2.12. Will be removed in v3.0 ( Ignorable by Annotation )

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

69
                yield /** @scrutinizer ignore-deprecated */ new AnnotatedClass($target, $found);
Loading history...
70
            }
71
        }
72
    }
73
74
    /**
75
     * Find all methods with given annotation.
76
     *
77
     * @return iterable|AnnotatedMethod[]
78
     */
79
    public function findMethods(string $annotation): iterable
80
    {
81
        foreach ($this->getTargets() as $target) {
82
            foreach ($target->getMethods() as $method) {
83
                $found = $this->reader->firstFunctionMetadata($method, $annotation);
84
                if ($found !== null) {
85
                    yield new AnnotatedMethod($method, $found);
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new Spiral\Annotat...Method($method, $found) returns the type Generator which is incompatible with the documented return type Spiral\Annotations\AnnotatedMethod[]|iterable.
Loading history...
Deprecated Code introduced by
The class Spiral\Annotations\AnnotatedMethod has been deprecated: since v2.12. Will be removed in v3.0 ( Ignorable by Annotation )

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

85
                    yield /** @scrutinizer ignore-deprecated */ new AnnotatedMethod($method, $found);
Loading history...
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * Find all properties with given annotation.
93
     *
94
     * @return iterable|AnnotatedProperty[]
95
     */
96
    public function findProperties(string $annotation): iterable
97
    {
98
        foreach ($this->getTargets() as $target) {
99
            foreach ($target->getProperties() as $property) {
100
                $found = $this->reader->firstPropertyMetadata($property, $annotation);
101
                if ($found !== null) {
102
                    yield new AnnotatedProperty($property, $found);
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new Spiral\Annotat...erty($property, $found) returns the type Generator which is incompatible with the documented return type Spiral\Annotations\AnnotatedProperty[]|iterable.
Loading history...
Deprecated Code introduced by
The class Spiral\Annotations\AnnotatedProperty has been deprecated: since v2.12. Will be removed in v3.0 ( Ignorable by Annotation )

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

102
                    yield /** @scrutinizer ignore-deprecated */ new AnnotatedProperty($property, $found);
Loading history...
103
                }
104
            }
105
        }
106
    }
107
108
    /**
109
     * @return iterable|\ReflectionClass[]
110
     */
111
    private function getTargets(): iterable
112
    {
113
        if ($this->targets === []) {
114
            yield from $this->classLocator->getClasses();
0 ignored issues
show
Bug Best Practice introduced by
The expression YieldFromNode returns the type Generator which is incompatible with the documented return type ReflectionClass[]|iterable.
Loading history...
115
            return;
116
        }
117
118
        foreach ($this->targets as $target) {
119
            yield from $this->classLocator->getClasses($target);
120
        }
121
    }
122
}
123