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 |
||
24 | class EventResolverFactory extends UserAwareComponent |
||
25 | { |
||
26 | private $customResolvers = array(); |
||
27 | private $commonResolver; |
||
28 | |||
29 | /** |
||
30 | * @var EventResolverInterface |
||
31 | */ |
||
32 | private $entityEventResolver; |
||
33 | |||
34 | private $resolverEventMap = array(); |
||
35 | |||
36 | private $debug = false; |
||
37 | private $userProperty; |
||
38 | private $entityClass; |
||
39 | |||
40 | /** |
||
41 | * EventResolverFactory constructor. |
||
42 | * |
||
43 | * @param array $resolverEventMap |
||
44 | * @param $userProperty |
||
45 | * @param $entityClass |
||
46 | */ |
||
47 | public function __construct(array $resolverEventMap = array(), $userProperty = 'username', $entityClass = BaseAuditLog::class) |
||
53 | |||
54 | /** |
||
55 | * @param Event $event |
||
56 | * @param string $eventName |
||
57 | * @return null|BaseAuditLog |
||
58 | * @throws UnrecognizedEventInfoException |
||
59 | * @throws \Exception |
||
60 | */ |
||
61 | public function getEventLog(Event $event, $eventName) |
||
76 | |||
77 | /** |
||
78 | * @param $eventInfo |
||
79 | * |
||
80 | * @return null|BaseAuditLog |
||
81 | * @throws UnrecognizedEventInfoException |
||
82 | * @throws \Exception |
||
83 | */ |
||
84 | protected function getEventLogObject($eventInfo) |
||
96 | |||
97 | /** |
||
98 | * @param string $eventName |
||
99 | * |
||
100 | * @return EventResolverInterface |
||
101 | */ |
||
102 | protected function getResolver($eventName) |
||
116 | |||
117 | /** |
||
118 | * @param string $eventName |
||
119 | * @return bool |
||
120 | */ |
||
121 | protected function isEntityEvent($eventName) |
||
125 | |||
126 | /** |
||
127 | * @param Event $event |
||
128 | * @param string $eventName |
||
129 | * @return null |
||
130 | * @throws InvalidServiceException |
||
131 | */ |
||
132 | protected function getEventLogInfo(Event $event, $eventName) |
||
144 | |||
145 | /** |
||
146 | * @param BaseAuditLog $entity |
||
147 | * @throws \Exception |
||
148 | */ |
||
149 | protected function setUser(BaseAuditLog $entity) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function getClientIp() |
||
175 | |||
176 | /** |
||
177 | * @param $id |
||
178 | * @param EventResolverInterface $resolver |
||
179 | * |
||
180 | * @throws \Exception|InvalidServiceException |
||
181 | */ |
||
182 | View Code Duplication | public function addCustomResolver($id, $resolver) |
|
194 | |||
195 | /** |
||
196 | * @param mixed $resolver |
||
197 | * |
||
198 | * @throws \Exception |
||
199 | */ |
||
200 | View Code Duplication | public function setCommonResolver($resolver) |
|
212 | |||
213 | |||
214 | /** |
||
215 | * @param \Exception $e |
||
216 | * @throws \Exception |
||
217 | * @return null |
||
218 | */ |
||
219 | protected function handleException(\Exception $e) |
||
227 | |||
228 | /** |
||
229 | * @param $eventInfo |
||
230 | * @return null|BaseAuditLog |
||
231 | * @throws \Exception |
||
232 | */ |
||
233 | protected function createEventObjectFromArray($eventInfo) |
||
248 | |||
249 | /** |
||
250 | * @param $userProperty |
||
251 | * @param $user |
||
252 | * @return mixed |
||
253 | */ |
||
254 | protected function getSettablePropertyValue($userProperty, $user) |
||
267 | |||
268 | /** |
||
269 | * @param BaseAuditLog $entity |
||
270 | * @param $userProperty |
||
271 | */ |
||
272 | protected function setImpersonatingUser(BaseAuditLog $entity, $userProperty) |
||
278 | |||
279 | /** |
||
280 | * @param mixed $debug |
||
281 | */ |
||
282 | public function setDebug($debug) |
||
286 | |||
287 | /** |
||
288 | * @param EventResolverInterface $entityEventResolver |
||
289 | */ |
||
290 | public function setEntityEventResolver($entityEventResolver) |
||
294 | |||
295 | private function isDebug() |
||
299 | } |
||
300 |
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.