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 |
||
| 11 | class NotifynderHandler |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Handle the event. |
||
| 15 | * |
||
| 16 | * @param NotifyListener $eventListener |
||
| 17 | * @param null $notifynder |
||
| 18 | * @return mixed |
||
| 19 | */ |
||
| 20 | public function handle(NotifyListener $eventListener, $notifynder = null) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Check if the listener exists on the class |
||
| 53 | * adding when as convention. |
||
| 54 | * |
||
| 55 | * ['postAdd'] whenPostAdd] |
||
| 56 | * |
||
| 57 | * @param $eventName |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | protected function listenerIsRegistered($eventName) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get Event Name from the key |
||
| 67 | * it use a convention. |
||
| 68 | * |
||
| 69 | * given user.post.add -> postAdd |
||
| 70 | * given user@postAdd -> postAdd |
||
| 71 | * |
||
| 72 | * @param $event |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | protected function getEventName($event) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get Notifynder Instance. |
||
| 97 | * |
||
| 98 | * @return Notifynder |
||
| 99 | */ |
||
| 100 | protected function getNotifynder() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Check if the fired method has some notifications |
||
| 109 | * to send. |
||
| 110 | * |
||
| 111 | * @param $notificationsResult |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | View Code Duplication | protected function hasNotificationToSend($notificationsResult) |
|
| 121 | } |
||
| 122 |
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.