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.

AnnotationBuilder   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 229
Duplicated Lines 9.61 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 23
c 2
b 0
f 1
lcom 1
cbo 11
dl 22
loc 229
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 20 4
B getSpecification() 0 22 4
A configureEntity() 11 18 2
B configureProperty() 11 25 3
A setEventManager() 0 11 1
A getEventManager() 0 7 2
A getAnnotationManager() 0 8 2
A setAnnotationManager() 0 10 2
A getAnnotationParser() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Newage\Annotations\Entity\Annotation;
4
5
use Newage\Annotations\Config\AnnotationBuilderConfig;
6
use Newage\Annotations\Mapper\Annotation\EntityAnnotationListener;
7
use Newage\Annotations\Mapper\Annotation\PropertyAnnotationListener;
8
use Newage\Annotations\Mapper\MapperBuilder;
9
use Zend\Code\Annotation\AnnotationCollection;
10
use Zend\Code\Annotation\AnnotationManager;
11
use Zend\Code\Annotation\Parser;
12
use Zend\Code\Reflection\ClassReflection;
13
use Zend\Code\Reflection\PropertyReflection;
14
use Zend\EventManager\EventManager;
15
use Zend\EventManager\EventManagerAwareInterface;
16
use Zend\EventManager\EventManagerInterface;
17
use Zend\Stdlib\ArrayObject;
18
19
/**
20
 * @package Newage\Annotations\Mapper\Annotation
21
 */
22
class AnnotationBuilder implements EventManagerAwareInterface, AnnotationBuilderInterface
23
{
24
    /**
25
     * @var MapperBuilder
26
     */
27
    protected $mapperBuilder;
28
29
    /**
30
     * @var Parser\DoctrineAnnotationParser
31
     */
32
    protected $annotationParser;
33
34
    /**
35
     * @var AnnotationManager
36
     */
37
    protected $annotationManager;
38
39
    /**
40
     * @var EventManagerInterface
41
     */
42
    protected $events;
43
44
    /**
45
     * @var AnnotationBuilderConfig
46
     */
47
    private $config;
48
49
    /**
50
     * @var array Default annotations to register
51
     */
52
    protected $defaultAnnotations = [
53
        'Table',
54
        'Id',
55
        'Column',
56
        'OneToOne',
57
        'OneToMany',
58
        'ManyToOne',
59
        'ManyToMany',
60
    ];
61
62
    /**
63
     * AnnotationBuilder constructor.
64
     *
65
     * @param AnnotationBuilderConfig $config
66
     * @param MapperBuilder           $mapperBuilder
67
     */
68
    public function __construct(AnnotationBuilderConfig $config, MapperBuilder $mapperBuilder)
69
    {
70
        $this->mapperBuilder = $mapperBuilder;
71
        $this->config = $config;
72
    }
73
74
    /**
75
     * Create annotations from entity
76
     * @return ArrayObject
77
     */
78
    public function create()
79
    {
80
        $spec = new ArrayObject(['entities' => []]);
81
        $factory = $this->mapperBuilder;
82
        $modelsOptions = $this->config->getConfig('models');
83
        foreach ($modelsOptions as $modelOption) {
84
            foreach (new \FilesystemIterator($modelOption['path']) as $file) {
85
                $entityName = substr($file->getFileName(), 0, -4);
86
                $entityNamespace = '\\' . $modelOption['namespace'] . '\\' . $entityName;
87
88
                if (!class_exists($entityNamespace)) {
89
                    continue;
90
                }
91
                
92
                $entity = new $entityNamespace();
93
                $spec['entities'][] = $this->getSpecification($entity);
94
            }
95
        };
96
        $factory->create($spec);
97
    }
98
99
    /**
100
     * @param $entity
101
     * @return ArrayObject
102
     * @internal param $spec
103
     */
104
    protected function getSpecification($entity)
105
    {
106
        $annotationManager = $this->getAnnotationManager();
107
108
        $spec = new ArrayObject();
109
        $reflection  = new ClassReflection($entity);
110
        $annotations = $reflection->getAnnotations($annotationManager);
111
112
        if ($annotations instanceof AnnotationCollection) {
113
            $this->configureEntity($annotations, $reflection, $spec);
114
        }
115
116
        foreach ($reflection->getProperties() as $property) {
117
            $annotations = $property->getAnnotations($annotationManager);
118
119
            if ($annotations instanceof AnnotationCollection) {
120
                $this->configureProperty($annotations, $property, $spec);
121
            }
122
        }
123
124
        return $spec;
125
    }
126
127
    /**
128
     * @param AnnotationCollection $annotations
129
     * @param ClassReflection      $reflection
130
     * @param ArrayObject          $spec
131
     */
132
    protected function configureEntity($annotations, $reflection, $spec)
133
    {
134
        $name = $reflection->getName();
135
        $spec['entity'] = $name;
136
137
        $events = $this->getEventManager();
138 View Code Duplication
        foreach ($annotations as $annotation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            $events->trigger(
140
                __FUNCTION__,
141
                $this,
142
                [
143
                    'annotation' => $annotation,
144
                    'spec'       => $spec,
145
                    'name'       => $name
146
                ]
147
            );
148
        }
149
    }
150
151
    /**
152
     * @param AnnotationCollection $annotations
153
     * @param PropertyReflection   $reflection
154
     * @param ArrayObject          $spec
155
     */
156
    protected function configureProperty($annotations, $reflection, $spec)
157
    {
158
        $name = $reflection->getName();
159
        $propertySpec = new ArrayObject([
160
            'property' => $name
161
        ]);
162
163
        $events = $this->getEventManager();
164 View Code Duplication
        foreach ($annotations as $annotation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
            $events->trigger(
166
                __FUNCTION__,
167
                $this,
168
                [
169
                    'annotation' => $annotation,
170
                    'spec'       => $propertySpec,
171
                    'name'       => $name
172
                ]
173
            );
174
        }
175
176
        if (!isset($spec['properties'])) {
177
            $spec['properties'] = [];
178
        }
179
        $spec['properties'][] = $propertySpec;
180
    }
181
182
    /**
183
     * Set event manager instance
184
     *
185
     * @param  EventManagerInterface $events
186
     * @return AnnotationBuilder
187
     */
188
    public function setEventManager(EventManagerInterface $events)
189
    {
190
        $events->setIdentifiers([
191
            __CLASS__,
192
            get_class($this),
193
        ]);
194
        $events->attach(new EntityAnnotationListener());
0 ignored issues
show
Documentation introduced by
new \Newage\Annotations\...ityAnnotationListener() is of type object<Newage\Annotation...tityAnnotationListener>, but the function expects a string.

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...
195
        $events->attach(new PropertyAnnotationListener());
0 ignored issues
show
Documentation introduced by
new \Newage\Annotations\...rtyAnnotationListener() is of type object<Newage\Annotation...ertyAnnotationListener>, but the function expects a string.

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...
196
        $this->events = $events;
197
        return $this;
198
    }
199
200
    /**
201
     * Get event manager
202
     *
203
     * @return EventManagerInterface
204
     */
205
    public function getEventManager()
206
    {
207
        if (null === $this->events) {
208
            $this->setEventManager(new EventManager());
209
        }
210
        return $this->events;
211
    }
212
213
    /**
214
     * @return mixed
215
     */
216
    public function getAnnotationManager()
217
    {
218
        if (null === $this->annotationManager) {
219
            $this->setAnnotationManager(new AnnotationManager());
220
        }
221
222
        return $this->annotationManager;
223
    }
224
225
    /**
226
     * @param mixed $annotationManager
227
     */
228
    public function setAnnotationManager(AnnotationManager $annotationManager)
229
    {
230
        $parser = $this->getAnnotationParser();
231
        foreach ($this->defaultAnnotations as $annotationName) {
232
            $class = __NAMESPACE__ . '\\' . $annotationName;
233
            $parser->registerAnnotation($class);
234
        }
235
        $annotationManager->attach($parser);
236
        $this->annotationManager = $annotationManager;
237
    }
238
239
    /**
240
     * @return \Zend\Code\Annotation\Parser\DoctrineAnnotationParser
241
     */
242
    public function getAnnotationParser()
243
    {
244
        if (null === $this->annotationParser) {
245
            $this->annotationParser = new Parser\DoctrineAnnotationParser();
246
        }
247
248
        return $this->annotationParser;
249
    }
250
}
251