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 ( 621097...e4ba91 )
by Bruno
03:34
created

AnnotationParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 6
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
        $this->registerAnnotations();
38
39 1
        $this->annotationReader = new AnnotationReader();
40
41 1
        $this->result = array(
42 1
            'colors' => array(),
43 1
            'notes' => array()
44 1
        );
45 1
    }
46
47
48
    /**
49
     * @param ClassMetadata[] $metadata
50
     * @return array
51
     */
52 6
    public function getAnnotations($metadata)
53
    {
54 6
        foreach ($metadata as $class) {
55 6
            $this->setClassAnnotations($class->getName());
56 6
        }
57
58 6
        return $this->result;
59
    }
60
61
    /**
62
     * @param string $className
63
     * @return null|string
64
     */
65 3
    public function getClassDisplay($className)
66
    {
67 3
        if (class_exists($className)) {
68 2
            return $this->getClassDisplayAnnotations($className);
69
        }
70
71 1
        return null;
72
    }
73
74
    /**
75
     * @param string $className
76
     * @return array
77
     */
78 3
    public function getClassMethodsAnnotations($className)
79
    {
80 3
        $methods = array();
81
82 3
        if (class_exists($className)) {
83 2
            return $this->getMethods($className);
84
        }
85
86 1
        return $methods;
87
    }
88
89
    /**
90
     * @param string $className
91
     */
92 6
    private function setClassAnnotations($className)
93
    {
94 6
        if (class_exists($className)) {
95 6
            $this->setClassColor($className);
96 6
            $this->setClassNote($className);
97 6
        }
98 6
    }
99
100
    /**
101
     * @param string $className
102
     */
103 6
    private function setClassColor($className)
104
    {
105 6
        $color = $this->annotationReader->getClassAnnotation(
106 6
            new ReflectionClass(new $className),
107
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\Color"
108 6
        );
109
110 6
        if (null !== $color) {
111 5
            $this->result['colors'][$className] = $color->value;
112 5
        }
113 6
    }
114
115
    /**
116
     * @param string $className
117
     */
118 6
    private function setClassNote($className)
119
    {
120 6
        $note = $this->annotationReader->getClassAnnotation(
121 6
            new ReflectionClass(new $className),
122
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\Note"
123 6
        );
124
125 6
        if (null !== $note) {
126 2
            $this->result['notes'][$className] = array(
127 2
                'value' => $note->value,
128 2
                'color' => $note->color
129 2
            );
130 2
        }
131 6
    }
132
133
    /**
134
     * @return string
135
     */
136 1
    private function getORMAnnotationsPath()
137
    {
138 1
        $reflector = new ReflectionClass('Doctrine\\ORM\\Mapping\\Entity');
139 1
        $entityFilename = $reflector->getFileName();
140 1
        $entityPathArray = explode('\\', $entityFilename);
141
142 1
        unset($entityPathArray[count($entityPathArray) - 1]);
143
144 1
        return implode(DIRECTORY_SEPARATOR, $entityPathArray)
145 1
            . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'
146 1
            . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
147
    }
148
149
    /**
150
     * @param string $className
151
     * @return array
152
     */
153 2
    private function getMethods($className)
154
    {
155 2
        $methods = array();
156 2
        $entityReflectionClass = new ReflectionClass(new $className);
157
158 2
        foreach ($entityReflectionClass->getMethods() as $method) {
159 2
            $methodAnnotation = $this->annotationReader->getMethodAnnotation(
160 2
                $method,
161
                "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\IsDisplayedMethod"
162 2
            );
163
164 2
            if (null !== $methodAnnotation) {
165 2
                $methods[] = $method->getName();
166 2
            }
167 2
        }
168
169 2
        return $methods;
170
    }
171
172
    /**
173
     * @param $className
174
     * @return null|string
175
     * @throws \Exception
176
     */
177 2
    private function getClassDisplayAnnotations($className)
178
    {
179 2
        $hide = $this->annotationReader->getClassAnnotation(
180 2
            new ReflectionClass($className),
181
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\HideAttributesProperties"
182 2
        );
183
184 2
        $show = $this->annotationReader->getClassAnnotation(
185 2
            new ReflectionClass($className),
186
            "Onurb\\Doctrine\\ORMMetadataGrapher\\Mapping\\ShowAttributesProperties"
187 2
        );
188
189 2
        $this->checkDisplayAnnotationValidity($hide, $show);
190
191 1
        return !$hide && !$show ? null : ($hide ? 'hide' : 'show');
192
    }
193
194
    /**
195
     * @param Object|null $hide
196
     * @param Object|null $show
197
     * @throws \Exception
198
     */
199 2
    private function checkDisplayAnnotationValidity($hide, $show)
200
    {
201 2
        if ($hide && $show) {
202 1
            throw new \Exception('Annotations HideAttributesProperties and ShowAttributesProperties '
203 1
            . 'can\'t be used together');
204
        }
205 1
    }
206
207 1
    private function registerAnnotations()
208
    {
209 1
        AnnotationRegistry::registerAutoloadNamespace(
210 1
            "Doctrine\\ORM\\Mapping",
211 1
            $this->getORMAnnotationsPath()
212 1
        );
213
214 1
        AnnotationRegistry::registerFile(
215 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR . "Color.php"
216 1
        );
217 1
        AnnotationRegistry::registerFile(
218 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR . "Note.php"
219 1
        );
220
221 1
        AnnotationRegistry::registerFile(
222 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
223 1
            . "IsDisplayedMethod.php"
224 1
        );
225
226 1
        AnnotationRegistry::registerFile(
227 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
228 1
            . "ShowAttributesProperties.php"
229 1
        );
230
231 1
        AnnotationRegistry::registerFile(
232 1
            __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Mapping" . DIRECTORY_SEPARATOR
233 1
            . "HideAttributesProperties.php"
234 1
        );
235 1
    }
236
}
237