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 SessionMonitor implements MonitorInterface, EventEmitterInterface |
||
22 | { |
||
23 | use |
||
24 | MonitorTrait { |
||
25 | start as doStart; |
||
26 | stop as doStop; |
||
27 | } |
||
28 | |||
29 | const SESSION_JOIN_TOPIC = 'wamp.session.on_join'; |
||
30 | const SESSION_LEAVE_TOPIC = 'wamp.session.on_leave'; |
||
31 | const SESSION_COUNT_TOPIC = 'wamp.session.count'; |
||
32 | const SESSION_LIST_TOPIC = 'wamp.session.list'; |
||
33 | const SESSION_INFO_TOPIC = 'wamp.session.get'; |
||
34 | |||
35 | |||
36 | protected $sessionIds = []; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param ClientSession $session |
||
43 | */ |
||
44 | public function __construct(ClientSession $session) |
||
48 | |||
49 | /** |
||
50 | * Start the monitor. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function start() |
||
64 | |||
65 | /** |
||
66 | * Stop the monitor. |
||
67 | * Returns boolean wether the monitor could be started. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function stop() |
||
78 | |||
79 | /** |
||
80 | * Retrieves the session-info for given sessionId |
||
81 | * and populates it in via given callback |
||
82 | * |
||
83 | * @param $sessionId |
||
84 | * @param callable $callback |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function getSessionInfo($sessionId, callable $callback) |
||
99 | |||
100 | /** |
||
101 | * Retrieves the Ids of the sessions currently |
||
102 | * registered on the wamp-router in the monitor's realm |
||
103 | * and populates the data via given callback, |
||
104 | * |
||
105 | * @param callable $callback |
||
106 | * @return mixed |
||
107 | */ |
||
108 | public function getSessionIds(callable $callback) |
||
116 | |||
117 | /** |
||
118 | * Initializes the subscription to the meta-events |
||
119 | */ |
||
120 | protected function startSubscriptions() |
||
141 | |||
142 | /** |
||
143 | * Unsubscribes from the meta-events. |
||
144 | */ |
||
145 | protected function stopSubscriptions() |
||
150 | |||
151 | /** |
||
152 | * Retrieves the list of current sessionIds on the router. |
||
153 | * |
||
154 | * @param callable|null $callback |
||
155 | */ |
||
156 | protected function retrieveSessionIds(callable $callback = null) |
||
173 | |||
174 | |||
175 | protected function setList($list) |
||
179 | |||
180 | protected function getList() |
||
184 | |||
185 | /** |
||
186 | * remove the sessionID of the Monitor from the list |
||
187 | * @param array $sessionsIds |
||
188 | * @return mixed |
||
189 | */ |
||
190 | protected function removeOwnSessionId(array $sessionsIds) |
||
199 | } |
||
200 |
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.