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 |
||
| 16 | class ProcessesGauge implements GaugeInterface |
||
| 17 | { |
||
| 18 | protected $cpuAbove; |
||
| 19 | |||
| 20 | protected $memoryAbove; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var TopCommand |
||
| 24 | */ |
||
| 25 | protected $command; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * This is the cache namespace |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $namespace; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * ProcessesGauge constructor. |
||
| 36 | * |
||
| 37 | * Track CPU and memory of processes |
||
| 38 | * |
||
| 39 | * @param float $cpuAbove |
||
| 40 | * @param float $memoryAbove |
||
| 41 | */ |
||
| 42 | 3 | public function __construct($cpuAbove = 5.0, $memoryAbove = 1.0) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function getSamplingPeriod() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 1 | public function getCollection() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Return parsed data for processes |
||
| 95 | */ |
||
| 96 | 1 | protected function getProcesses() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Return only those processes above certail value |
||
| 108 | * |
||
| 109 | * @param $processes Process[] |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | protected function filterAbove($processes, $gate) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Aggregate CPU for processes with the same name |
||
| 121 | * |
||
| 122 | * @param $processes Process[] |
||
| 123 | */ |
||
| 124 | 1 | View Code Duplication | protected function aggregateCpu($processes) |
| 139 | |||
| 140 | /** |
||
| 141 | * Aggregate memory for processes with the same name |
||
| 142 | * |
||
| 143 | * @param $processes Process[] |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | 1 | View Code Duplication | protected function aggregateMemory($processes) |
| 161 | |||
| 162 | /** |
||
| 163 | * Return command object for top utility |
||
| 164 | * |
||
| 165 | * @return TopCommand |
||
| 166 | */ |
||
| 167 | protected function getCommand() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Persist collection values to a temporary storage |
||
| 174 | * key is the command string value |
||
| 175 | * |
||
| 176 | * @param $collection |
||
| 177 | */ |
||
| 178 | 3 | protected function persistCollection($collection) |
|
| 185 | |||
| 186 | /**\ |
||
| 187 | * Retrieve a new collection of a previous collection |
||
| 188 | * |
||
| 189 | * @return mixed|ValuesCollection |
||
| 190 | */ |
||
| 191 | 3 | protected function retrieveCollection() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Reset collection values to 0 |
||
| 210 | * |
||
| 211 | * These are in fact previous collection values that needs to be sent to statsd as 0 |
||
| 212 | * |
||
| 213 | * @param $collection |
||
| 214 | */ |
||
| 215 | 1 | protected function resetValues($collection) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Remove empty values from collection |
||
| 225 | * |
||
| 226 | * @param $collection |
||
| 227 | */ |
||
| 228 | 1 | protected function removeEmpty($collection) { |
|
| 237 | } |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: