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 |
||
10 | class ProcessStatusHandler |
||
11 | { |
||
12 | const COMPUTE_ITERATIONS = 3; |
||
13 | const ITERATIONS_SLEEP = 2; |
||
14 | |||
15 | protected $informations = []; |
||
16 | protected $server; |
||
17 | protected $usePrecision = true; |
||
18 | |||
19 | /** |
||
20 | * Set the server helper |
||
21 | * |
||
22 | * @param ServerInterface $server |
||
23 | */ |
||
24 | 39 | public function setServer(ServerInterface $server) |
|
28 | |||
29 | /** |
||
30 | * Whether to run compute multiple times cpu usage |
||
31 | * to get a better approximation or not |
||
32 | * |
||
33 | * @param bool $use |
||
34 | */ |
||
35 | 39 | public function setUsePrecision($use) |
|
39 | |||
40 | /** |
||
41 | * Return the number of process running for the givent command |
||
42 | * |
||
43 | * @param string $command |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | 27 | View Code Duplication | public function getProcessCount($command) |
57 | |||
58 | /** |
||
59 | * Return the average CPU usage for the given command |
||
60 | * |
||
61 | * @param string $command |
||
62 | * |
||
63 | * @return float |
||
64 | */ |
||
65 | 6 | View Code Duplication | public function getProcessUsage($command) |
75 | |||
76 | /** |
||
77 | * Compute the process usage/number for the given command |
||
78 | * |
||
79 | * @param string $command |
||
80 | */ |
||
81 | 30 | protected function computeProcessInformations($command) |
|
104 | |||
105 | /** |
||
106 | * Return a unique key for the given command |
||
107 | * |
||
108 | * @param string $command |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 30 | protected function getKey($command) |
|
116 | } |
||
117 |
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.