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 |
||
8 | View Code Duplication | class Text extends Field { |
|
|
|||
9 | |||
10 | protected $value = ''; |
||
11 | protected $width = '100%'; |
||
12 | |||
13 | public function setWidth($width) |
||
18 | 15 | ||
19 | public function getValue() |
||
23 | 29 | ||
24 | public function getButtonName() |
||
28 | 8 | ||
29 | public function renderWith() |
||
36 | 25 | ||
37 | public function setValueFromDefault() |
||
41 | 1 | ||
42 | public function setValueFromModel($model) |
||
46 | 2 | ||
47 | public function setValueFromRequest($request) |
||
51 | 1 | ||
52 | public function fillModelWithValue($model) |
||
56 | |||
57 | public function validateRequired() |
||
61 | 1 | ||
62 | public function validateBetween($min, $max) |
||
67 | |||
68 | public function validateMax($max) |
||
72 | |||
73 | public function validateMin($min) |
||
77 | |||
78 | public function validateAlpha() |
||
82 | |||
83 | public function validateAlphaNum() |
||
87 | |||
88 | public function validateAlphaDash() |
||
92 | |||
93 | public function validateIn($values = []) |
||
97 | |||
98 | public function validateNotIn($values = []) |
||
102 | |||
103 | } |
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.