Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

RawAnnotationReader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 10.81%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 139
ccs 4
cts 37
cp 0.1081
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setImports() 0 8 2
A addNamespace() 0 4 1
A getClassAnnotations() 0 6 1
A getMethodAnnotations() 0 6 1
A getPropertyAnnotations() 0 6 1
A getClassAnnotation() 0 10 3
A getMethodAnnotation() 0 10 3
A getPropertyAnnotation() 0 10 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2011, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument;
12
13
use TokenReflection\ReflectionClass as ParsedReflectionClass;
14
use TokenReflection\ReflectionMethod as ParsedReflectionMethod;
15
use TokenReflection\ReflectionProperty as ParsedReflectionProperty;
16
use Doctrine\Common\Annotations\DocParser;
17
use Doctrine\Common\Annotations\Annotation\Target;
18
19
/**
20
 * Simple Raw Annotation Reader.
21
 *
22
 * This annotation reader is intended to be used in projects where you have
23
 * full-control over all annotations that are available.
24
 *
25
 * @since  2.2
26
 * @author Johannes M. Schmitt <[email protected]>
27
 * @author Fabio B. Silva <[email protected]>
28
 */
29
class RawAnnotationReader
30
{
31
    /**
32
     * @var DocParser
33
     */
34
    private $parser;
35
36
    /**
37
     * Constructor.
38
     *
39
     * Initializes a new SimpleAnnotationReader.
40
     */
41 30
    public function __construct()
42
    {
43 30
        $this->parser = new DocParser();
44 30
        $this->parser->setIgnoreNotImportedAnnotations(true);
45 30
    }
46
47
    /**
48
     * Set imports for annotations
49
     *
50
     * @param array $imports
51
     */
52
    public function setImports(array $imports)
53
    {
54
        $convertedImports = [];
55
        foreach ($imports as $aliasName=>$fullName) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
56
            $convertedImports[strtolower($aliasName)] = $fullName;
57
        }
58
        $this->parser->setImports($convertedImports);
59
    }
60
61
    /**
62
     * Adds a namespace in which we will look for annotations.
63
     *
64
     * @param string $namespace
65
     */
66
    public function addNamespace($namespace)
67
    {
68
        $this->parser->addNamespace($namespace);
0 ignored issues
show
Documentation introduced by
$namespace is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    /**
72
     * Gets the annotations applied to a class.
73
     *
74
     * @param ParsedReflectionClass $class The ReflectionClass of the class from which
75
     *                               the class annotations should be read.
76
     * @return array An array of Annotations.
77
     */
78
    public function getClassAnnotations(ParsedReflectionClass $class)
79
    {
80
        $this->parser->setTarget(Target::TARGET_CLASS);
81
82
        return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
83
    }
84
85
    /**
86
     * Gets the annotations applied to a method.
87
     *
88
     * @param ParsedReflectionMethod $method The ReflectionMethod of the method from which
89
     *                                   the annotations should be read.
90
     * @return array An array of Annotations.
91
     */
92
    public function getMethodAnnotations(ParsedReflectionMethod $method)
93
    {
94
        $this->parser->setTarget(Target::TARGET_METHOD);
95
96
        return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
97
    }
98
99
    /**
100
     * Gets the annotations applied to a property.
101
     *
102
     * @param ParsedReflectionProperty $property The ReflectionProperty of the property
103
     *                                     from which the annotations should be read.
104
     * @return array An array of Annotations.
105
     */
106
    public function getPropertyAnnotations(ParsedReflectionProperty $property)
107
    {
108
        $this->parser->setTarget(Target::TARGET_PROPERTY);
109
110
        return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
111
    }
112
113
    /**
114
     * Gets a class annotation.
115
     *
116
     * @param ParsedReflectionClass $class The ReflectionClass of the class from which
117
     *                               the class annotations should be read.
118
     * @param string $annotationName The name of the annotation.
119
     * @return The Annotation or NULL, if the requested annotation does not exist.
120
     */
121
    public function getClassAnnotation(ParsedReflectionClass $class, $annotationName)
122
    {
123
        foreach ($this->getClassAnnotations($class) as $annot) {
124
            if ($annot instanceof $annotationName) {
125
                return $annot;
126
            }
127
        }
128
129
        return null;
130
    }
131
132
    /**
133
     * Gets a method annotation.
134
     *
135
     * @param ParsedReflectionMethod $method
136
     * @param string $annotationName The name of the annotation.
137
     * @return The Annotation or NULL, if the requested annotation does not exist.
138
     */
139
    public function getMethodAnnotation(ParsedReflectionMethod $method, $annotationName)
140
    {
141
        foreach ($this->getMethodAnnotations($method) as $annot) {
142
            if ($annot instanceof $annotationName) {
143
                return $annot;
144
            }
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * Gets a property annotation.
152
     *
153
     * @param ParsedReflectionProperty $property
154
     * @param string $annotationName The name of the annotation.
155
     * @return The Annotation or NULL, if the requested annotation does not exist.
156
     */
157
    public function getPropertyAnnotation(ParsedReflectionProperty $property, $annotationName)
158
    {
159
        foreach ($this->getPropertyAnnotations($property) as $annot) {
160
            if ($annot instanceof $annotationName) {
161
                return $annot;
162
            }
163
        }
164
165
        return null;
166
    }
167
}
168