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 setTransformer(TransformerInterface $transformer) |
||
80 | |||
81 | public function setInstantiator(InstantiatorInterface $instantiator) |
||
85 | |||
86 | public function setTypeHandler(TypeHandler $typeHandler) |
||
90 | |||
91 | public function setType(string $type) |
||
95 | |||
96 | public function setTypeAlias(string $typeAlias) |
||
100 | |||
101 | public function getTypeAlias(): string |
||
105 | |||
106 | public function setRequired(bool $required) |
||
110 | |||
111 | public function setDefault($default) |
||
115 | |||
116 | public function setAllowNull(bool $allowNull) |
||
120 | |||
121 | public function getDefault() |
||
125 | |||
126 | public function hasDefault(): bool |
||
130 | |||
131 | public function add(string $key, string $type, array $options = []): BaseNode |
||
172 | |||
173 | public function remove(string $key) |
||
177 | |||
178 | /** |
||
179 | * @return BaseNode[] |
||
180 | */ |
||
181 | public function getChildren(): array |
||
185 | |||
186 | public function hasChildren(): bool |
||
190 | |||
191 | public function isRequired(): bool |
||
199 | |||
200 | public function allowNull(): bool |
||
204 | |||
205 | public function getValue(string $field, $value) |
||
219 | |||
220 | public function walk($input) |
||
246 | |||
247 | protected function checkConstraints(string $field, $value) |
||
255 | } |
||
256 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.