Passed
Push — master ( daa35d...1046d9 )
by Anton
01:34
created

Reader::getClassAnnotations()   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
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Spiral\Annotations;
11
12
final class Reader
13
{
14
    /** @var Parser */
15
    private $classParser;
16
17
    /** @var Parser */
18
    private $methodParser;
19
20
    /** @var Parser */
21
    private $propertyParser;
22
23
    /**
24
     * @param Parser|null $parser
25
     * @param array       $classAnnotations
26
     * @param array       $methodAnnotations
27
     * @param array       $propertyAnnotations
28
     */
29
    public function __construct(
30
        Parser $parser = null,
31
        array $classAnnotations = [],
32
        array $methodAnnotations = [],
33
        array $propertyAnnotations = []
34
    ) {
35
        $parser = $parser ?? new Parser();
36
37
        $this->classParser = clone $parser;
38
        foreach ($classAnnotations as $annotation) {
39
            $this->classParser->register($annotation);
40
        }
41
42
        $this->methodParser = clone $parser;
43
        foreach ($methodAnnotations as $annotation) {
44
            $this->methodParser->register($annotation);
45
        }
46
47
        $this->propertyParser = clone $parser;
48
        foreach ($propertyAnnotations as $annotation) {
49
            $this->propertyParser->register($annotation);
50
        }
51
    }
52
53
    /**
54
     * Get all annotations found in class doc comment.
55
     *
56
     * @param \ReflectionClass $class
57
     * @return array
58
     */
59
    public function getClassAnnotations(\ReflectionClass $class): array
60
    {
61
        return $this->classParser->parse($class->getDocComment());
0 ignored issues
show
Bug introduced by
It seems like $class->getDocComment() can also be of type boolean; however, parameter $body of Spiral\Annotations\Parser::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        return $this->classParser->parse(/** @scrutinizer ignore-type */ $class->getDocComment());
Loading history...
62
    }
63
64
    /**
65
     * Get all annotations found in class method doc comment.
66
     *
67
     * @param \ReflectionClass $class
68
     * @param string           $method
69
     * @return array
70
     *
71
     * @throws \ReflectionException
72
     */
73
    public function getMethodAnnotations(\ReflectionClass $class, string $method): array
74
    {
75
        return $this->classParser->parse($class->getMethod($method)->getDocComment());
0 ignored issues
show
Bug introduced by
It seems like $class->getMethod($method)->getDocComment() can also be of type boolean; however, parameter $body of Spiral\Annotations\Parser::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

75
        return $this->classParser->parse(/** @scrutinizer ignore-type */ $class->getMethod($method)->getDocComment());
Loading history...
76
    }
77
78
    /**
79
     * Get all annotations found in class property doc comment.
80
     *
81
     * @param \ReflectionClass $class
82
     * @param string           $property
83
     * @return array
84
     *
85
     * @throws \ReflectionException
86
     */
87
    public function getPropertyAnnotations(\ReflectionClass $class, string $property)
88
    {
89
        return $this->classParser->parse($class->getProperty($property)->getDocComment());
0 ignored issues
show
Bug introduced by
It seems like $class->getProperty($property)->getDocComment() can also be of type boolean; however, parameter $body of Spiral\Annotations\Parser::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        return $this->classParser->parse(/** @scrutinizer ignore-type */ $class->getProperty($property)->getDocComment());
Loading history...
90
    }
91
}