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:
Complex classes like JsonProcessor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\RpcServer\Request; |
||
26 | class JsonProcessor { |
||
27 | |||
28 | /** |
||
29 | * A parameters object |
||
30 | * |
||
31 | * @var \Comodojo\RpcServer\Request\Parameters |
||
32 | */ |
||
33 | private $parameters = null; |
||
34 | |||
35 | /** |
||
36 | * Array of requests |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $requests = array(); |
||
41 | |||
42 | /** |
||
43 | * Array of results |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $results = array(); |
||
48 | |||
49 | /** |
||
50 | * Internal flag to identify a batch request |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | private $is_batch_request = false; |
||
55 | |||
56 | /** |
||
57 | * Current logger |
||
58 | * |
||
59 | * @var \Psr\Log\LoggerInterface |
||
60 | */ |
||
61 | private $logger = null; |
||
62 | |||
63 | /** |
||
64 | * Class constructor |
||
65 | * |
||
66 | * @param array|object $payload |
||
67 | * @param \Comodojo\RpcServer\Request\Parameters $parameters |
||
68 | * @param \Psr\Log\LoggerInterface $logger |
||
69 | */ |
||
70 | 42 | public function __construct($payload, Parameters $parameters, \Psr\Log\LoggerInterface $logger) { |
|
81 | |||
82 | /** |
||
83 | * Run the processor and exec callback(s) |
||
84 | * |
||
85 | * @return mixed |
||
86 | * @throws Exception |
||
87 | */ |
||
88 | 42 | public function run() { |
|
146 | |||
147 | /** |
||
148 | * Static constructor - start processor |
||
149 | * |
||
150 | * @param array|object $payload |
||
151 | * @param \Comodojo\RpcServer\Request\Parameters $parameters |
||
152 | * @param \Psr\Log\LoggerInterface $logger |
||
153 | * |
||
154 | * @return mixed |
||
155 | * @throws Exception |
||
156 | */ |
||
157 | 42 | View Code Duplication | public static function process($payload, Parameters $parameters, \Psr\Log\LoggerInterface $logger) { |
174 | |||
175 | /** |
||
176 | * Preprocess json payload |
||
177 | * |
||
178 | * @param array|object $payload |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | 42 | private static function preprocessJsonPayload($payload) { |
|
203 | |||
204 | /** |
||
205 | * Preprocess a single json request |
||
206 | * |
||
207 | * @param array|object $request |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | 42 | private static function preprocessJsonRequest($request) { |
|
240 | |||
241 | /** |
||
242 | * Exec a single request |
||
243 | * |
||
244 | * @param string $request_method |
||
245 | * @param array $parameters |
||
246 | * |
||
247 | * @return mixed |
||
248 | * @throws RpcException |
||
249 | */ |
||
250 | 42 | private function runSingleRequest($request_method, $parameters) { |
|
315 | |||
316 | /** |
||
317 | * Pack a json error response |
||
318 | * |
||
319 | * @param integer $code |
||
320 | * @param string $message |
||
321 | * @param integer $id |
||
322 | * |
||
323 | * @return array|null |
||
324 | */ |
||
325 | 12 | private static function packJsonError($code, $message, $id) { |
|
345 | |||
346 | /** |
||
347 | * Pack a json success response |
||
348 | * |
||
349 | * @param mixed $result |
||
350 | * @param integer $id |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | 33 | private static function packJsonSuccess($result, $id) { |
|
371 | |||
372 | /** |
||
373 | * Create an associative array of $name => $parameter from current signature |
||
374 | * |
||
375 | * @param array $provided |
||
376 | * @param \Comodojo\RpcServer\RpcMethod $method |
||
377 | * @param integer $selected_signature |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | 39 | View Code Duplication | private static function matchParameters($provided, $method, $selected_signature) { |
398 | |||
399 | /** |
||
400 | * Check if a request is sustainable (i.e. if method is registered) |
||
401 | * |
||
402 | * @param string $request_method |
||
403 | * |
||
404 | * @return \Comodojo\RpcServer\RpcMethod |
||
405 | * @throws \Comodojo\Exception\RpcException |
||
406 | */ |
||
407 | 42 | View Code Duplication | private function checkRequestSustainability($request_method) { |
416 | |||
417 | /** |
||
418 | * Check if a request is consistent (i.e. if it matches one of method's signatures) |
||
419 | * |
||
420 | * @param \Comodojo\RpcServer\RpcMethod $registered_method |
||
421 | * @param array $parameters |
||
422 | * |
||
423 | * @return int |
||
424 | * @throws \Comodojo\Exception\RpcException |
||
425 | */ |
||
426 | 39 | private function checkRequestConsistence($registered_method, $parameters) { |
|
439 | |||
440 | /** |
||
441 | * Check if call match a signature |
||
442 | * |
||
443 | * @param array $provided |
||
444 | * @param array $requested |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | 39 | private static function checkSignatureMatch($provided, $requested) { |
|
484 | |||
485 | } |
||
486 |
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.