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 |
||
| 15 | class BaseNode |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var ConstraintInterface[] |
||
| 19 | */ |
||
| 20 | protected $constraints = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var TransformerInterface |
||
| 24 | */ |
||
| 25 | protected $transformer; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var InstantiatorInterface |
||
| 29 | */ |
||
| 30 | protected $instantiator; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $type = 'array'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $typeAlias = 'array'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $required = true; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var mixed |
||
| 49 | */ |
||
| 50 | protected $default; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var BaseNode[] |
||
| 54 | */ |
||
| 55 | protected $children = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var TypeHandler |
||
| 59 | */ |
||
| 60 | protected $typeHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $allowNull = false; |
||
| 66 | |||
| 67 | public function setConstraints(array $constraints) |
||
| 71 | |||
| 72 | public function addConstraint(ConstraintInterface $constraint) |
||
| 76 | |||
| 77 | public function addConstraints(array $constraints) |
||
| 81 | |||
| 82 | public function setTransformer(TransformerInterface $transformer) |
||
| 86 | |||
| 87 | public function setInstantiator(InstantiatorInterface $instantiator) |
||
| 91 | |||
| 92 | public function setTypeHandler(TypeHandler $typeHandler) |
||
| 96 | |||
| 97 | public function setType(string $type) |
||
| 101 | |||
| 102 | public function setTypeAlias(string $typeAlias) |
||
| 106 | |||
| 107 | public function getTypeAlias(): string |
||
| 111 | |||
| 112 | public function setRequired(bool $required) |
||
| 116 | |||
| 117 | public function setDefault($default) |
||
| 121 | |||
| 122 | public function setAllowNull(bool $allowNull) |
||
| 126 | |||
| 127 | public function getDefault() |
||
| 131 | |||
| 132 | public function hasDefault(): bool |
||
| 136 | |||
| 137 | public function add(string $key, string $type, array $options = []): BaseNode |
||
| 178 | |||
| 179 | public function remove(string $key) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return BaseNode[] |
||
| 186 | */ |
||
| 187 | public function getChildren(): array |
||
| 191 | |||
| 192 | public function hasChildren(): bool |
||
| 196 | |||
| 197 | public function isRequired(): bool |
||
| 205 | |||
| 206 | public function allowNull(): bool |
||
| 210 | |||
| 211 | public function getValue(string $field, $value) |
||
| 225 | |||
| 226 | public function walk($input) |
||
| 252 | |||
| 253 | protected function checkConstraints(string $field, $value) |
||
| 261 | } |
||
| 262 |
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..