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 |
||
8 | class Validator |
||
9 | { |
||
10 | |||
11 | const TYPE_NULL = 'null'; |
||
12 | const TYPE_ARRAY = 'array'; |
||
13 | const TYPE_INT = 'int'; |
||
14 | const TYPE_STRING = 'string'; |
||
15 | const TYPE_DATE_TIME = 'datetime'; |
||
16 | const TYPE_DATE_TIME_STRING = 'datetimestring'; |
||
17 | |||
18 | /** |
||
19 | * @param mixed $value |
||
20 | * @param string $type self::TYPE_* |
||
21 | */ |
||
22 | public static function checkType($value, $type) |
||
53 | |||
54 | public static function checkStructure($data, array $structure, array $path = array()) |
||
100 | |||
101 | /** |
||
102 | * @param mixed $value |
||
103 | */ |
||
104 | public static function checkNull($value) |
||
110 | |||
111 | /** |
||
112 | * @param mixed $value |
||
113 | */ |
||
114 | public static function checkArray($value) |
||
120 | |||
121 | /** |
||
122 | * @param mixed $value |
||
123 | */ |
||
124 | public static function checkDateTime($value) |
||
130 | |||
131 | /** |
||
132 | * @param mixed $value |
||
133 | */ |
||
134 | public static function checkDateTimeString($value) |
||
143 | |||
144 | /** |
||
145 | * @param mixed $value |
||
146 | */ |
||
147 | public static function checkInt($value) |
||
153 | |||
154 | /** |
||
155 | * @param mixed $value |
||
156 | */ |
||
157 | public static function checkString($value) |
||
163 | |||
164 | /** |
||
165 | * @param string $type |
||
166 | * @param mixed $value |
||
167 | * @internal |
||
168 | */ |
||
169 | private static function throwException($type, $value) |
||
177 | |||
178 | /** |
||
179 | * @param mixed $value |
||
180 | * @return string |
||
181 | */ |
||
182 | private static function getType($value) |
||
186 | |||
187 | /** |
||
188 | * @param mixed $value |
||
189 | * @return bool |
||
190 | */ |
||
191 | private static function isList($value) |
||
195 | |||
196 | /** |
||
197 | * @param array $path |
||
198 | * @param string|null $currentKey |
||
199 | * @return string |
||
200 | */ |
||
201 | private static function getStructurePath(array $path, $currentKey = null) |
||
215 | |||
216 | } |
||
217 |
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.