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 |
||
34 | class ClientSessionStub implements ClientSessionInterface, EventEmitterInterface |
||
35 | { |
||
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 | |||
55 | /** |
||
56 | * Subscribe. |
||
57 | * |
||
58 | * @param string $topicName |
||
59 | * @param callable $callback |
||
60 | * @param $options array |
||
61 | * @return \React\Promise\Promise |
||
62 | */ |
||
63 | public function subscribe($topicName, callable $callback, $options = null) |
||
73 | |||
74 | /** |
||
75 | * Trigger a SUBSCRIBED message for given topic. |
||
76 | * |
||
77 | * @param $topicName |
||
78 | * @param $requestId |
||
79 | * @param $sessionId |
||
80 | * @throws \RuntimeException if the topic is unknown. |
||
81 | */ |
||
82 | View Code Duplication | public function completeSubscription($topicName, $requestId = 1, $sessionId = 1) |
|
95 | |||
96 | |||
97 | /** |
||
98 | * Publish. |
||
99 | * |
||
100 | * @param string $topicName |
||
101 | * @param array|mixed $arguments |
||
102 | * @param array|mixed $argumentsKw |
||
103 | * @param array|mixed $options |
||
104 | * @return \React\Promise\Promise |
||
105 | */ |
||
106 | public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null) |
||
114 | |||
115 | /** |
||
116 | * Trigger a PUBLISHED message for given topic. |
||
117 | * |
||
118 | * @param string $topicName |
||
119 | * @param int $requestId |
||
120 | * @param int $publicationId |
||
121 | * @throws \RuntimeException if the topic is unknown. |
||
122 | */ |
||
123 | View Code Duplication | public function confirmPublication($topicName, $requestId = 1, $publicationId = 1) |
|
134 | |||
135 | |||
136 | /** |
||
137 | * Register. |
||
138 | * |
||
139 | * @param string $procedureName |
||
140 | * @param callable $callback |
||
141 | * @param array|mixed $options |
||
142 | * @return \React\Promise\Promise |
||
143 | */ |
||
144 | public function register($procedureName, callable $callback, $options = null) |
||
157 | |||
158 | /** |
||
159 | * Trigger a REGISTERED message for given procedure. |
||
160 | * |
||
161 | * @param string $procedureName |
||
162 | * @param int $requestId |
||
163 | * @param int $registrationId |
||
164 | * @throws \RuntimeException if the procedure is unknown. |
||
165 | */ |
||
166 | public function confirmRegistration($procedureName, $requestId = 1, $registrationId = 1) |
||
177 | |||
178 | /** |
||
179 | * Triggers a call to a registered procedure and returns its result. |
||
180 | * |
||
181 | * @param string $procedureName |
||
182 | * @param array $args |
||
183 | * @return mixed the procedure result |
||
184 | * @throws \RuntimeException if the procedure is unknown. |
||
185 | */ |
||
186 | public function callRegistration($procedureName, array $args = []) |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Unregister. |
||
200 | * |
||
201 | * @param string $procedureName |
||
202 | * |
||
203 | * @return \React\Promise\Promise|false |
||
204 | */ |
||
205 | public function unregister($procedureName) |
||
216 | |||
217 | /** |
||
218 | * Triggers a UNREGISTERED message for given procedure. |
||
219 | * |
||
220 | * @param string $procedureName |
||
221 | * @param int $requestId |
||
222 | */ |
||
223 | View Code Duplication | public function confirmUnregistration($procedureName, $requestId = 1) |
|
234 | |||
235 | /** |
||
236 | * Call. |
||
237 | * |
||
238 | * @param string $procedureName |
||
239 | * @param array|mixed $arguments |
||
240 | * @param array|mixed $argumentsKw |
||
241 | * @param array|mixed $options |
||
242 | * @return \React\Promise\Promise |
||
243 | */ |
||
244 | public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null) |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Process ResultMessage |
||
258 | * |
||
259 | * @param string $procedureName |
||
260 | * @param \stdClass $result |
||
261 | */ |
||
262 | View Code Duplication | public function respondToCall($procedureName, $result) |
|
273 | |||
274 | |||
275 | /** |
||
276 | * @param int $sessionId |
||
277 | */ |
||
278 | public function setSessionId($sessionId) |
||
282 | |||
283 | /** |
||
284 | * @return int the Session Id |
||
285 | */ |
||
286 | public function getSessionId() |
||
290 | |||
291 | /** |
||
292 | * Generate a unique id for sessions and requests |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public static function getUniqueId() |
||
304 | |||
305 | |||
306 | } |
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.