Complex classes like AbstractRule 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 AbstractRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class AbstractRule |
||
9 | { |
||
10 | // default error message when there is no LABEL attached |
||
11 | const MESSAGE = 'Value is not valid'; |
||
12 | |||
13 | // default error message when there is a LABEL attached |
||
14 | const LABELED_MESSAGE = '{label} is not valid'; |
||
15 | |||
16 | /** |
||
17 | * The validation context |
||
18 | * This is the data set that the data being validated belongs to |
||
19 | * @var \Sirius\Validation\DataWrapper\WrapperInterface |
||
20 | */ |
||
21 | protected $context; |
||
22 | |||
23 | /** |
||
24 | * Options for the validator. |
||
25 | * Also passed to the error message for customization. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options = array(); |
||
30 | |||
31 | /** |
||
32 | * Custom error message template for the validator instance |
||
33 | * If you don't agree with the default messages that were provided |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $messageTemplate; |
||
38 | |||
39 | /** |
||
40 | * Result of the last validation |
||
41 | * |
||
42 | * @var boolean |
||
43 | */ |
||
44 | protected $success = false; |
||
45 | |||
46 | /** |
||
47 | * Last value validated with the validator. |
||
48 | * Stored in order to be passed to the errorMessage so that you get error |
||
49 | * messages like '"abc" is not a valid email' |
||
50 | * |
||
51 | * @var mixed |
||
52 | */ |
||
53 | protected $value; |
||
54 | |||
55 | /** |
||
56 | * The error message prototype that will be used to generate the error message |
||
57 | * |
||
58 | * @var ErrorMessage |
||
59 | */ |
||
60 | protected $errorMessagePrototype; |
||
61 | |||
62 | /** |
||
63 | * Options map in case the options are passed as list instead of associative array |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $optionsIndexMap = array(); |
||
68 | |||
69 | 165 | public function __construct($options = array()) |
|
78 | |||
79 | /** |
||
80 | * Method that parses the option variable and converts it into an array |
||
81 | * You can pass anything to a validator like: |
||
82 | * - a query string: 'min=3&max=5' |
||
83 | * - a JSON string: '{"min":3,"max":5}' |
||
84 | * - a CSV string: '5,true' (for this scenario the 'optionsIndexMap' property is required) |
||
85 | * |
||
86 | * @param mixed $options |
||
87 | * |
||
88 | * @return array |
||
89 | * @throws \InvalidArgumentException |
||
90 | */ |
||
91 | 165 | protected function normalizeOptions($options) |
|
122 | |||
123 | /** |
||
124 | * Converts a HTTP query string to an array |
||
125 | * |
||
126 | * @param $str |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 6 | protected function parseHttpQueryString($str) |
|
136 | |||
137 | /** |
||
138 | * Converts 'true' and 'false' strings to TRUE and FALSE |
||
139 | * |
||
140 | * @param $v |
||
141 | * |
||
142 | * @return bool|array |
||
143 | */ |
||
144 | 9 | protected function convertBooleanStrings($v) |
|
158 | |||
159 | /** |
||
160 | * Parses a CSV string and converts the result into an "options" array |
||
161 | * (an associative array that contains the options for the validation rule) |
||
162 | * |
||
163 | * @param $str |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | 3 | protected function parseCsvString($str) |
|
191 | |||
192 | /** |
||
193 | * Checks if an array is associative (ie: the keys are not numbers in sequence) |
||
194 | * |
||
195 | * @param array $arr |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 53 | protected function arrayIsAssoc($arr) |
|
203 | |||
204 | |||
205 | /** |
||
206 | * Generates a unique string to identify the validator. |
||
207 | * It is used to compare 2 validators so you don't add the same rule twice in a validator object |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 23 | public function getUniqueId() |
|
215 | |||
216 | /** |
||
217 | * Set an option for the validator. |
||
218 | * |
||
219 | * The options are also be passed to the error message. |
||
220 | * |
||
221 | * @param string $name |
||
222 | * @param mixed $value |
||
223 | * |
||
224 | * @return \Sirius\Validation\Rule\AbstractRule |
||
225 | */ |
||
226 | 109 | public function setOption($name, $value) |
|
232 | |||
233 | /** |
||
234 | * Get an option for the validator. |
||
235 | * |
||
236 | * @param string $name |
||
237 | * |
||
238 | * @return mixed |
||
239 | */ |
||
240 | 6 | public function getOption($name) |
|
248 | |||
249 | /** |
||
250 | * The context of the validator can be used when the validator depends on other values |
||
251 | * that are not known at the moment the validator is constructed |
||
252 | * For example, when you need to validate an email field matches another email field, |
||
253 | * to confirm the email address |
||
254 | * |
||
255 | * @param array|object $context |
||
256 | * |
||
257 | * @throws \InvalidArgumentException |
||
258 | * @return \Sirius\Validation\Rule\AbstractRule |
||
259 | */ |
||
260 | 40 | public function setContext($context = null) |
|
278 | |||
279 | /** |
||
280 | * Custom message for this validator to used instead of the the default one |
||
281 | * |
||
282 | * @param string $messageTemplate |
||
283 | * |
||
284 | * @return \Sirius\Validation\Rule\AbstractRule |
||
285 | */ |
||
286 | 23 | public function setMessageTemplate($messageTemplate) |
|
292 | |||
293 | /** |
||
294 | * Retrieves the error message template (either the global one or the custom message) |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 28 | public function getMessageTemplate() |
|
309 | |||
310 | /** |
||
311 | * Validates a value |
||
312 | * |
||
313 | * @param mixed $value |
||
314 | * @param null|mixed $valueIdentifier |
||
315 | * |
||
316 | * @return mixed |
||
317 | */ |
||
318 | abstract public function validate($value, $valueIdentifier = null); |
||
319 | |||
320 | /** |
||
321 | * Sets the error message prototype that will be used when returning the error message |
||
322 | * when validation fails. |
||
323 | * This option can be used when you need translation |
||
324 | * |
||
325 | * @param ErrorMessage $errorMessagePrototype |
||
326 | * |
||
327 | * @throws \InvalidArgumentException |
||
328 | * @return \Sirius\Validation\Rule\AbstractRule |
||
329 | */ |
||
330 | 23 | public function setErrorMessagePrototype(ErrorMessage $errorMessagePrototype) |
|
336 | |||
337 | /** |
||
338 | * Returns the error message prototype. |
||
339 | * It constructs one if there isn't one. |
||
340 | * |
||
341 | * @return ErrorMessage |
||
342 | */ |
||
343 | 29 | public function getErrorMessagePrototype() |
|
351 | |||
352 | /** |
||
353 | * Retrieve the error message if validation failed |
||
354 | * |
||
355 | * @return NULL|\Sirius\Validation\ErrorMessage |
||
356 | */ |
||
357 | 24 | public function getMessage() |
|
371 | |||
372 | /** |
||
373 | * Retrieve the potential error message. |
||
374 | * Example: when you do client-side validation you need to access the "potential error message" to be displayed |
||
375 | * |
||
376 | * @return ErrorMessage |
||
377 | */ |
||
378 | 28 | public function getPotentialMessage() |
|
386 | |||
387 | /** |
||
388 | * Method for determining the path to a related item. |
||
389 | * Eg: for `lines[5][price]` the related item `lines[*][quantity]` |
||
390 | * has the value identifier as `lines[5][quantity]` |
||
391 | * |
||
392 | * @param $valueIdentifier |
||
393 | * @param $relatedItem |
||
394 | * |
||
395 | * @return string|null |
||
396 | */ |
||
397 | 12 | protected function getRelatedValueIdentifier($valueIdentifier, $relatedItem) |
|
432 | } |
||
433 |