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 Field implements \PluginSimpleValidate\Contracts\Field |
||
14 | { |
||
15 | const VAR_LIMIT = 'limit'; |
||
16 | |||
17 | const VAR_MATCH = 'match'; |
||
18 | |||
19 | const VAR_LOWER_LIMIT = 'lower'; |
||
20 | |||
21 | const VAR_UPPER_LIMIT = 'upper'; |
||
22 | |||
23 | const VAR_MESSAGE = 'message'; |
||
24 | |||
25 | const VAR_REGION = 'region'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $name; |
||
31 | |||
32 | /** |
||
33 | * @var mixed |
||
34 | */ |
||
35 | protected $value; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $errors; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | * array of Rule |
||
45 | */ |
||
46 | protected $rules = []; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $status; |
||
52 | |||
53 | /** |
||
54 | * @var \PluginSimpleValidate\Contracts\RuleMapping |
||
55 | */ |
||
56 | protected $ruleMapping; |
||
57 | |||
58 | /** |
||
59 | * Field constructor. |
||
60 | * @param string $name |
||
61 | * @param mixed $value |
||
62 | */ |
||
63 | 10 | public function __construct( |
|
71 | |||
72 | /** |
||
73 | * @param Language $language |
||
74 | * @return bool |
||
75 | */ |
||
76 | 9 | View Code Duplication | public function isValid(Language $language) : bool |
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 3 | public function getName(): string |
|
103 | |||
104 | /** |
||
105 | * @return mixed |
||
106 | */ |
||
107 | 1 | public function getValue() |
|
111 | |||
112 | /** |
||
113 | * @param mixed $value |
||
114 | * @return $this |
||
115 | */ |
||
116 | 1 | public function setValue($value) |
|
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getRules(): array |
||
129 | |||
130 | /** |
||
131 | * @return array |
||
132 | */ |
||
133 | 8 | public function getErrors(): array |
|
137 | |||
138 | /** |
||
139 | * @param string $rulesMethod |
||
140 | * @param array $args |
||
141 | * @return $this |
||
142 | */ |
||
143 | 9 | protected function addRules(string $rulesMethod, array $args = []) |
|
148 | |||
149 | /** |
||
150 | * @return $this |
||
151 | */ |
||
152 | 9 | protected function emptyErrors() |
|
157 | } |
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.