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 |
||
21 | class RequiredChecker extends AbstractChecker implements SingletonInterface |
||
22 | { |
||
23 | use NotEmptyTrait; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | const MESSAGES = [ |
||
29 | 'notEmpty' => '[[This value is required.]]', |
||
30 | 'with' => '[[This value is required.]]', |
||
31 | 'withAll' => '[[This value is required.]]', |
||
32 | 'without' => '[[This value is required.]]', |
||
33 | 'withoutAll' => '[[This value is required.]]', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Check if field not empty but only if any of listed fields presented or not empty. |
||
38 | * Also: requiredWhenAnyExternalSet |
||
39 | * |
||
40 | * @param mixed $value |
||
41 | * @param array|string $with |
||
42 | * @param bool $asString Automatically trim external values before non empty |
||
43 | * comparision. |
||
44 | * |
||
45 | * @return bool|int |
||
46 | */ |
||
47 | View Code Duplication | public function with($value, $with, bool $asString = true) |
|
64 | |||
65 | /** |
||
66 | * Check if field not empty but only if all of listed fields presented and not empty. |
||
67 | * Also: requiredWhenAllExternalSet |
||
68 | * |
||
69 | * @param mixed $value |
||
70 | * @param array $withAll |
||
71 | * @param bool $asString Automatically trim external values before non empty comparision. |
||
72 | * |
||
73 | * @return bool|int |
||
74 | */ |
||
75 | View Code Duplication | public function withAll($value, $withAll, bool $asString = true) |
|
92 | |||
93 | /** |
||
94 | * Check if field not empty but only if one of listed fields missing or empty. |
||
95 | * Also: requiredWhenNoExternalsSet |
||
96 | * |
||
97 | * @param mixed $value |
||
98 | * @param array|string $without |
||
99 | * @param bool $asString Automatically trim external values before non empty |
||
100 | * comparision. |
||
101 | * |
||
102 | * @return bool|int |
||
103 | */ |
||
104 | View Code Duplication | public function without($value, $without, bool $asString = true) |
|
121 | |||
122 | /** |
||
123 | * Check if field not empty but only if all of listed fields missing or empty. |
||
124 | * Also: requiredWhenAllExternalNotSet |
||
125 | * |
||
126 | * @param mixed $value |
||
127 | * @param array|string $withoutAll |
||
128 | * @param bool $asString Automatically trim external values before non empty |
||
129 | * comparision. |
||
130 | * |
||
131 | * @return bool|int |
||
132 | */ |
||
133 | public function withoutAll($value, $withoutAll, bool $asString = true) |
||
153 | |||
154 | /** |
||
155 | * Check if external validation value is empty. |
||
156 | * |
||
157 | * @param string $field |
||
158 | * @param bool $string |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | private function isEmpty(string $field, bool $string) |
||
166 | } |
||
167 |
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.