GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7f5f8b...23b9d8 )
by Bruno
04:20
created

AnnotationParser::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
ccs 29
cts 29
cp 1
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
namespace Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher;
20
21
use Doctrine\Common\Annotations\AnnotationReader;
22
use Doctrine\Common\Annotations\AnnotationRegistry;
23
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
24
use ReflectionClass;
25
26
class AnnotationParser implements AnnotationParserInterface
27
{
28
    /**
29
     * AnnotationReader
30
     */
31
    private $annotationReader;
32
33
    private $result;
34
35 1
    public function __construct()
36
    {
37 1
        AnnotationRegistry::registerAutoloadNamespace(
38 1
            "Doctrine\\ORM\\Mapping",
39 1
            $this->getORMAnnotationsPath()
40 1
        );
41
42 1
        AnnotationRegistry::registerFile(
43 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR . "Color.php"
44 1
        );
45 1
        AnnotationRegistry::registerFile(
46 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR . "Note.php"
47 1
        );
48
49 1
        AnnotationRegistry::registerFile(
50 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
51 1
            . "IsDisplayedMethod.php"
52 1
        );
53
54 1
        AnnotationRegistry::registerFile(
55 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
56 1
            . "ShowAttributesProperties.php"
57 1
        );
58
59 1
        AnnotationRegistry::registerFile(
60 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
61 1
            . "HideAttributesProperties.php"
62 1
        );
63
64 1
        $this->annotationReader = new AnnotationReader();
65
66 1
        $this->result = array(
67 1
            'colors' => array(),
68 1
            'notes' => array()
69 1
        );
70 1
    }
71
72
73
    /**
74
     * @param ClassMetadata[] $metadata
75
     * @return array
76
     */
77 6
    public function getAnnotations($metadata)
78
    {
79 6
        foreach ($metadata as $class) {
80 6
            $this->setClassAnnotations($class->getName());
81 4
        }
82
83 4
        return $this->result;
84
    }
85
86
    /**
87
     * @param string $className
88
     * @return null|string
89
     */
90 3
    public function getClassDisplay($className)
91
    {
92 3
        if (class_exists($className)) {
93 2
            return $this->getClassDisplayAnnotations($className);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getClassDisplayAnnotations($className); of type null|string adds the type string to the return on line 93 which is incompatible with the return type declared by the interface Onurb\Doctrine\ORMMetada...erface::getClassDisplay of type boolean.
Loading history...
94
        }
95
96 1
        return null;
97
    }
98
99
    /**
100
     * @param string $className
101
     * @return array
102
     */
103 2
    public function getClassMethodsAnnotations($className)
104
    {
105 2
        $methods = array();
106
107 2
        if (class_exists($className)) {
108 1
            return $this->getMethods($className);
109
        }
110
111 1
        return $methods;
112
    }
113
114
    /**
115
     * @param string $className
116
     */
117 6
    private function setClassAnnotations($className)
118
    {
119 6
        if (class_exists($className)) {
120 6
            $this->setClassColor($className);
121 4
            $this->setClassNote($className);
122 4
        }
123 4
    }
124
125
    /**
126
     * @param string $className
127
     */
128 6
    private function setClassColor($className)
129
    {
130 6
        $color = $this->annotationReader->getClassAnnotation(
131 6
            new ReflectionClass(new $className),
132
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\Color"
133 6
        );
134
135 4
        if (null !== $color) {
136 3
            $this->result['colors'][$className] = $color->value;
137 3
        }
138 4
    }
139
140
    /**
141
     * @param string $className
142
     */
143 4
    private function setClassNote($className)
144
    {
145 4
        $note = $this->annotationReader->getClassAnnotation(
146 4
            new ReflectionClass(new $className),
147
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\Note"
148 4
        );
149
150 4
        if (null !== $note) {
151 2
            $this->result['notes'][$className] = array(
152 2
                'value' => $note->value,
153 2
                'color' => $note->color
154 2
            );
155 2
        }
156 4
    }
157
158
    /**
159
     * @return string
160
     */
161 1
    private function getORMAnnotationsPath()
162
    {
163 1
        $reflector = new ReflectionClass('Doctrine\\ORM\\Mapping\\Entity');
164 1
        $entityFilename = $reflector->getFileName();
165 1
        $entityPathArray = explode('\\', $entityFilename);
166
167 1
        unset($entityPathArray[count($entityPathArray) - 1]);
168
169 1
        return implode(DIRECTORY_SEPARATOR, $entityPathArray)
170 1
            . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'
171 1
            . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
172
    }
173
174
    /**
175
     * @param $className
176
     * @return array
177
     */
178 1
    private function getMethods($className)
179
    {
180 1
        $methods = array();
181 1
        $entityReflectionClass = new ReflectionClass(new $className);
182
183 1
        foreach ($entityReflectionClass->getMethods() as $method) {
184 1
            $methodAnnotation = $this->annotationReader->getMethodAnnotation(
185 1
                $method,
186
                "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\IsDisplayedMethod"
187 1
            );
188
189 1
            if (null !== $methodAnnotation) {
190 1
                $methods[] = $method->getName();
191 1
            }
192 1
        }
193
194 1
        return $methods;
195
    }
196
197
    /**
198
     * @param $className
199
     * @return null|string
200
     * @throws \Exception
201
     */
202 2
    private function getClassDisplayAnnotations($className)
203
    {
204 2
        $hide = $this->annotationReader->getClassAnnotation(
205 2
            new ReflectionClass($className),
206
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\HideAttributesProperties"
207 2
        );
208
209 2
        $show = $this->annotationReader->getClassAnnotation(
210 2
            new ReflectionClass($className),
211
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\ShowAttributesProperties"
212 2
        );
213
214 2
        $this->checkDisplayAnnotationValidity($hide, $show);
215
216 1
        return !$hide && !$show ? null : ($hide ? 'hide' : 'show');
217
    }
218
219
    /**
220
     * @param Object|null $hide
221
     * @param Object|null $show
222
     * @throws \Exception
223
     */
224 2
    private function checkDisplayAnnotationValidity($hide, $show)
225
    {
226 2
        if ($hide && $show) {
227 1
            throw new \Exception('Annotations HideAttributesProperties and ShowAttributesProperties '
228 1
            . 'can\'t be used together');
229
        }
230 1
    }
231
}
232