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 |
||
35 | class ClientSessionStub implements ClientSessionInterface, EventEmitterInterface |
||
36 | { |
||
37 | use EventEmitterTrait; |
||
38 | |||
39 | protected $sessionId; |
||
40 | |||
41 | protected $subscriptions = []; |
||
42 | |||
43 | protected $publications = []; |
||
44 | |||
45 | protected $registrations = []; |
||
46 | |||
47 | protected $unregistrations = []; |
||
48 | |||
49 | protected $calls = []; |
||
50 | |||
51 | protected $procedures = []; |
||
52 | |||
53 | /** |
||
54 | * Subscribe. |
||
55 | * |
||
56 | * @param string $topicName |
||
57 | * @param callable $callback |
||
58 | * @param $options array |
||
59 | * |
||
60 | * @return \React\Promise\Promise |
||
61 | */ |
||
62 | public function subscribe($topicName, callable $callback, $options = null) |
||
72 | |||
73 | /** |
||
74 | * Trigger a SUBSCRIBED message for given topic. |
||
75 | * |
||
76 | * @param $topicName |
||
77 | * @param $requestId |
||
78 | * @param $sessionId |
||
79 | * |
||
80 | * @throws UnknownTopicException if the topic is unknown. |
||
81 | */ |
||
82 | View Code Duplication | public function completeSubscription($topicName, $requestId = 1, $sessionId = 1) |
|
94 | |||
95 | public function hasSubscription($topicName) |
||
99 | |||
100 | /** |
||
101 | * Publish. |
||
102 | * |
||
103 | * @param string $topicName |
||
104 | * @param array|mixed $arguments |
||
105 | * @param array|mixed $argumentsKw |
||
106 | * @param array|mixed $options |
||
107 | * |
||
108 | * @return \React\Promise\Promise |
||
109 | */ |
||
110 | public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null) |
||
118 | |||
119 | /** |
||
120 | * Trigger a PUBLISHED message for given topic. |
||
121 | * |
||
122 | * @param string $topicName |
||
123 | * @param int $requestId |
||
124 | * @param int $publicationId |
||
125 | * |
||
126 | * @throws UnknownTopicException if the topic is unknown. |
||
127 | */ |
||
128 | View Code Duplication | public function confirmPublication($topicName, $requestId = 1, $publicationId = 1) |
|
139 | |||
140 | /** |
||
141 | * Register. |
||
142 | * |
||
143 | * @param string $procedureName |
||
144 | * @param callable $callback |
||
145 | * @param array|mixed $options |
||
146 | * |
||
147 | * @return \React\Promise\Promise |
||
148 | */ |
||
149 | public function register($procedureName, callable $callback, $options = null) |
||
159 | |||
160 | /** |
||
161 | * Trigger a REGISTERED message for given procedure. |
||
162 | * |
||
163 | * @param string $procedureName |
||
164 | * @param int $requestId |
||
165 | * @param int $registrationId |
||
166 | * |
||
167 | * @throws UnknownProcedureException if the procedure is unknown. |
||
168 | */ |
||
169 | View Code Duplication | public function confirmRegistration($procedureName, $requestId = 1, $registrationId = 1) |
|
180 | |||
181 | /** |
||
182 | * Triggers a call to a registered procedure and returns its result. |
||
183 | * |
||
184 | * @param string $procedureName |
||
185 | * @param array $args |
||
186 | * |
||
187 | * @throws UnknownProcedureException if the procedure is unknown. |
||
188 | * |
||
189 | * @return mixed the procedure result |
||
190 | */ |
||
191 | public function callRegistration($procedureName, array $args = []) |
||
201 | |||
202 | /** |
||
203 | * Unregister. |
||
204 | * |
||
205 | * @param string $procedureName |
||
206 | * |
||
207 | * @return \React\Promise\Promise|false |
||
208 | */ |
||
209 | public function unregister($procedureName) |
||
217 | |||
218 | /** |
||
219 | * Triggers a UNREGISTERED message for given procedure. |
||
220 | * |
||
221 | * @param string $procedureName |
||
222 | * @param int $requestId |
||
223 | * |
||
224 | * @throws UnknownProcedureException |
||
225 | */ |
||
226 | View Code Duplication | public function confirmUnregistration($procedureName, $requestId = 1) |
|
237 | |||
238 | /** |
||
239 | * Call. |
||
240 | * |
||
241 | * @param string $procedureName |
||
242 | * @param array|mixed $arguments |
||
243 | * @param array|mixed $argumentsKw |
||
244 | * @param array|mixed $options |
||
245 | * |
||
246 | * @return \React\Promise\Promise |
||
247 | */ |
||
248 | public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null) |
||
256 | |||
257 | /** |
||
258 | * Process ResultMessage. |
||
259 | * |
||
260 | * @param string $procedureName |
||
261 | * @param \stdClass $result |
||
262 | */ |
||
263 | public function respondToCall($procedureName, $result) |
||
274 | |||
275 | public function hasCall($procedureName) |
||
279 | |||
280 | /** |
||
281 | * @param int $sessionId |
||
282 | */ |
||
283 | public function setSessionId($sessionId) |
||
287 | |||
288 | /** |
||
289 | * @return int the Session Id |
||
290 | */ |
||
291 | public function getSessionId() |
||
295 | |||
296 | /** |
||
297 | * Generate a unique id for sessions and requests. |
||
298 | * |
||
299 | * @return mixed |
||
300 | */ |
||
301 | public static function getUniqueId() |
||
309 | |||
310 | public function sendMessage($msg) |
||
313 | } |
||
314 |
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.