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 BaseNode 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 BaseNode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class BaseNode |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var ConstraintInterface[] |
||
| 18 | */ |
||
| 19 | protected $constraints = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var TransformerInterface |
||
| 23 | */ |
||
| 24 | protected $transformer; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var InstantiatorInterface |
||
| 28 | */ |
||
| 29 | protected $instantiator; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $type = 'array'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $typeAlias = 'array'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $required = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | protected $default; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var BaseNode[] |
||
| 53 | */ |
||
| 54 | protected $children = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var TypeHandler |
||
| 58 | */ |
||
| 59 | protected $typeHandler; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $allowNull = false; |
||
| 65 | |||
| 66 | public function setConstraints(array $constraints) |
||
| 70 | |||
| 71 | public function addConstraint(ConstraintInterface $constraint) |
||
| 75 | |||
| 76 | public function addConstraints(array $constraints) |
||
| 80 | |||
| 81 | public function setTransformer(TransformerInterface $transformer) |
||
| 85 | |||
| 86 | public function setInstantiator(InstantiatorInterface $instantiator) |
||
| 90 | |||
| 91 | public function setTypeHandler(TypeHandler $typeHandler) |
||
| 95 | |||
| 96 | public function setType(string $type) |
||
| 100 | |||
| 101 | public function setTypeAlias(string $typeAlias) |
||
| 105 | |||
| 106 | public function getTypeAlias(): string |
||
| 110 | |||
| 111 | public function setRequired(bool $required) |
||
| 115 | |||
| 116 | public function setDefault($default) |
||
| 120 | |||
| 121 | public function setAllowNull(bool $allowNull) |
||
| 125 | |||
| 126 | public function getDefault() |
||
| 130 | |||
| 131 | public function hasDefault(): bool |
||
| 135 | |||
| 136 | public function add(string $key, string $type, array $options = []): BaseNode |
||
| 177 | |||
| 178 | public function remove(string $key) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return BaseNode[] |
||
| 185 | */ |
||
| 186 | public function getChildren(): array |
||
| 190 | |||
| 191 | public function hasChildren(): bool |
||
| 195 | |||
| 196 | public function isRequired(): bool |
||
| 204 | |||
| 205 | public function allowNull(): bool |
||
| 209 | |||
| 210 | public function getValue(string $field, $value) |
||
| 224 | |||
| 225 | public function walk($input) |
||
| 251 | |||
| 252 | protected function checkConstraints(string $field, $value) |
||
| 260 | } |
||
| 261 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..