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 | abstract class EventPipe extends \PEIP\Pipe\Pipe |
||
28 | { |
||
29 | protected $connections = []; |
||
30 | |||
31 | /** |
||
32 | * Connects the event-pipe to a given \PEIP\INF\Event\Connectable instance by listening |
||
33 | * to a given event on the connectable. |
||
34 | * |
||
35 | * @param string $eventName name of the event to listen to |
||
36 | * @param \PEIP\INF\Event\Connectable $connectable instance of \PEIP\INF\Event\Connectable to listen to |
||
37 | */ |
||
38 | View Code Duplication | protected function doListen($eventName, \PEIP\INF\Event\Connectable $connectable) |
|
45 | |||
46 | /** |
||
47 | * Disonnects the event-pipe from listening to a given event on a \PEIP\INF\Event\Connectable instance. |
||
48 | * |
||
49 | * @param string $eventName name of the event to unlisten |
||
50 | * @param \PEIP\INF\Event\Connectable $connectable instance of \PEIP\INF\Event\Connectable to unlisten to |
||
51 | */ |
||
52 | View Code Duplication | protected function doUnlisten($eventName, \PEIP\INF\Event\Connectable $connectable) |
|
59 | |||
60 | /** |
||
61 | * Returns the instances of \PEIP\INF\Event\Connectable the event-pipe is litening to. |
||
62 | * |
||
63 | * @return array array of \PEIP\INF\Event\Connectable instances |
||
64 | */ |
||
65 | public function doGetConnected() |
||
69 | } |
||
70 |
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.