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 ItemExtendedTrait 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 ItemExtendedTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | trait ItemExtendedTrait |
||
24 | { |
||
25 | /******************** |
||
26 | * |
||
27 | * PSR-6 Extended Methods |
||
28 | * |
||
29 | *******************/ |
||
30 | |||
31 | /** |
||
32 | * @var EventManager |
||
33 | */ |
||
34 | protected $eventManager; |
||
35 | |||
36 | /** |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function getUncommittedData() |
||
43 | |||
44 | /** |
||
45 | * @return \DateTimeInterface |
||
46 | */ |
||
47 | public function getExpirationDate() |
||
51 | |||
52 | /** |
||
53 | * Alias of expireAt() with forced $expiration param |
||
54 | * |
||
55 | * @param \DateTimeInterface $expiration |
||
56 | * The point in time after which the item MUST be considered expired. |
||
57 | * If null is passed explicitly, a default value MAY be used. If none is set, |
||
58 | * the value should be stored permanently or for as long as the |
||
59 | * implementation allows. |
||
60 | * |
||
61 | * @return static |
||
62 | * The called object. |
||
63 | */ |
||
64 | public function setExpirationDate(\DateTimeInterface $expiration) |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @return \DateTimeInterface |
||
72 | * @throws \LogicException |
||
73 | */ |
||
74 | public function getCreationDate() |
||
82 | |||
83 | /** |
||
84 | * @param \DateTimeInterface $date |
||
85 | * @return $this |
||
86 | * @throws \LogicException |
||
87 | */ |
||
88 | View Code Duplication | public function setCreationDate(\DateTimeInterface $date) |
|
97 | |||
98 | /** |
||
99 | * @return \DateTimeInterface |
||
100 | * @throws \LogicException |
||
101 | */ |
||
102 | public function getModificationDate() |
||
110 | |||
111 | /** |
||
112 | * @param \DateTimeInterface $date |
||
113 | * @return $this |
||
114 | * @throws \LogicException |
||
115 | */ |
||
116 | View Code Duplication | public function setModificationDate(\DateTimeInterface $date) |
|
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | public function getTtl() |
||
138 | |||
139 | /** |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isExpired() |
||
146 | |||
147 | /** |
||
148 | * @param int $step |
||
149 | * @return $this |
||
150 | * @throws \InvalidArgumentException |
||
151 | */ |
||
152 | View Code Duplication | public function increment($step = 1) |
|
163 | |||
164 | /** |
||
165 | * @param int $step |
||
166 | * @return $this |
||
167 | * @throws \InvalidArgumentException |
||
168 | */ |
||
169 | View Code Duplication | public function decrement($step = 1) |
|
180 | |||
181 | /** |
||
182 | * @param array|string $data |
||
183 | * @return $this |
||
184 | * @throws \InvalidArgumentException |
||
185 | */ |
||
186 | View Code Duplication | public function append($data) |
|
198 | |||
199 | |||
200 | /** |
||
201 | * @param array|string $data |
||
202 | * @return $this |
||
203 | * @throws \InvalidArgumentException |
||
204 | */ |
||
205 | View Code Duplication | public function prepend($data) |
|
217 | |||
218 | /** |
||
219 | * @param $tagName |
||
220 | * @return $this |
||
221 | * @throws \InvalidArgumentException |
||
222 | */ |
||
223 | public function addTag($tagName) |
||
233 | |||
234 | /** |
||
235 | * @param array $tagNames |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function addTags(array $tagNames) |
||
246 | |||
247 | /** |
||
248 | * @param array $tags |
||
249 | * @return $this |
||
250 | * @throws \InvalidArgumentException |
||
251 | */ |
||
252 | public function setTags(array $tags) |
||
264 | |||
265 | /** |
||
266 | * @return array |
||
267 | */ |
||
268 | public function getTags() |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getTagsAsString($separator = ', ') |
||
280 | |||
281 | /** |
||
282 | * @param $tagName |
||
283 | * @return $this |
||
284 | */ |
||
285 | public function removeTag($tagName) |
||
294 | |||
295 | /** |
||
296 | * @param array $tagNames |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function removeTags(array $tagNames) |
||
307 | |||
308 | /** |
||
309 | * @return array |
||
310 | */ |
||
311 | public function getRemovedTags() |
||
315 | |||
316 | /** |
||
317 | * Return the data as a well-formatted string. |
||
318 | * Any scalar value will be casted to an array |
||
319 | * @param int $option json_encode() options |
||
320 | * @param int $depth json_encode() depth |
||
321 | * @return string |
||
322 | */ |
||
323 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
335 | |||
336 | /** |
||
337 | * Implements \JsonSerializable interface |
||
338 | * @return mixed |
||
339 | */ |
||
340 | public function jsonSerialize() |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Set the EventManager instance |
||
348 | * |
||
349 | * @param EventManager $em |
||
350 | */ |
||
351 | public function setEventManager(EventManager $em) |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Prevent recursions for Debug (php 5.6+) |
||
359 | * @return array |
||
360 | */ |
||
361 | final public function __debugInfo() |
||
368 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: