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 ItemBaseTrait 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 ItemBaseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | trait ItemBaseTrait |
||
20 | { |
||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $fetched = false; |
||
25 | |||
26 | /** |
||
27 | * @var DriverAbstract |
||
28 | */ |
||
29 | protected $driver; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $key; |
||
35 | |||
36 | /** |
||
37 | * @var mixed |
||
38 | */ |
||
39 | protected $data; |
||
40 | |||
41 | /** |
||
42 | * @var \DateTime |
||
43 | */ |
||
44 | protected $expirationDate; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $tags = []; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $removedTags = []; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $isHit = false; |
||
60 | |||
61 | /******************** |
||
62 | * |
||
63 | * PSR-6 Methods |
||
64 | * |
||
65 | *******************/ |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getKey() |
||
74 | |||
75 | /** |
||
76 | * @return mixed |
||
77 | */ |
||
78 | public function get() |
||
82 | |||
83 | /** |
||
84 | * @param mixed $value |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function set($value) |
||
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | * @throws \InvalidArgumentException |
||
103 | */ |
||
104 | public function isHit() |
||
108 | |||
109 | /** |
||
110 | * @param bool $isHit |
||
111 | * @return $this |
||
112 | * @throws \InvalidArgumentException |
||
113 | */ |
||
114 | public function setHit($isHit) |
||
124 | |||
125 | /** |
||
126 | * @param \DateTimeInterface $expiration |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function expiresAt($expiration) |
||
152 | |||
153 | /** |
||
154 | * Sets the expiration time for this cache item. |
||
155 | * |
||
156 | * @param int|\DateInterval $time |
||
157 | * The period of time from the present after which the item MUST be considered |
||
158 | * expired. An integer parameter is understood to be the time in seconds until |
||
159 | * expiration. If null is passed explicitly, a default value MAY be used. |
||
160 | * If none is set, the value should be stored permanently or for as long as the |
||
161 | * implementation allows. |
||
162 | * |
||
163 | * @return static |
||
164 | * The called object. |
||
165 | * |
||
166 | * @deprecated Use CacheItemInterface::expiresAfter() instead |
||
167 | */ |
||
168 | public function touch($time) |
||
174 | |||
175 | /** |
||
176 | * @param \DateInterval|int $time |
||
177 | * @return $this |
||
178 | * @throws \InvalidArgumentException |
||
179 | */ |
||
180 | public function expiresAfter($time) |
||
199 | |||
200 | /******************** |
||
201 | * |
||
202 | * PSR-6 Extended Methods |
||
203 | * |
||
204 | *******************/ |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getEncodedKey() |
||
213 | |||
214 | /** |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public function getUncommittedData() |
||
221 | |||
222 | /** |
||
223 | * @return \DateTimeInterface |
||
224 | */ |
||
225 | public function getExpirationDate() |
||
229 | |||
230 | /** |
||
231 | * @return int |
||
232 | */ |
||
233 | public function getTtl() |
||
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isExpired() |
||
250 | |||
251 | /** |
||
252 | * @param int $step |
||
253 | * @return $this |
||
254 | * @throws \InvalidArgumentException |
||
255 | */ |
||
256 | View Code Duplication | public function increment($step = 1) |
|
267 | |||
268 | /** |
||
269 | * @param int $step |
||
270 | * @return $this |
||
271 | * @throws \InvalidArgumentException |
||
272 | */ |
||
273 | View Code Duplication | public function decrement($step = 1) |
|
284 | |||
285 | /** |
||
286 | * @param array|string $data |
||
287 | * @return $this |
||
288 | * @throws \InvalidArgumentException |
||
289 | */ |
||
290 | View Code Duplication | public function append($data) |
|
302 | |||
303 | |||
304 | /** |
||
305 | * @param array|string $data |
||
306 | * @return $this |
||
307 | * @throws \InvalidArgumentException |
||
308 | */ |
||
309 | View Code Duplication | public function prepend($data) |
|
321 | |||
322 | /** |
||
323 | * @param $tagName |
||
324 | * @return $this |
||
325 | * @throws \InvalidArgumentException |
||
326 | */ |
||
327 | public function addTag($tagName) |
||
337 | |||
338 | /** |
||
339 | * @param array $tagNames |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function addTags(array $tagNames) |
||
350 | |||
351 | /** |
||
352 | * @param array $tags |
||
353 | * @return $this |
||
354 | * @throws \InvalidArgumentException |
||
355 | */ |
||
356 | public function setTags(array $tags) |
||
368 | |||
369 | /** |
||
370 | * @return array |
||
371 | */ |
||
372 | public function getTags() |
||
376 | |||
377 | /** |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getTagsAsString($separator = ', ') |
||
384 | |||
385 | /** |
||
386 | * @param $tagName |
||
387 | * @return $this |
||
388 | */ |
||
389 | public function removeTag($tagName) |
||
398 | |||
399 | /** |
||
400 | * @param array $tagNames |
||
401 | * @return $this |
||
402 | */ |
||
403 | public function removeTags(array $tagNames) |
||
411 | |||
412 | /** |
||
413 | * @return array |
||
414 | */ |
||
415 | public function getRemovedTags() |
||
419 | |||
420 | /** |
||
421 | * Return the data as a well-formatted string. |
||
422 | * Any scalar value will be casted to an array |
||
423 | * @param int $option json_encode() options |
||
424 | * @param int $depth json_encode() depth |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
439 | |||
440 | /** |
||
441 | * Implements \JsonSerializable interface |
||
442 | * @return mixed |
||
443 | */ |
||
444 | public function jsonSerialize() |
||
448 | |||
449 | /** |
||
450 | * Prevent recursions for Debug (php 5.6+) |
||
451 | * @return array |
||
452 | */ |
||
453 | final public function __debugInfo() |
||
460 | } |
||
461 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.