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:
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 bool |
||
38 | */ |
||
39 | protected $required = true; |
||
40 | |||
41 | /** |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $default; |
||
45 | |||
46 | /** |
||
47 | * @var BaseNode[] |
||
48 | */ |
||
49 | protected $children = []; |
||
50 | |||
51 | /** |
||
52 | * @var TypeHandler |
||
53 | */ |
||
54 | protected $typeHandler; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $allowNull = false; |
||
60 | |||
61 | public function setConstraints(array $constraints) |
||
65 | |||
66 | public function addConstraint(ConstraintInterface $constraint) |
||
70 | |||
71 | public function setTransformer(TransformerInterface $transformer) |
||
75 | |||
76 | public function setInstantiator(InstantiatorInterface $instantiator) |
||
80 | |||
81 | public function setTypeHandler(TypeHandler $typeHandler) |
||
85 | |||
86 | public function setType(string $type) |
||
90 | |||
91 | public function setRequired(bool $required) |
||
95 | |||
96 | public function setDefault($default) |
||
100 | |||
101 | public function setAllowNull(bool $allowNull) |
||
105 | |||
106 | public function getDefault() |
||
110 | |||
111 | public function hasDefault(): bool |
||
115 | |||
116 | public function add(string $key, string $type, array $options = []): BaseNode |
||
157 | |||
158 | public function remove(string $key) |
||
162 | |||
163 | /** |
||
164 | * @return BaseNode[] |
||
165 | */ |
||
166 | public function getChildren(): array |
||
170 | |||
171 | public function hasChildren(): bool |
||
175 | |||
176 | public function isRequired(): bool |
||
184 | |||
185 | public function allowNull(): bool |
||
189 | |||
190 | public function getValue(string $field, $value) |
||
204 | |||
205 | public function walk($input) |
||
231 | |||
232 | protected function checkConstraints(string $field, $value) |
||
240 | } |
||
241 |
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.