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 |
||
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) |
||
73 | |||
74 | /** |
||
75 | * Create annotations from entity |
||
76 | * @return ArrayObject |
||
77 | */ |
||
78 | public function create() |
||
98 | |||
99 | /** |
||
100 | * @param $entity |
||
101 | * @return ArrayObject |
||
102 | * @internal param $spec |
||
103 | */ |
||
104 | protected function getSpecification($entity) |
||
126 | |||
127 | /** |
||
128 | * @param AnnotationCollection $annotations |
||
129 | * @param ClassReflection $reflection |
||
130 | * @param ArrayObject $spec |
||
131 | */ |
||
132 | protected function configureEntity($annotations, $reflection, $spec) |
||
150 | |||
151 | /** |
||
152 | * @param AnnotationCollection $annotations |
||
153 | * @param PropertyReflection $reflection |
||
154 | * @param ArrayObject $spec |
||
155 | */ |
||
156 | protected function configureProperty($annotations, $reflection, $spec) |
||
181 | |||
182 | /** |
||
183 | * Set event manager instance |
||
184 | * |
||
185 | * @param EventManagerInterface $events |
||
186 | * @return AnnotationBuilder |
||
187 | */ |
||
188 | public function setEventManager(EventManagerInterface $events) |
||
199 | |||
200 | /** |
||
201 | * Get event manager |
||
202 | * |
||
203 | * @return EventManagerInterface |
||
204 | */ |
||
205 | public function getEventManager() |
||
212 | |||
213 | /** |
||
214 | * @return mixed |
||
215 | */ |
||
216 | public function getAnnotationManager() |
||
224 | |||
225 | /** |
||
226 | * @param mixed $annotationManager |
||
227 | */ |
||
228 | public function setAnnotationManager(AnnotationManager $annotationManager) |
||
238 | |||
239 | /** |
||
240 | * @return \Zend\Code\Annotation\Parser\DoctrineAnnotationParser |
||
241 | */ |
||
242 | public function getAnnotationParser() |
||
250 | } |
||
251 |
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.