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 $whiteSpaceHandle = "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) |
||
| 99 | |||
| 100 | protected function fixWhitespace($val, $handle = "preserve") |
||
| 113 | |||
| 114 | private function isBaseValid($v) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Checks a pattern against a string |
||
| 149 | * @param string $pattern the regex pattern |
||
| 150 | * @param string $string the string to check |
||
| 151 | * @return bool true if string matches pattern |
||
| 152 | */ |
||
| 153 | private function matchesRegexPattern($pattern, $string) |
||
| 158 | |||
| 159 | abstract protected function isValid($v); |
||
| 160 | |||
| 161 | public function __set($name, $value) |
||
| 203 | |||
| 204 | private function setEnumoration($value) |
||
| 214 | |||
| 215 | private function setMaxLength($value) |
||
| 220 | |||
| 221 | private function checkLength($value, $min = 0) |
||
| 230 | |||
| 231 | private function setMinLength($value) |
||
| 236 | |||
| 237 | private function setWhiteSpaceHandle($value) |
||
| 244 | |||
| 245 | private function setPattern($value) |
||
| 255 | |||
| 256 | private function checkPattern($pattern) |
||
| 260 | |||
| 261 | private function setFractionDigits($value) |
||
| 266 | |||
| 267 | private function setMinExclusive($value) |
||
| 272 | |||
| 273 | private function setMaxExclusive($value) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Gets a string value |
||
| 281 | * |
||
| 282 | * @return mixed |
||
| 283 | */ |
||
| 284 | public function __toString() |
||
| 288 | |||
| 289 | private function checkMinLength($v) |
||
| 299 | } |
||
| 300 |
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.