Complex classes like ValidatorsManager 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 ValidatorsManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ValidatorsManager { |
||
| 37 | protected static $instanceValidators=[]; |
||
| 38 | public static $validatorTypes=[ |
||
| 39 | "notNull"=>NotNullValidator::class, |
||
| 40 | "isNull"=>IsNullValidator::class, |
||
| 41 | "notEmpty"=>NotEmptyValidator::class, |
||
| 42 | "isEmpty"=>IsEmptyValidator::class, |
||
| 43 | "isTrue"=>IsTrueValidator::class, |
||
| 44 | "isFalse"=>IsFalseValidator::class, |
||
| 45 | "isBool"=>IsBooleanValidator::class, |
||
| 46 | "equals"=>EqualsValidator::class, |
||
| 47 | "type"=>TypeValidator::class, |
||
| 48 | "greaterThan"=>GreaterThanValidator::class, |
||
| 49 | "greaterThanOrEqual"=>GreaterThanOrEqualValidator::class, |
||
| 50 | "lessThan"=>LessThanValidator::class, |
||
| 51 | "lessThanOrEqual"=>LessThanOrEqualValidator::class, |
||
| 52 | "length"=>LengthValidator::class, |
||
| 53 | "id"=>IdValidator::class, |
||
| 54 | "regex"=>RegexValidator::class, |
||
| 55 | "email"=>EmailValidator::class, |
||
| 56 | "url"=>UrlValidator::class, |
||
| 57 | "ip"=>IpValidator::class, |
||
| 58 | "range"=>RangeValidator::class, |
||
| 59 | "date"=>DateValidator::class, |
||
| 60 | "dateTime"=>DateTimeValidator::class, |
||
| 61 | "time"=>TimeValidator::class |
||
| 62 | |||
| 63 | ]; |
||
| 64 | |||
| 65 | protected static $key="contents/validators/"; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Registers a validator type for using with @validator annotation |
||
| 69 | * @param string $type |
||
| 70 | * @param string $validatorClass |
||
| 71 | */ |
||
| 72 | public static function registerType($type,$validatorClass){ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Parses models and save validators in cache |
||
| 78 | * to use in dev only |
||
| 79 | * @param array $config |
||
| 80 | */ |
||
| 81 | public static function initModelsValidators(&$config){ |
||
| 92 | |||
| 93 | protected static function store($model,$validators){ |
||
| 96 | |||
| 97 | protected static function fetch($model){ |
||
| 104 | |||
| 105 | protected static function getGroupValidators(array $validators,$group){ |
||
| 115 | |||
| 116 | protected static function getGroupMemberValidators(array $validators,$group){ |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Validates an instance |
||
| 128 | * @param object $instance |
||
| 129 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 130 | */ |
||
| 131 | public static function validate($instance){ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Validates an array of objects |
||
| 137 | * @param array $instances |
||
| 138 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 139 | */ |
||
| 140 | public static function validateInstances($instances){ |
||
| 150 | |||
| 151 | protected static function validateInstances_($instances,$members){ |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Validates an array of objects using a group of validators |
||
| 169 | * @param array $instances |
||
| 170 | * @param string $group |
||
| 171 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 172 | */ |
||
| 173 | public static function validateInstancesGroup($instances,$group){ |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Validates an instance using a group of validators |
||
| 187 | * @param object $instance |
||
| 188 | * @param string $group |
||
| 189 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 190 | */ |
||
| 191 | public static function validateGroup($instance,$group){ |
||
| 195 | |||
| 196 | protected static function validate_($instance,$members){ |
||
| 215 | |||
| 216 | protected static function initInstancesValidators($instance,$members,$group=null){ |
||
| 233 | |||
| 234 | protected static function getModelCacheKey($classname){ |
||
| 237 | |||
| 238 | protected static function getValidatorInstance($type){ |
||
| 247 | } |
||
| 248 | |||
| 249 |