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 |
||
10 | class Evaluator |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Evaluates a value according to a list of expected types. |
||
15 | * |
||
16 | * @param mixed $value The value to evaluate. |
||
17 | * @param array $types The expected types of the evaluated value. |
||
18 | * |
||
19 | * @return mixed |
||
20 | * |
||
21 | * @throws UnexpectedValueException if the value could not be evaluated. |
||
22 | */ |
||
23 | 54 | public static function evaluate($value, array $types) |
|
47 | |||
48 | /** |
||
49 | * Calls an evaluation function based on the type of the value. |
||
50 | * |
||
51 | * @param mixed $value The value to evaluate. |
||
52 | * @param string $type The expected type of the evaluated value. |
||
53 | * @param string $valuetype The raw type of the value to evaluate. |
||
54 | * @param array $types Acceptable value types. |
||
55 | * |
||
56 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
57 | */ |
||
58 | 37 | private static function delegate($value, $type, $valuetype, $types) |
|
69 | |||
70 | /** |
||
71 | * Attempts to evaluate a string according to an expected type. |
||
72 | * |
||
73 | * @param string $value The value to evaluate. |
||
74 | * @param string $type The expected type of the evaluated value. |
||
75 | * @param array $types Acceptable value types. |
||
76 | * |
||
77 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
78 | */ |
||
79 | 10 | private static function evaluateString($value, $type, $types) |
|
87 | |||
88 | /** |
||
89 | * Attempts to evaluate a string according to an expected type, knowing |
||
90 | * that the expected type was not a string. |
||
91 | * |
||
92 | * @param string $value The value to evaluate. |
||
93 | * @param string $type The expected type of the evaluated value. |
||
94 | * |
||
95 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
96 | */ |
||
97 | 8 | private static function evaluateUnmatchedString($value, $type) |
|
117 | |||
118 | /** |
||
119 | * Attempts to evaluate an integer according to an expected type, knowing |
||
120 | * that the expected type was not an integer. |
||
121 | * |
||
122 | * @param string $value The value to evaluate. |
||
123 | * @param string $type The expected type of the evaluated value. |
||
124 | * |
||
125 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
126 | */ |
||
127 | 6 | View Code Duplication | private static function evaluateInteger($value, $type) |
138 | |||
139 | /** |
||
140 | * Attempts to evaluate a float according to an expected type, knowing |
||
141 | * that the expected type was not an float. |
||
142 | * |
||
143 | * @param string $value The value to evaluate. |
||
144 | * @param string $type The expected type of the evaluated value. |
||
145 | * |
||
146 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
147 | */ |
||
148 | 4 | View Code Duplication | private static function evaluateDouble($value, $type) |
159 | |||
160 | /** |
||
161 | * Attempts to evaluate an object according to an expected type. |
||
162 | * |
||
163 | * @param object $value The value to evaluate. |
||
164 | * @param string $type The expected type of the evaluated value. |
||
165 | * @param array $types Acceptable value types. |
||
166 | * |
||
167 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
168 | */ |
||
169 | 15 | private static function evaluateObject($value, $type, $types) |
|
185 | |||
186 | /** |
||
187 | * Attempts to evaluate an object according to an expected type. |
||
188 | * |
||
189 | * @param array $value The value to evaluate. |
||
190 | * @param string $type The expected type of the evaluated value. |
||
191 | * @param array $types Acceptable value types. |
||
192 | * |
||
193 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
194 | */ |
||
195 | 5 | private static function evaluateArray($value, $type, $types) |
|
207 | } |
||
208 |
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.