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 SimpleTypeBase 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 SimpleTypeBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class SimpleTypeBase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @Exclude |
||
| 23 | * @var array Defines a list of acceptable values |
||
| 24 | */ |
||
| 25 | private $enumeration = array(); |
||
| 26 | /** |
||
| 27 | * @Exclude |
||
| 28 | * @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
||
| 29 | */ |
||
| 30 | private $maxLength = null; |
||
| 31 | /** |
||
| 32 | * @Exclude |
||
| 33 | * @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
||
| 34 | */ |
||
| 35 | private $minLength = null; |
||
| 36 | /** |
||
| 37 | * @Exclude |
||
| 38 | * @var string Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |
||
| 39 | */ |
||
| 40 | private $whiteSpace = "preserve"; |
||
| 41 | /** |
||
| 42 | * @Exclude |
||
| 43 | * @var string Defines the exact sequence of characters that are acceptable |
||
| 44 | */ |
||
| 45 | private $pattern = null; |
||
| 46 | /** |
||
| 47 | * @Exclude |
||
| 48 | * @var string Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
||
| 49 | */ |
||
| 50 | private $fractionDigits = null; |
||
| 51 | /** |
||
| 52 | * @Exclude |
||
| 53 | * @var integer Specifies the lower bounds for numeric values (the value must be greater than this value) |
||
| 54 | */ |
||
| 55 | private $minExclusive = null; |
||
| 56 | /** |
||
| 57 | * @Exclude |
||
| 58 | * @var integer Specifies the upper bounds for numeric values (the value must be less than this value) |
||
| 59 | */ |
||
| 60 | private $maxExclusive = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @property mixed $__value |
||
| 64 | */ |
||
| 65 | private $__value = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Construct |
||
| 69 | * |
||
| 70 | * @param mixed $value |
||
| 71 | */ |
||
| 72 | public function __construct($value) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Gets or sets the inner value |
||
| 79 | * |
||
| 80 | * @param mixed ...$value |
||
| 81 | * @return mixed |
||
| 82 | * @throws \Exception |
||
| 83 | */ |
||
| 84 | public function value(...$value) |
||
| 94 | |||
| 95 | protected function fixValue($v) |
||
| 101 | |||
| 102 | protected function fixWhitespace($val, $handle = "preserve") |
||
| 115 | |||
| 116 | protected function fixFractionDigits($val,$fractionDigits = null){ |
||
| 122 | |||
| 123 | private function isBaseValid($v) |
||
| 131 | |||
| 132 | View Code Duplication | private function checkMinLength($v) |
|
| 142 | |||
| 143 | View Code Duplication | private function checkMaxLength($v) |
|
| 153 | |||
| 154 | private function checkEnumeration($v) |
||
| 161 | |||
| 162 | private function checkPattern($v) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Checks a pattern against a string |
||
| 173 | * @param string $pattern the regex pattern |
||
| 174 | * @param string $string the string to check |
||
| 175 | * @return bool true if string matches pattern |
||
| 176 | */ |
||
| 177 | private function matchesRegexPattern($pattern, $string) |
||
| 182 | |||
| 183 | abstract protected function isValid($v); |
||
| 184 | |||
| 185 | public function __set($name, $value) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets a string value |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function __toString() |
||
| 205 | |||
| 206 | private function setEnumoration($value) |
||
| 216 | |||
| 217 | private function setLength($value) |
||
| 222 | |||
| 223 | private function setMinLength($value) |
||
| 228 | |||
| 229 | private function checkLength($value, $min = 0) |
||
| 238 | |||
| 239 | private function setMaxLength($value) |
||
| 244 | |||
| 245 | private function setWhiteSpaceHandle($value) |
||
| 252 | |||
| 253 | private function setPattern($value) |
||
| 263 | |||
| 264 | private function checkRegexValidPattern($pattern) |
||
| 268 | |||
| 269 | private function setFractionDigits($value) |
||
| 274 | |||
| 275 | private function setMinExclusive($value) |
||
| 280 | |||
| 281 | private function setMaxExclusive($value) |
||
| 286 | } |
||
| 287 |