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 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 |
||
| 37 | class ValidatorsManager { |
||
| 38 | protected static $instanceValidators=[]; |
||
| 39 | protected static $cache; |
||
| 40 | |||
| 41 | public static function start(){ |
||
| 44 | |||
| 45 | public static $validatorTypes=[ |
||
| 46 | "notNull"=>NotNullValidator::class, |
||
| 47 | "isNull"=>IsNullValidator::class, |
||
| 48 | "notEmpty"=>NotEmptyValidator::class, |
||
| 49 | "isEmpty"=>IsEmptyValidator::class, |
||
| 50 | "isTrue"=>IsTrueValidator::class, |
||
| 51 | "isFalse"=>IsFalseValidator::class, |
||
| 52 | "isBool"=>IsBooleanValidator::class, |
||
| 53 | "equals"=>EqualsValidator::class, |
||
| 54 | "type"=>TypeValidator::class, |
||
| 55 | "greaterThan"=>GreaterThanValidator::class, |
||
| 56 | "greaterThanOrEqual"=>GreaterThanOrEqualValidator::class, |
||
| 57 | "lessThan"=>LessThanValidator::class, |
||
| 58 | "lessThanOrEqual"=>LessThanOrEqualValidator::class, |
||
| 59 | "length"=>LengthValidator::class, |
||
| 60 | "id"=>IdValidator::class, |
||
| 61 | "regex"=>RegexValidator::class, |
||
| 62 | "email"=>EmailValidator::class, |
||
| 63 | "url"=>UrlValidator::class, |
||
| 64 | "ip"=>IpValidator::class, |
||
| 65 | "range"=>RangeValidator::class, |
||
| 66 | "date"=>DateValidator::class, |
||
| 67 | "dateTime"=>DateTimeValidator::class, |
||
| 68 | "time"=>TimeValidator::class |
||
| 69 | |||
| 70 | ]; |
||
| 71 | |||
| 72 | protected static $key="contents/validators/"; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Registers a validator type for using with @validator annotation |
||
| 76 | * @param string $type |
||
| 77 | * @param string $validatorClass |
||
| 78 | */ |
||
| 79 | public static function registerType($type,$validatorClass){ |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Parses models and save validators in cache |
||
| 85 | * to use in dev only |
||
| 86 | * @param array $config |
||
| 87 | */ |
||
| 88 | public static function initModelsValidators(&$config){ |
||
| 99 | |||
| 100 | protected static function store($model,$validators){ |
||
| 103 | |||
| 104 | protected static function fetch($model){ |
||
| 111 | |||
| 112 | protected static function getGroupArrayValidators(array $validators,$group){ |
||
| 122 | |||
| 123 | protected static function getGroupMemberValidators(array $validators,$group){ |
||
| 132 | |||
| 133 | private static function getCacheValidators($instance,$group=""){ |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Validates an instance |
||
| 145 | * @param object $instance |
||
| 146 | * @param string $group |
||
| 147 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 148 | */ |
||
| 149 | public static function validate($instance,$group=""){ |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Validates an array of objects |
||
| 163 | * @param array $instances |
||
| 164 | * @param string $group |
||
| 165 | * @return \Ubiquity\contents\validation\validators\ConstraintViolation[] |
||
| 166 | */ |
||
| 167 | public static function validateInstances($instances,$group=""){ |
||
| 181 | |||
| 182 | public static function clearCache($model=null,$group=""){ |
||
| 192 | |||
| 193 | View Code Duplication | protected static function validateInstances_($instances,$members){ |
|
| 207 | |||
| 208 | protected static function validate_($instance,$members){ |
||
| 227 | |||
| 228 | View Code Duplication | protected static function validateFromCache_($instance,$members){ |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Initializes the cache (SessionCache) for the class of înstance |
||
| 243 | * @param object $instance |
||
| 244 | * @param string $group |
||
| 245 | */ |
||
| 246 | public static function initCacheInstanceValidators($instance,$group=""){ |
||
| 251 | |||
| 252 | protected static function initInstancesValidators($instance,$members,$group=""){ |
||
| 272 | |||
| 273 | protected static function getHash($class){ |
||
| 276 | |||
| 277 | protected static function getModelCacheKey($classname){ |
||
| 280 | |||
| 281 | protected static function getValidatorInstance($type){ |
||
| 290 | } |
||
| 291 | |||
| 292 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.