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 |
||
26 | class Validator implements ValidatorInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $constraints = array(); |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $defaultGroup; |
||
37 | |||
38 | /** |
||
39 | * @var MetadataFactoryInterface |
||
40 | */ |
||
41 | protected $metadataFactory; |
||
42 | |||
43 | /** |
||
44 | * @var Validator |
||
45 | */ |
||
46 | private static $instance = null; |
||
47 | |||
48 | /** |
||
49 | * Validator constructor. |
||
50 | * |
||
51 | * @param MetadataFactoryInterface $metadataFactory |
||
52 | * @param string $defaultGroup |
||
53 | */ |
||
54 | private function __construct(MetadataFactoryInterface $metadataFactory, $defaultGroup = Assert::DEFAULT_GROUP) |
||
59 | |||
60 | /** |
||
61 | * @param MetadataFactoryInterface $metadataFactory |
||
62 | */ |
||
63 | public static function setMetadataFactory(MetadataFactoryInterface $metadataFactory) |
||
67 | |||
68 | /** |
||
69 | * @param $defaultGroup |
||
70 | */ |
||
71 | public static function setDefaultGroup($defaultGroup) |
||
75 | |||
76 | /** |
||
77 | * @return Validator |
||
78 | */ |
||
79 | public static function create() |
||
89 | |||
90 | /** |
||
91 | * @param Assert $assert |
||
92 | * @param string $className |
||
93 | * @param string $group |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | protected function addConstraint(Assert $assert, $className = null, $group = null) |
||
114 | |||
115 | /** |
||
116 | * @param string $className |
||
117 | * @param string $group |
||
118 | * |
||
119 | * @return Assert |
||
120 | */ |
||
121 | protected function getConstraintsByGroup($className = null, $group = null) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public static function assert($value, $constraints = null, $group = null) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | protected function assertConstraints($value, $constraints = null, $group = null) |
||
149 | { |
||
150 | $group = $this->normalizeGroup($group); |
||
151 | |||
152 | // If explicit constraints are passed, validate the value against |
||
153 | // those constraints |
||
154 | if (null !== $constraints) { |
||
155 | if (!is_array($constraints)) { |
||
156 | $constraints = array($constraints); |
||
157 | } |
||
158 | |||
159 | foreach ($constraints as $constraint) { |
||
160 | $this->addConstraint($constraint, null, $group); |
||
161 | } |
||
162 | |||
163 | $constraints = $this->getConstraintsByGroup(null, $group); |
||
164 | |||
165 | try { |
||
166 | $returnValue = $constraints->assert($value); |
||
167 | } catch (NestedValidationException $e) { |
||
168 | throw new ValidationException( |
||
169 | implode(', ', $e->getMessages()), |
||
170 | $e->getMessages(), |
||
171 | $e->getCode(), |
||
172 | $e->getPrevious() |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | return $returnValue; |
||
177 | } |
||
178 | |||
179 | // If an object is passed without explicit constraints, validate that |
||
180 | // object against the constraints defined for the object's class |
||
181 | if (is_object($value)) { |
||
182 | $this->addObjectConstraints($value); |
||
183 | |||
184 | $constraints = $this->getConstraintsByGroup(get_class($value), $group); |
||
185 | |||
186 | try { |
||
187 | $returnValue = $constraints->assert($value); |
||
188 | } catch (NestedValidationException $e) { |
||
189 | throw new ValidationException( |
||
190 | implode(', ', $e->getMessages()), |
||
191 | $e->getMessages(), |
||
192 | $e->getCode(), |
||
193 | $e->getPrevious() |
||
194 | ); |
||
195 | } |
||
196 | |||
197 | return $returnValue; |
||
198 | } |
||
199 | |||
200 | // If an array is passed without explicit constraints, validate each |
||
201 | // object in the array |
||
202 | if (is_array($value)) { |
||
203 | $this->addArrayConstraints($value); |
||
204 | |||
205 | $returnValue = true; |
||
206 | foreach ($value as $item) { |
||
207 | $constraints = $this->getConstraintsByGroup(is_object($item) ? get_class($item) : null, $group); |
||
208 | |||
209 | try { |
||
210 | $returnValue = $returnValue && $constraints->assert($item); |
||
211 | } catch (NestedValidationException $e) { |
||
212 | throw new ValidationException( |
||
213 | implode(', ', $e->getMessages()), |
||
214 | $e->getMessages(), |
||
215 | $e->getCode(), |
||
216 | $e->getPrevious() |
||
217 | ); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | return $returnValue; |
||
222 | } |
||
223 | |||
224 | throw new \RuntimeException(sprintf( |
||
225 | 'Cannot validate values of type "%s" automatically. Please '. |
||
226 | 'provide a constraint.', |
||
227 | gettype($value) |
||
228 | )); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public static function validate($value, $constraints = null, $group = null) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | protected function validateConstraints($value, $constraints = null, $group = null) |
||
288 | |||
289 | /** |
||
290 | * @param object $object |
||
291 | */ |
||
292 | protected function addObjectConstraints($object) |
||
330 | |||
331 | /** |
||
332 | * @param string $className |
||
333 | * |
||
334 | * @return ClassMetadata|null |
||
335 | */ |
||
336 | public static function getMetadataForClass($className) |
||
340 | |||
341 | /** |
||
342 | * @param string $namespace |
||
343 | * @param bool $prepend |
||
344 | */ |
||
345 | public static function registerValidator($namespace, $prepend = false) |
||
349 | |||
350 | /** |
||
351 | * @param mixed $array |
||
352 | */ |
||
353 | protected function addArrayConstraints($array) |
||
374 | |||
375 | /** |
||
376 | * Normalizes the given group. |
||
377 | * |
||
378 | * @param string $group |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function normalizeGroup($group = null) |
||
386 | |||
387 | /** |
||
388 | * Normalizes the given className. |
||
389 | * |
||
390 | * @param string $className |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | protected function normalizeClassName($className = null) |
||
398 | } |
||
399 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: