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 |
||
21 | class PollableChannel extends \PEIP\ABS\Channel\Channel implements \PEIP\INF\Channel\PollableChannel |
||
22 | { |
||
23 | const |
||
24 | EVENT_PRE_RECEIVE = 'pre_receive', |
||
25 | EVENT_POST_RECEIVE = 'post_receive', |
||
26 | HEADER_MESSAGE = 'MESSAGE'; |
||
27 | |||
28 | protected $messages = []; |
||
29 | |||
30 | /** |
||
31 | * Sends a message on the channel. |
||
32 | * |
||
33 | * @param \PEIP\INF\Message\Message $message the message to send |
||
34 | * |
||
35 | * @return |
||
36 | */ |
||
37 | protected function doSend(\PEIP\INF\Message\Message $message) |
||
43 | |||
44 | /** |
||
45 | * Receives a message from the channel. |
||
46 | * |
||
47 | * @event preReceive |
||
48 | * @event postReceive |
||
49 | * |
||
50 | * @param int $timeout timout for receiving a message |
||
51 | * |
||
52 | * @return |
||
53 | */ |
||
54 | public function receive($timeout = 0) |
||
76 | |||
77 | /** |
||
78 | * Returns a message from top of the message stack. |
||
79 | * |
||
80 | * @return \PEIP\INF\Message\Message message from top of the message stack |
||
81 | */ |
||
82 | protected function getMessage() |
||
86 | |||
87 | /** |
||
88 | * Deletes all messages on the message stack. |
||
89 | * |
||
90 | * @return |
||
91 | */ |
||
92 | public function clear() |
||
96 | |||
97 | /** |
||
98 | * Removes all messages not accepted by a given message-selector from the message-stack. |
||
99 | * |
||
100 | * @param \PEIP\INF\Message\Message_Selector $selector the selector to accept messages |
||
101 | * |
||
102 | * @return array accepted messages |
||
103 | */ |
||
104 | View Code Duplication | public function purge(\PEIP\INF\Selector\MessageSelector $selector) |
|
114 | } |
||
115 |
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.