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 |
||
27 | class LdapObjectSubscriber implements EventSubscriber |
||
28 | { |
||
29 | /** |
||
30 | * The annotation class to check for. |
||
31 | */ |
||
32 | const ANNOTATION = 'LdapTools\Bundle\LdapToolsBundle\Annotation\LdapObject'; |
||
33 | |||
34 | /** |
||
35 | * @var Reader |
||
36 | */ |
||
37 | protected $reader; |
||
38 | |||
39 | /** |
||
40 | * @var LdapManager |
||
41 | */ |
||
42 | protected $ldap; |
||
43 | |||
44 | /** |
||
45 | * @param Reader $reader |
||
46 | * @param LdapManager $ldap |
||
47 | */ |
||
48 | public function __construct(Reader $reader, LdapManager $ldap) |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getSubscribedEvents() |
||
65 | |||
66 | /** |
||
67 | * @param LifecycleEventArgs $args |
||
68 | */ |
||
69 | public function preUpdate(LifecycleEventArgs $args) |
||
73 | |||
74 | /** |
||
75 | * @param LifecycleEventArgs $args |
||
76 | */ |
||
77 | public function prePersist(LifecycleEventArgs $args) |
||
81 | |||
82 | /** |
||
83 | * @param LifecycleEventArgs $args |
||
84 | */ |
||
85 | View Code Duplication | public function postLoad(LifecycleEventArgs $args) |
|
97 | |||
98 | /** |
||
99 | * Handles transforming the value as it currently is to the value the DB expects. |
||
100 | * |
||
101 | * @param LifecycleEventArgs $args |
||
102 | */ |
||
103 | View Code Duplication | protected function transformValueForDb(LifecycleEventArgs $args) |
|
115 | |||
116 | /** |
||
117 | * Get all the properties from the entity that have a LdapObject annotation defined. |
||
118 | * |
||
119 | * @param object $entity |
||
120 | * @param ObjectManager $om |
||
121 | * @return array |
||
122 | */ |
||
123 | protected function getLdapObjectAnnotationProperties($entity, $om) |
||
137 | |||
138 | /** |
||
139 | * Based on an array of IDs for LDAP objects, set the property to either a LdapObject for LdapObjectCollection. |
||
140 | * |
||
141 | * @param \ReflectionProperty $property |
||
142 | * @param LdapObjectAnnotation $annotation |
||
143 | * @param $entity |
||
144 | */ |
||
145 | protected function setLdapObjectForProperty(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity) |
||
163 | |||
164 | /** |
||
165 | * Get the specified ID for each LDAP object and save it to an array. |
||
166 | * |
||
167 | * @param \ReflectionProperty $property |
||
168 | * @param LdapObjectAnnotation $annotation |
||
169 | * @param $entity |
||
170 | */ |
||
171 | protected function setLdapValueForProperty(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity) |
||
191 | |||
192 | /** |
||
193 | * @param LdapObjectAnnotation $annotation |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function getLdapAttributesToSelect(LdapObjectAnnotation $annotation) |
||
214 | |||
215 | /** |
||
216 | * @param \ReflectionProperty $property |
||
217 | * @param LdapObjectAnnotation $annotation |
||
218 | * @param $entity |
||
219 | * @return LdapObject|LdapObjectCollection|null |
||
220 | */ |
||
221 | protected function queryLdapForObjects(\ReflectionProperty $property, LdapObjectAnnotation $annotation, $entity) |
||
222 | { |
||
223 | $query = $this->ldap->buildLdapQuery() |
||
224 | ->select($this->getLdapAttributesToSelect($annotation)) |
||
225 | ->from($annotation->type); |
||
226 | $values = $property->getValue($entity); |
||
227 | |||
228 | // A single LdapObject type... |
||
229 | if (is_string($values) && !empty($values)) { |
||
230 | $query->where([$annotation->id => $values]); |
||
231 | // A LdapObjectCollection type... |
||
232 | } elseif (is_array($values) && !empty($values)) { |
||
233 | foreach ($values as $value) { |
||
234 | $query->orWhere([$annotation->id => $value]); |
||
235 | } |
||
236 | // A currently null/empty value? |
||
237 | } else { |
||
238 | return null; |
||
239 | } |
||
240 | |||
241 | if ($annotation->collection) { |
||
242 | $results = $query->getLdapQuery()->getResult(); |
||
243 | } else { |
||
244 | $results = $query->getLdapQuery()->getOneOrNullResult(); |
||
245 | } |
||
246 | |||
247 | return $results; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Avoid calling deprecated methods if possible. |
||
252 | * |
||
253 | * @param LifecycleEventArgs $args |
||
254 | * @return object |
||
255 | */ |
||
256 | View Code Duplication | protected function getObjectFromLifeCycleArgs(LifecycleEventArgs $args) |
|
266 | |||
267 | /** |
||
268 | * Avoid calling deprecated methods if possible. |
||
269 | * |
||
270 | * @param LifecycleEventArgs $args |
||
271 | * @return \Doctrine\Common\Persistence\ObjectManager|\Doctrine\ORM\EntityManager |
||
272 | */ |
||
273 | View Code Duplication | protected function getOmFromLifeCycleArgs(LifecycleEventArgs $args) |
|
283 | } |
||
284 |
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.