Complex classes like ValueValidator 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 ValueValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class ValueValidator |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * The error messages generated after validation or set manually |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $messages = array(); |
||
16 | |||
17 | /** |
||
18 | * Will be used to construct the rules |
||
19 | * |
||
20 | * @var \Sirius\Validation\RuleFactory |
||
21 | */ |
||
22 | protected $ruleFactory; |
||
23 | |||
24 | /** |
||
25 | * The prototype that will be used to generate the error message |
||
26 | * |
||
27 | * @var \Sirius\Validation\ErrorMessage |
||
28 | */ |
||
29 | protected $errorMessagePrototype; |
||
30 | |||
31 | /** |
||
32 | * The rule collections for the validation |
||
33 | * |
||
34 | * @var \Sirius\Validation\RuleCollection |
||
35 | */ |
||
36 | protected $rules; |
||
37 | |||
38 | /** |
||
39 | * The label of the value to be validated |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $label; |
||
44 | |||
45 | |||
46 | 22 | public function __construct( |
|
64 | |||
65 | 1 | public function setLabel($label = null) |
|
71 | |||
72 | /** |
||
73 | * Add 1 or more validation rules |
||
74 | * |
||
75 | * @example |
||
76 | * // add multiple rules at once |
||
77 | * $validator->add(array( |
||
78 | * 'required', |
||
79 | * array('required', array('email', null, '{label} must be an email', 'Field B')), |
||
80 | * )); |
||
81 | * |
||
82 | * // add multiple rules using a string |
||
83 | * $validator->add('required | email'); |
||
84 | * |
||
85 | * // add validator with options |
||
86 | * $validator->add('minlength', array('min' => 2), '{label} should have at least {min} characters', 'Field label'); |
||
87 | * |
||
88 | * // add validator with string and parameters as JSON string |
||
89 | * $validator->add('minlength({"min": 2})({label} should have at least {min} characters)(Field label)'); |
||
90 | * |
||
91 | * // add validator with string and parameters as query string |
||
92 | * $validator->add('minlength(min=2)({label} should have at least {min} characters)(Field label)'); |
||
93 | * |
||
94 | * @param string|callback $name |
||
95 | * @param string|array $options |
||
96 | * @param string $messageTemplate |
||
97 | * @param string $label |
||
98 | * |
||
99 | * @return ValueValidator |
||
100 | */ |
||
101 | 22 | public function add($name, $options = null, $messageTemplate = null, $label = null) |
|
126 | |||
127 | /** |
||
128 | * @param array $rules |
||
129 | * |
||
130 | * @return ValueValidator |
||
131 | */ |
||
132 | 4 | public function addMultiple($rules) |
|
150 | |||
151 | /** |
||
152 | * @param AbstractValidator $validationRule |
||
153 | * |
||
154 | * @return ValueValidator |
||
155 | */ |
||
156 | 20 | public function addRule(AbstractRule $validationRule) |
|
163 | |||
164 | /** |
||
165 | * Remove validation rule |
||
166 | * |
||
167 | * @param mixed $name |
||
168 | * rule name or true if all rules should be deleted for that selector |
||
169 | * @param mixed $options |
||
170 | * rule options, necessary for rules that depend on params for their ID |
||
171 | * |
||
172 | * @throws \InvalidArgumentException |
||
173 | * @internal param string $selector data selector |
||
174 | * @return self |
||
175 | */ |
||
176 | 4 | public function remove($name = true, $options = null) |
|
188 | |||
189 | /** |
||
190 | * Converts a rule that was supplied as string into a set of options that define the rule |
||
191 | * |
||
192 | * @example 'minLength({"min":2})({label} must have at least {min} characters)(Street)' |
||
193 | * |
||
194 | * will be converted into |
||
195 | * |
||
196 | * array( |
||
197 | * 'minLength', // validator name |
||
198 | * array('min' => 2'), // validator options |
||
199 | * '{label} must have at least {min} characters', |
||
200 | * 'Street' // label |
||
201 | * ) |
||
202 | * |
||
203 | * @param string $ruleAsString |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 5 | protected function parseRule($ruleAsString) |
|
238 | |||
239 | |||
240 | 20 | public function validate($value, $valueIdentifier = null, DataWrapper\WrapperInterface $context = null) |
|
241 | { |
||
242 | 20 | $this->messages = array(); |
|
243 | 20 | $isRequired = false; |
|
244 | 20 | ||
245 | 20 | // evaluate the required rules |
|
246 | 14 | /* @var $rule \Sirius\Validation\Rule\AbstractValidator */ |
|
247 | 14 | foreach ($this->rules as $rule) { |
|
248 | if ($rule instanceof Rule\Required) { |
||
249 | 20 | $isRequired = true; |
|
250 | |||
251 | 20 | if (!$this->validateRule($rule, $value, $valueIdentifier, $context)) { |
|
252 | 5 | return false; |
|
253 | } |
||
254 | } |
||
255 | } |
||
256 | 19 | ||
257 | 19 | // avoid future rule evaluations if value is null or empty string |
|
258 | 19 | if ($this->isEmpty($value)) { |
|
259 | 19 | return true; |
|
260 | 19 | } |
|
261 | |||
262 | // evaluate the non-required rules |
||
263 | 19 | foreach ($this->rules as $rule) { |
|
264 | 13 | if (!($rule instanceof Rule\Required)) { |
|
265 | $this->validateRule($rule, $value, $valueIdentifier, $context); |
||
266 | 19 | ||
267 | // if field is required and we have an error, |
||
268 | 19 | // do not continue with the rest of rules |
|
269 | if ($isRequired && count($this->messages)) { |
||
270 | break; |
||
271 | 18 | } |
|
272 | } |
||
273 | 18 | } |
|
274 | |||
275 | return count($this->messages) === 0; |
||
276 | 19 | } |
|
277 | |||
278 | 19 | private function validateRule($rule, $value, $valueIdentifier, $context) |
|
279 | { |
||
280 | 19 | $rule->setContext($context); |
|
281 | if (!$rule->validate($value, $valueIdentifier)) { |
||
282 | $this->addMessage($rule->getMessage()); |
||
283 | 1 | return false; |
|
284 | } |
||
285 | 1 | return true; |
|
286 | } |
||
287 | |||
288 | public function getMessages() |
||
292 | |||
293 | public function addMessage($message) |
||
299 | |||
300 | public function getRules() |
||
304 | |||
305 | protected function isEmpty($value) |
||
306 | { |
||
309 | } |
||
310 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: