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 BaseField 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 BaseField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class BaseField |
||
| 8 | { |
||
| 9 | public function __construct($descriptor=null) |
||
| 13 | |||
| 14 | public function descriptor() |
||
| 18 | |||
| 19 | public function fullDescriptor() |
||
| 26 | |||
| 27 | public function name() |
||
| 31 | |||
| 32 | public function format() |
||
| 36 | |||
| 37 | public function constraints() |
||
| 45 | |||
| 46 | public function required() |
||
| 50 | |||
| 51 | public function unique() |
||
| 55 | |||
| 56 | public function disableConstraints() |
||
| 61 | |||
| 62 | public function enum() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * try to create a field object based on the descriptor |
||
| 73 | * by default uses the type attribute |
||
| 74 | * return the created field object or false if the descriptor does not match this field |
||
| 75 | * @param object $descriptor |
||
| 76 | * @return bool|BaseField |
||
| 77 | */ |
||
| 78 | public static function inferDescriptor($descriptor) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * try to create a new field object based on the given value |
||
| 89 | * @param mixed $val |
||
| 90 | * @param null|object $descriptor |
||
| 91 | * @param bool @lenient |
||
| 92 | * @return bool|BaseField |
||
| 93 | */ |
||
| 94 | public static function infer($val, $descriptor=null, $lenient=false) |
||
| 105 | |||
| 106 | public function inferProperties($val, $lenient) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param mixed $val |
||
| 115 | * @return mixed |
||
| 116 | * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException; |
||
| 117 | */ |
||
| 118 | final public function castValue($val) |
||
| 127 | |||
| 128 | public function validateValue($val) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * get a unique identifier for this field |
||
| 140 | * used in the inferring process |
||
| 141 | * this is usually the type, but can be modified to support more advanced inferring process |
||
| 142 | * @param bool @lenient |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getInferIdentifier($lenient=false) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * should be implemented by extending classes to return the table schema type of this field |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | static public function type() |
||
| 158 | |||
| 159 | protected $descriptor; |
||
| 160 | protected $constraintsDisabled = false; |
||
| 161 | |||
| 162 | protected function getValidationException($errorMsg, $val=null) |
||
| 172 | |||
| 173 | protected function isEmptyValue($val) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param mixed $val |
||
| 180 | * @return mixed |
||
| 181 | * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException; |
||
| 182 | */ |
||
| 183 | protected function validateCastValue($val) |
||
| 198 | |||
| 199 | protected function checkConstraints($val) |
||
| 260 | |||
| 261 | protected function checkPatternConstraint($val, $pattern) |
||
| 265 | |||
| 266 | protected function checkMinimumConstraint($val, $minConstraint) |
||
| 270 | |||
| 271 | protected function checkMaximumConstraint($val, $maxConstraint) |
||
| 275 | |||
| 276 | protected function checkMinLengthConstraint($val, $minLength) |
||
| 280 | |||
| 281 | protected function checkMaxLengthConstraint($val, $maxLength) |
||
| 285 | |||
| 286 | protected function getAllowedValues() |
||
| 294 | |||
| 295 | protected function castValueNoConstraints($val) |
||
| 302 | } |