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 |
||
| 13 | abstract class AbstractGraphWidget extends AdminWidget implements GraphWidgetInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var mixed $height |
||
| 17 | */ |
||
| 18 | protected $height = 400; |
||
| 19 | /** |
||
| 20 | * @var array $colors |
||
| 21 | */ |
||
| 22 | protected $colors; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param mixed $height |
||
| 26 | * @return GraphWidgetInterface Chainable |
||
| 27 | */ |
||
| 28 | public function setHeight($height) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | public function height() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string[] $colors |
||
| 44 | * @return GraphWidgetInterface Chainable |
||
| 45 | */ |
||
| 46 | public function setColors(array $colors) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | */ |
||
| 55 | View Code Duplication | public function colors() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @todo Read from widget metadata |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function defaultColors() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return array Categories structure. |
||
| 94 | */ |
||
| 95 | abstract public function categories(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string JSONified categories structure. |
||
| 99 | */ |
||
| 100 | public function seriesJson() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return array Series structure. |
||
| 107 | */ |
||
| 108 | abstract public function series(); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string JSONified categories structure. |
||
| 112 | */ |
||
| 113 | public function categoriesJson() |
||
| 117 | } |
||
| 118 |
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.