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 |
||
26 | class ReflectionEntity |
||
27 | { |
||
28 | /** |
||
29 | * Required to validly merge parent and children attributes. |
||
30 | */ |
||
31 | const BASE_CLASS = AbstractEntity::class; |
||
32 | |||
33 | /** |
||
34 | * Properties cache. |
||
35 | * |
||
36 | * @invisible |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $cache = []; |
||
41 | |||
42 | /** |
||
43 | * @var \ReflectionClass |
||
44 | */ |
||
45 | private $reflection = null; |
||
46 | |||
47 | /** |
||
48 | * Only support SchematicEntity classes! |
||
49 | * |
||
50 | * @param string $class |
||
51 | */ |
||
52 | public function __construct(string $class) |
||
56 | |||
57 | /** |
||
58 | * @return \ReflectionClass |
||
59 | */ |
||
60 | public function getReflection(): \ReflectionClass |
||
64 | |||
65 | /** |
||
66 | * @return array|string |
||
67 | */ |
||
68 | public function getSecured() |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getFillable(): array |
||
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getHidden(): array |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getSetters(): array |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getGetters(): array |
||
108 | |||
109 | /** |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getAccessors(): array |
||
116 | |||
117 | /** |
||
118 | * Get methods declared in current class and exclude methods declared in parents. |
||
119 | * |
||
120 | * @return \ReflectionMethod[] |
||
121 | */ |
||
122 | public function declareMethods(): array |
||
135 | |||
136 | /** |
||
137 | * Entity schema. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getSchema(): array |
||
146 | |||
147 | /** |
||
148 | * Model mutators grouped by their type. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getMutators(): array |
||
174 | |||
175 | /** |
||
176 | * Read default model property value, will read "protected" and "private" properties. Method |
||
177 | * raises entity event "describe" to allow it traits modify needed values. |
||
178 | * |
||
179 | * @param string $property Property name. |
||
180 | * @param bool $merge If true value will be merged with all parent declarations. |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | public function getProperty(string $property, bool $merge = false) |
||
223 | |||
224 | /** |
||
225 | * Parent entity schema/ |
||
226 | * |
||
227 | * @return ReflectionEntity|null |
||
228 | */ |
||
229 | public function parentReflection() |
||
242 | |||
243 | /** |
||
244 | * Bypassing call to reflection. |
||
245 | * |
||
246 | * @param string $name |
||
247 | * @param array $arguments |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function __call(string $name, array $arguments) |
||
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | public function __toString(): string |
||
263 | |||
264 | /** |
||
265 | * Cloning and flushing cache. |
||
266 | */ |
||
267 | public function __clone() |
||
271 | } |
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.