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 |
||
17 | class StaticDateEntity extends AbstractEntity |
||
18 | { |
||
19 | /** |
||
20 | * List of fields must be hidden from publicFields() method. |
||
21 | * |
||
22 | * @see publicFields() |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | const HIDDEN = []; |
||
27 | |||
28 | /** |
||
29 | * Set of fields allowed to be filled using setFields() method. |
||
30 | * |
||
31 | * @see setFields() |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | const FILLABLE = []; |
||
36 | |||
37 | /** |
||
38 | * List of fields not allowed to be filled by setFields() method. Replace with and empty array |
||
39 | * to allow all fields. |
||
40 | * |
||
41 | * By default all entity fields are settable! Opposite behaviour has to be described in entity |
||
42 | * child implementations. |
||
43 | * |
||
44 | * @see setFields() |
||
45 | * |
||
46 | * @var array|string |
||
47 | */ |
||
48 | const SECURED = []; |
||
49 | |||
50 | /** |
||
51 | * @see setField() |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | const SETTERS = []; |
||
56 | |||
57 | /** |
||
58 | * @see getField() |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | const GETTERS = []; |
||
63 | |||
64 | /** |
||
65 | * Accessor used to mock field data and filter every request thought itself. |
||
66 | * |
||
67 | * @see getField() |
||
68 | * @see setField() |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | const ACCESSORS = []; |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | * |
||
77 | * Include every composition public data into result. |
||
78 | */ |
||
79 | View Code Duplication | public function publicFields(): array |
|
100 | |||
101 | /** |
||
102 | * Check if field can be set using setFields() method. |
||
103 | * |
||
104 | * @see setField() |
||
105 | * @see $fillable |
||
106 | * @see $secured |
||
107 | * |
||
108 | * @param string $field |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | protected function isFillable(string $field): bool |
||
124 | |||
125 | /** |
||
126 | * Check and return name of mutator (getter, setter, accessor) associated with specific field. |
||
127 | * |
||
128 | * @param string $field |
||
129 | * @param string $mutator Mutator type (setter, getter, accessor). |
||
130 | * |
||
131 | * @return mixed|null |
||
132 | * |
||
133 | * @throws EntityException |
||
134 | */ |
||
135 | protected function getMutator(string $field, string $mutator) |
||
156 | } |
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.