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:
Complex classes like Validator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Validator |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $errors = array(); |
||
23 | |||
24 | /** |
||
25 | * @var \Pimf\Param |
||
26 | */ |
||
27 | protected $attributes; |
||
28 | |||
29 | /** |
||
30 | * @param \Pimf\Param $attributes |
||
31 | */ |
||
32 | public function __construct(\Pimf\Param $attributes) |
||
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getErrors() |
||
44 | |||
45 | /** |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function isValid() |
||
52 | |||
53 | /** |
||
54 | * check to see if valid email address |
||
55 | * |
||
56 | * @param string $field |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function email($field) |
||
65 | |||
66 | /** |
||
67 | * Check is a valid IP. |
||
68 | * |
||
69 | * @param $field |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function ip($field) |
||
78 | |||
79 | /** |
||
80 | * Check is a valid URL. |
||
81 | * |
||
82 | * @param $field |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function url($field) |
||
91 | |||
92 | /** |
||
93 | * Check to see if two fields are equal. |
||
94 | * |
||
95 | * @param string $field1 |
||
96 | * @param string $field2 |
||
97 | * @param bool $caseInsensitive |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function compare($field1, $field2, $caseInsensitive = false) |
||
114 | |||
115 | /** |
||
116 | * Check to see if the length of a field is between two numbers |
||
117 | * |
||
118 | * @param string $field |
||
119 | * @param int $min |
||
120 | * @param int $max |
||
121 | * @param bool $inclusive |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | View Code Duplication | public function lengthBetween($field, $min, $max, $inclusive = false) |
|
137 | |||
138 | /** |
||
139 | * Check to see if there is punctuation |
||
140 | * |
||
141 | * @param string $field |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function punctuation($field) |
||
149 | |||
150 | /** |
||
151 | * length functions on a field takes <, >, ==, <=, and >= as operators. |
||
152 | * |
||
153 | * @param string $field |
||
154 | * @param string $operator |
||
155 | * @param int $length |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function length($field, $operator, $length) |
||
163 | |||
164 | /** |
||
165 | * Number value functions takes <, >, ==, <=, and >= as operators. |
||
166 | * |
||
167 | * @param string $field |
||
168 | * @param string $operator |
||
169 | * @param string|int $value |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function value($field, $operator, $value) |
||
177 | |||
178 | /** |
||
179 | * Check if a number value is between $max and $min |
||
180 | * |
||
181 | * @param string $field |
||
182 | * @param int $min |
||
183 | * @param int $max |
||
184 | * @param bool $inclusive |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | View Code Duplication | public function valueBetween($field, $min, $max, $inclusive = false) |
|
200 | |||
201 | /** |
||
202 | * Check if a field contains only decimal digit |
||
203 | * |
||
204 | * @param string $field |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function digit($field) |
||
212 | |||
213 | /** |
||
214 | * Check if a field contains only alphabetic characters |
||
215 | * |
||
216 | * @param string $field |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function alpha($field) |
||
224 | |||
225 | /** |
||
226 | * Check if a field contains only alphanumeric characters |
||
227 | * |
||
228 | * @param string $field |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function alphaNumeric($field) |
||
236 | |||
237 | /** |
||
238 | * Check if field is a date by specified format. |
||
239 | * |
||
240 | * @param string $field |
||
241 | * @param string $format Find formats here http://www.php.net/manual/en/function.date.php |
||
242 | * |
||
243 | * @return boolean |
||
244 | */ |
||
245 | public function date($field, $format) |
||
258 | |||
259 | /** |
||
260 | * @param string $field |
||
261 | * @param int $error |
||
262 | * |
||
263 | * @return boolean |
||
264 | */ |
||
265 | protected function error($field, $error) |
||
271 | |||
272 | /** |
||
273 | * @param string $attribute |
||
274 | * |
||
275 | * @return string |
||
276 | * @throws \OutOfBoundsException If attribute not at range |
||
277 | */ |
||
278 | protected function get($attribute) |
||
286 | |||
287 | /** |
||
288 | * @param string $fieldName |
||
289 | * @param string $comparing |
||
290 | * @param string $operator |
||
291 | * @param string|integer $expecting |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | protected function middleware($fieldName, $comparing, $operator, $expecting) |
||
318 | } |
||
319 |
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.