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 |
||
| 17 | class DiskMonitoring extends AbstractMonitoring |
||
| 18 | { |
||
| 19 | private $maxUtil; |
||
| 20 | private $partition; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param array $config |
||
| 24 | * @param String $name |
||
| 25 | */ |
||
| 26 | public function __construct($config, $name) |
||
| 27 | { |
||
| 28 | parent::__construct($config, $name); |
||
| 29 | $this->maxUtil = $this->config->maxUtil; |
||
| 30 | $this->partition = $this->config->partition; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return integer Percentage of use disk space |
||
| 35 | */ |
||
| 36 | public function getMetric() |
||
| 37 | { |
||
| 38 | return 100 - intval(100 * ( disk_free_space($this->partition) / disk_total_space($this->partition))); |
||
| 39 | |||
| 40 | } |
||
| 41 | /** |
||
| 42 | * @return string "None" |
||
| 43 | */ |
||
| 44 | public function getUnit() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return array Alarm min and max for number of apache process |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function getAlarms() |
|
| 60 | } |
||
| 61 |
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.