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:
Complex classes like NotifynderManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NotifynderManager, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Fenos\Notifynder; |
||
25 | class NotifynderManager extends NotifynderBuilder implements Notifynder |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Version Notifynder |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const VERSION = '3.1.0'; |
||
34 | |||
35 | /** |
||
36 | * @var NotifynderCategory |
||
37 | */ |
||
38 | protected $notifynderCategory; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $categoriesContainer = []; |
||
44 | |||
45 | /** |
||
46 | * @var Models\NotificationCategory|null |
||
47 | */ |
||
48 | protected $defaultCategory; |
||
49 | |||
50 | /** |
||
51 | * @var NotifynderSender |
||
52 | */ |
||
53 | protected $notifynderSender; |
||
54 | |||
55 | /** |
||
56 | * @var NotifynderNotification |
||
57 | */ |
||
58 | protected $notification; |
||
59 | |||
60 | /** |
||
61 | * @var NotifynderDispatcher |
||
62 | */ |
||
63 | protected $notifynderDispatcher; |
||
64 | |||
65 | /** |
||
66 | * This sender method |
||
67 | * will be used on the dispatcher |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $eventSender = 'send'; |
||
72 | |||
73 | /** |
||
74 | * @var NotifynderGroup |
||
75 | */ |
||
76 | protected $notifynderGroup; |
||
77 | |||
78 | /** |
||
79 | * @var string | null |
||
80 | */ |
||
81 | protected $entity; |
||
82 | |||
83 | /** |
||
84 | * @param NotifynderCategory $notifynderCategory |
||
85 | * @param NotifynderSender $notifynderSender |
||
86 | * @param NotifynderNotification $notification |
||
87 | * @param NotifynderDispatcher $notifynderDispatcher |
||
88 | * @param NotifynderGroup $notifynderGroup |
||
89 | */ |
||
90 | public function __construct(NotifynderCategory $notifynderCategory, |
||
91 | NotifynderSender $notifynderSender, |
||
92 | NotifynderNotification $notification, |
||
93 | NotifynderDispatcher $notifynderDispatcher, |
||
94 | NotifynderGroup $notifynderGroup) |
||
95 | { |
||
96 | $this->notifynderCategory = $notifynderCategory; |
||
97 | $this->notifynderSender = $notifynderSender; |
||
98 | $this->notification = $notification; |
||
99 | $this->notifynderDispatcher = $notifynderDispatcher; |
||
100 | $this->notifynderGroup = $notifynderGroup; |
||
101 | |||
102 | parent::__construct($notifynderCategory); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set the category of the |
||
107 | * notification |
||
108 | * |
||
109 | * @param $name |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function category($name) |
||
113 | { |
||
114 | // Check if the category is lazy loaded |
||
115 | if ($this->isLazyLoaded($name)) { |
||
116 | // Yes it is, split out the value from the array |
||
117 | $this->defaultCategory = $this->getCategoriesContainer($name); |
||
|
|||
118 | |||
119 | // set category on the builder |
||
120 | parent::category($this->defaultCategory->id); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | // Otherwise ask to the db and give me the right category |
||
126 | // associated with this name. If the category is not found |
||
127 | // it throw CategoryNotFoundException |
||
128 | $category = $this->notifynderCategory->findByName($name); |
||
129 | |||
130 | $this->defaultCategory = $category; |
||
131 | |||
132 | // Set the category on the array |
||
133 | $this->setCategoriesContainer($name, $category); |
||
134 | |||
135 | // set category on the builder |
||
136 | parent::category($category->id); |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Define an entity when Notifynder is |
||
143 | * used Polymorpically |
||
144 | * |
||
145 | * @param $name |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function entity($name) |
||
149 | { |
||
150 | $this->entity = $name; |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Add a category |
||
157 | * |
||
158 | * @param $name |
||
159 | * @param $text |
||
160 | * @return static |
||
161 | */ |
||
162 | public function addCategory($name, $text) |
||
166 | |||
167 | /** |
||
168 | * Update a category |
||
169 | * |
||
170 | * @param array $updates |
||
171 | * @param $id |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function updateCategory(array $updates, $id) |
||
178 | |||
179 | /** |
||
180 | * Send notifications |
||
181 | * Both multiple and single |
||
182 | * |
||
183 | * @param array $info |
||
184 | * @return mixed |
||
185 | */ |
||
186 | View Code Duplication | public function send($info = []) |
|
196 | |||
197 | /** |
||
198 | * Send immediately the notification |
||
199 | * even if the queue is enabled |
||
200 | * |
||
201 | * @param array $info |
||
202 | * @return mixed |
||
203 | */ |
||
204 | View Code Duplication | public function sendNow($info = []) |
|
214 | |||
215 | /** |
||
216 | * Send One notification |
||
217 | * |
||
218 | * @param array $info |
||
219 | * @return mixed |
||
220 | */ |
||
221 | View Code Duplication | public function sendOne($info = []) |
|
231 | |||
232 | /** |
||
233 | * Send multiple notifications |
||
234 | * |
||
235 | * @param array $info |
||
236 | * @return Senders\SendMultiple |
||
237 | */ |
||
238 | View Code Duplication | public function sendMultiple($info = []) |
|
248 | |||
249 | /** |
||
250 | * Send a group of notifications |
||
251 | * |
||
252 | * @param $group_name |
||
253 | * @param $info |
||
254 | * @return mixed |
||
255 | */ |
||
256 | public function sendGroup($group_name, $info = []) |
||
266 | |||
267 | /** |
||
268 | * Read one notification |
||
269 | * |
||
270 | * @param $notification_id |
||
271 | * @return bool|Models\Notification |
||
272 | */ |
||
273 | public function readOne($notification_id) |
||
277 | |||
278 | /** |
||
279 | * Read notification in base the number |
||
280 | * Given |
||
281 | * |
||
282 | * @param $to_id |
||
283 | * @param $numbers |
||
284 | * @param string $order |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function readLimit($to_id, $numbers, $order = "ASC") |
||
293 | |||
294 | /** |
||
295 | * Read all notifications of the given |
||
296 | * entity |
||
297 | * |
||
298 | * @param $to_id |
||
299 | * @return Number |
||
300 | */ |
||
301 | public function readAll($to_id) |
||
307 | |||
308 | /** |
||
309 | * Delete a single notification |
||
310 | * |
||
311 | * @param $notification_id |
||
312 | * @return Bool |
||
313 | */ |
||
314 | public function delete($notification_id) |
||
318 | |||
319 | /** |
||
320 | * Delete number of notifications |
||
321 | * secified of the given entity |
||
322 | * |
||
323 | * @param $to_id |
||
324 | * @param $number |
||
325 | * @param string $order |
||
326 | * @return mixed |
||
327 | */ |
||
328 | public function deleteLimit($to_id, $number, $order = "ASC") |
||
334 | |||
335 | /** |
||
336 | * Delete all notifications |
||
337 | * of the the given entity |
||
338 | * |
||
339 | * @param $to_id |
||
340 | * @return Bool |
||
341 | */ |
||
342 | public function deleteAll($to_id) |
||
348 | |||
349 | /** |
||
350 | * Delete All notifications from a |
||
351 | * defined category |
||
352 | * |
||
353 | * @param $category_name string |
||
354 | * @param $expired Bool |
||
355 | * @return Bool |
||
356 | */ |
||
357 | public function deleteByCategory($category_name, $expired = false) |
||
361 | |||
362 | /** |
||
363 | * Get Notifications not read |
||
364 | * of the given entity |
||
365 | * |
||
366 | * @param $to_id |
||
367 | * @param null $limit |
||
368 | * @param null|int $paginate |
||
369 | * @param string $order |
||
370 | * @param Closure $filterScope |
||
371 | * @return mixed |
||
372 | */ |
||
373 | public function getNotRead($to_id, $limit = null, $paginate = null, $order = "desc", Closure $filterScope = null) |
||
379 | |||
380 | /** |
||
381 | * Get all notifications of the |
||
382 | * given entity |
||
383 | * |
||
384 | * @param $to_id |
||
385 | * @param null $limit |
||
386 | * @param int|null $paginate |
||
387 | * @param string $order |
||
388 | * @param Closure $filterScope |
||
389 | * @return mixed |
||
390 | */ |
||
391 | public function getAll($to_id, $limit = null, $paginate = null, $order = "desc", Closure $filterScope = null) |
||
397 | |||
398 | /** |
||
399 | * Get number of notification not read |
||
400 | * of the given entity |
||
401 | * |
||
402 | * @param $to_id |
||
403 | * @param Closure $filterScope |
||
404 | * @return mixed |
||
405 | */ |
||
406 | public function countNotRead($to_id,Closure $filterScope = null) |
||
412 | |||
413 | /** |
||
414 | * Find Notification by ID |
||
415 | * |
||
416 | * @param $notification_id |
||
417 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static |
||
418 | */ |
||
419 | public function findNotificationById($notification_id) |
||
423 | |||
424 | /** |
||
425 | * Get last notification of the given |
||
426 | * entity, second parameter can filter by |
||
427 | * category |
||
428 | * |
||
429 | * @param $to_id |
||
430 | * @param null $category |
||
431 | * @param Closure $filterScope |
||
432 | * @return mixed |
||
433 | */ |
||
434 | public function getLastNotification($to_id,$category = null,Closure $filterScope = null) |
||
444 | |||
445 | /** |
||
446 | * Add category to a group |
||
447 | * giving the names of them |
||
448 | * |
||
449 | * @param $gorup_name |
||
450 | * @param $category_name |
||
451 | * @return mixed |
||
452 | */ |
||
453 | public function addCategoryToGroupByName($gorup_name, $category_name) |
||
457 | |||
458 | /** |
||
459 | * Add category to a group |
||
460 | * giving the ids of them |
||
461 | * |
||
462 | * @param $gorup_id |
||
463 | * @param $category_id |
||
464 | * @return mixed |
||
465 | */ |
||
466 | public function addCategoryToGroupById($gorup_id, $category_id) |
||
470 | |||
471 | /** |
||
472 | * Add categories to a group having as first parameter |
||
473 | * the name of the group, and others as name |
||
474 | * categories |
||
475 | * |
||
476 | * @return mixed |
||
477 | */ |
||
478 | public function addCategoriesToGroup() |
||
482 | |||
483 | /** |
||
484 | * Fire method for fire listeners |
||
485 | * of logic |
||
486 | * |
||
487 | * @param string $key |
||
488 | * @param string $category_name |
||
489 | * @param mixed|null $values |
||
490 | * @return mixed|null |
||
491 | */ |
||
492 | public function fire($key, $category_name, $values = []) |
||
497 | |||
498 | /** |
||
499 | * Associate events to categories |
||
500 | * |
||
501 | * @param $data |
||
502 | * @param array $delegation |
||
503 | * @return mixed |
||
504 | */ |
||
505 | public function delegate(array $delegation, $data = []) |
||
509 | |||
510 | /** |
||
511 | * Boot Listeners |
||
512 | * |
||
513 | * @param array $listeners |
||
514 | */ |
||
515 | public function bootListeners(array $listeners) |
||
519 | |||
520 | /** |
||
521 | * Get instance of the notifynder builder |
||
522 | * |
||
523 | * @return NotifynderBuilder |
||
524 | */ |
||
525 | public function builder() |
||
529 | |||
530 | /** |
||
531 | * Extend a custom sender method |
||
532 | * |
||
533 | * @param $name |
||
534 | * @param callable $registrar |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function extend($name, $registrar) |
||
548 | |||
549 | /** |
||
550 | * Check if the category is eager Loaded |
||
551 | * |
||
552 | * @param $name |
||
553 | * @return bool |
||
554 | */ |
||
555 | protected function isLazyLoaded($name) |
||
559 | |||
560 | /** |
||
561 | * Return the Id of the category |
||
562 | * |
||
563 | * @return mixed |
||
564 | */ |
||
565 | public function id() |
||
569 | |||
570 | /** |
||
571 | * Push a category in the categoriesContainer |
||
572 | * property |
||
573 | * |
||
574 | * @param $name |
||
575 | * @param array $categoriesContainer |
||
576 | */ |
||
577 | protected function setCategoriesContainer($name, $categoriesContainer) |
||
581 | |||
582 | /** |
||
583 | * Get the categoriesContainer property |
||
584 | * |
||
585 | * @param $name |
||
586 | * @return array |
||
587 | */ |
||
588 | public function getCategoriesContainer($name) |
||
592 | |||
593 | /** |
||
594 | * Define which method |
||
595 | * the event dispatcher has |
||
596 | * to send the notifications |
||
597 | * |
||
598 | * @param $customSenderName |
||
599 | * @return $this |
||
600 | */ |
||
601 | public function dipatchWith($customSenderName) |
||
607 | |||
608 | /** |
||
609 | * Call the custom sender method |
||
610 | * |
||
611 | * @param $name |
||
612 | * @param $arguments |
||
613 | * @return void|mixed |
||
614 | */ |
||
615 | public function __call($name, $arguments) |
||
629 | |||
630 | /** |
||
631 | * Set builder properties |
||
632 | * When setting dynamic properties |
||
633 | * |
||
634 | * @param $name |
||
635 | * @param $value |
||
636 | */ |
||
637 | function __set($name, $value) |
||
641 | |||
642 | /** |
||
643 | * Get property from the |
||
644 | * builder |
||
645 | * |
||
646 | * @param $name |
||
647 | * @return mixed |
||
648 | */ |
||
649 | function __get($name) |
||
653 | } |
||
654 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..