Complex classes like ValidatorResolver 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 ValidatorResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface |
||
28 | { |
||
29 | /** |
||
30 | * Match validator names and options |
||
31 | * @todo: adjust [a-z0-9_:.\\\\] once Tx_Extbase_Foo syntax is outdated. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const PATTERN_MATCH_VALIDATORS = '/ |
||
36 | (?:^|,\s*) |
||
37 | (?P<validatorName>[a-z0-9_:.\\\\]+) |
||
38 | \s* |
||
39 | (?:\( |
||
40 | (?P<validatorOptions>(?:\s*[a-z0-9]+\s*=\s*(?: |
||
41 | "(?:\\\\"|[^"])*" |
||
42 | |\'(?:\\\\\'|[^\'])*\' |
||
43 | |(?:\s|[^,"\']*) |
||
44 | )(?:\s|,)*)*) |
||
45 | \))? |
||
46 | /ixS'; |
||
47 | |||
48 | /** |
||
49 | * Match validator options (to parse actual options) |
||
50 | * @var string |
||
51 | */ |
||
52 | const PATTERN_MATCH_VALIDATOROPTIONS = '/ |
||
53 | \s* |
||
54 | (?P<optionName>[a-z0-9]+) |
||
55 | \s*=\s* |
||
56 | (?P<optionValue> |
||
57 | "(?:\\\\"|[^"])*" |
||
58 | |\'(?:\\\\\'|[^\'])*\' |
||
59 | |(?:\s|[^,"\']*) |
||
60 | ) |
||
61 | /ixS'; |
||
62 | |||
63 | /** |
||
64 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
65 | */ |
||
66 | protected $objectManager; |
||
67 | |||
68 | /** |
||
69 | * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService |
||
70 | */ |
||
71 | protected $reflectionService; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $baseValidatorConjunctions = []; |
||
77 | |||
78 | /** |
||
79 | * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager |
||
80 | */ |
||
81 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) |
||
82 | { |
||
83 | $this->objectManager = $objectManager; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param ReflectionService $reflectionService |
||
88 | */ |
||
89 | public function injectReflectionService(ReflectionService $reflectionService) |
||
90 | { |
||
91 | $this->reflectionService = $reflectionService; |
||
|
|||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get a validator for a given data type. Returns a validator implementing |
||
96 | * the \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface or NULL if no validator |
||
97 | * could be resolved. |
||
98 | * |
||
99 | * @param string $validatorType Either one of the built-in data types or fully qualified validator class name |
||
100 | * @param array $validatorOptions Options to be passed to the validator |
||
101 | * @return \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface Validator or NULL if none found. |
||
102 | */ |
||
103 | public function createValidator($validatorType, array $validatorOptions = []) |
||
104 | { |
||
105 | try { |
||
106 | /** |
||
107 | * @todo remove throwing Exceptions in resolveValidatorObjectName |
||
108 | */ |
||
109 | $validatorObjectName = $this->resolveValidatorObjectName($validatorType); |
||
110 | |||
111 | $validator = $this->objectManager->get($validatorObjectName, $validatorOptions); |
||
112 | |||
113 | if (!($validator instanceof \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface)) { |
||
114 | throw new Exception\NoSuchValidatorException('The validator "' . $validatorObjectName . '" does not implement TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface!', 1300694875); |
||
115 | } |
||
116 | |||
117 | return $validator; |
||
118 | } catch (NoSuchValidatorException $e) { |
||
119 | return null; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Resolves and returns the base validator conjunction for the given data type. |
||
125 | * |
||
126 | * If no validator could be resolved (which usually means that no validation is necessary), |
||
127 | * NULL is returned. |
||
128 | * |
||
129 | * @param string $targetClassName The data type to search a validator for. Usually the fully qualified object name |
||
130 | * @return ConjunctionValidator The validator conjunction or NULL |
||
131 | */ |
||
132 | public function getBaseValidatorConjunction($targetClassName) |
||
140 | |||
141 | /** |
||
142 | * Detects and registers any validators for arguments: |
||
143 | * - by the data type specified in the param annotations |
||
144 | * - additional validators specified in the validate annotations of a method |
||
145 | * |
||
146 | * @param string $className |
||
147 | * @param string $methodName |
||
148 | * @param array $methodParameters Optional pre-compiled array of method parameters |
||
149 | * @param array $methodValidateAnnotations Optional pre-compiled array of validate annotations (as array) |
||
150 | * @return ConjunctionValidator[] An Array of ValidatorConjunctions for each method parameters. |
||
151 | * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException |
||
152 | * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException |
||
153 | * @throws \TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException |
||
154 | */ |
||
155 | public function buildMethodArgumentsValidatorConjunctions($className, $methodName, array $methodParameters = null, array $methodValidateAnnotations = null) |
||
217 | |||
218 | /** |
||
219 | * Builds a chain of nested object validators by specification of the given |
||
220 | * object path. |
||
221 | * |
||
222 | * @param array $objectPath The object path |
||
223 | * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $propertyValidator The validator which should be added to the property specified by objectPath |
||
224 | * @return \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator |
||
225 | */ |
||
226 | protected function buildSubObjectValidator(array $objectPath, \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $propertyValidator) |
||
242 | |||
243 | /** |
||
244 | * Builds a base validator conjunction for the given data type. |
||
245 | * |
||
246 | * The base validation rules are those which were declared directly in a class (typically |
||
247 | * a model) through some validate annotations on properties. |
||
248 | * |
||
249 | * If a property holds a class for which a base validator exists, that property will be |
||
250 | * checked as well, regardless of a validate annotation |
||
251 | * |
||
252 | * Additionally, if a custom validator was defined for the class in question, it will be added |
||
253 | * to the end of the conjunction. A custom validator is found if it follows the naming convention |
||
254 | * "Replace '\Model\' by '\Validator\' and append 'Validator'". |
||
255 | * |
||
256 | * Example: $targetClassName is TYPO3\Foo\Domain\Model\Quux, then the validator will be found if it has the |
||
257 | * name TYPO3\Foo\Domain\Validator\QuuxValidator |
||
258 | * |
||
259 | * @param string $indexKey The key to use as index in $this->baseValidatorConjunctions; calculated from target class name and validation groups |
||
260 | * @param string $targetClassName The data type to build the validation conjunction for. Needs to be the fully qualified class name. |
||
261 | * @param array $validationGroups The validation groups to build the validator for |
||
262 | * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException |
||
263 | * @throws \InvalidArgumentException |
||
264 | */ |
||
265 | protected function buildBaseValidatorConjunction($indexKey, $targetClassName, array $validationGroups = []) |
||
333 | |||
334 | /** |
||
335 | * This adds custom validators to the passed $conjunctionValidator. |
||
336 | * |
||
337 | * A custom validator is found if it follows the naming convention "Replace '\Model\' by '\Validator\' and |
||
338 | * append 'Validator'". If found, it will be added to the $conjunctionValidator. |
||
339 | * |
||
340 | * In addition canValidate() will be called on all implementations of the ObjectValidatorInterface to find |
||
341 | * all validators that could validate the target. The one with the highest priority will be added as well. |
||
342 | * If multiple validators have the same priority, which one will be added is not deterministic. |
||
343 | * |
||
344 | * @param string $targetClassName |
||
345 | * @param ConjunctionValidator $conjunctionValidator |
||
346 | * @return Validator\ObjectValidatorInterface|null |
||
347 | */ |
||
348 | protected function addCustomValidators($targetClassName, ConjunctionValidator &$conjunctionValidator) |
||
360 | |||
361 | /** |
||
362 | * Parses the validator options given in @validate annotations. |
||
363 | * |
||
364 | * @param string $validateValue |
||
365 | * @return array |
||
366 | */ |
||
367 | protected function parseValidatorAnnotation($validateValue) |
||
387 | |||
388 | /** |
||
389 | * Parses $rawValidatorOptions not containing quoted option values. |
||
390 | * $rawValidatorOptions will be an empty string afterwards (pass by ref!). |
||
391 | * |
||
392 | * @param string $rawValidatorOptions |
||
393 | * @return array An array of optionName/optionValue pairs |
||
394 | */ |
||
395 | protected function parseValidatorOptions($rawValidatorOptions) |
||
406 | |||
407 | /** |
||
408 | * Removes escapings from a given argument string and trims the outermost |
||
409 | * quotes. |
||
410 | * |
||
411 | * This method is meant as a helper for regular expression results. |
||
412 | * |
||
413 | * @param string &$quotedValue Value to unquote |
||
414 | */ |
||
415 | protected function unquoteString(&$quotedValue) |
||
427 | |||
428 | /** |
||
429 | * Returns an object of an appropriate validator for the given class. If no validator is available |
||
430 | * FALSE is returned |
||
431 | * |
||
432 | * @param string $validatorName Either the fully qualified class name of the validator or the short name of a built-in validator |
||
433 | * |
||
434 | * @throws Exception\NoSuchValidatorException |
||
435 | * @return string Name of the validator object |
||
436 | */ |
||
437 | protected function resolveValidatorObjectName($validatorName) |
||
484 | |||
485 | /** |
||
486 | * Used to map PHP types to validator types. |
||
487 | * |
||
488 | * @param string $type Data type to unify |
||
489 | * @return string unified data type |
||
490 | */ |
||
491 | protected function getValidatorType($type) |
||
514 | |||
515 | /** |
||
516 | * Temporary replacement for $this->reflectionService->getMethodAnnotations() |
||
517 | * |
||
518 | * @param string $className |
||
519 | * @param string $methodName |
||
520 | * |
||
521 | * @return array |
||
522 | */ |
||
523 | public function getMethodValidateAnnotations($className, $methodName) |
||
543 | } |
||
544 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.