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) |
||
139 | |||
140 | /** |
||
141 | * Sets the expiration time for this cache item. |
||
142 | * |
||
143 | * @param int|\DateInterval $time |
||
144 | * The period of time from the present after which the item MUST be considered |
||
145 | * expired. An integer parameter is understood to be the time in seconds until |
||
146 | * expiration. If null is passed explicitly, a default value MAY be used. |
||
147 | * If none is set, the value should be stored permanently or for as long as the |
||
148 | * implementation allows. |
||
149 | * |
||
150 | * @return static |
||
151 | * The called object. |
||
152 | * |
||
153 | * @deprecated Use CacheItemInterface::expiresAfter() instead |
||
154 | */ |
||
155 | public function touch($time) |
||
161 | |||
162 | /** |
||
163 | * @param \DateInterval|int $time |
||
164 | * @return $this |
||
165 | * @throws \InvalidArgumentException |
||
166 | */ |
||
167 | public function expiresAfter($time) |
||
186 | |||
187 | /******************** |
||
188 | * |
||
189 | * PSR-6 Extended Methods |
||
190 | * |
||
191 | *******************/ |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getEncodedKey() |
||
200 | |||
201 | /** |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function getUncommittedData() |
||
208 | |||
209 | /** |
||
210 | * @return \DateTimeInterface |
||
211 | */ |
||
212 | public function getExpirationDate() |
||
216 | |||
217 | /** |
||
218 | * @return int |
||
219 | */ |
||
220 | public function getTtl() |
||
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function isExpired() |
||
237 | |||
238 | /** |
||
239 | * @param int $step |
||
240 | * @return $this |
||
241 | * @throws \InvalidArgumentException |
||
242 | */ |
||
243 | View Code Duplication | public function increment($step = 1) |
|
254 | |||
255 | /** |
||
256 | * @param int $step |
||
257 | * @return $this |
||
258 | * @throws \InvalidArgumentException |
||
259 | */ |
||
260 | View Code Duplication | public function decrement($step = 1) |
|
271 | |||
272 | /** |
||
273 | * @param array|string $data |
||
274 | * @return $this |
||
275 | * @throws \InvalidArgumentException |
||
276 | */ |
||
277 | View Code Duplication | public function append($data) |
|
289 | |||
290 | |||
291 | /** |
||
292 | * @param array|string $data |
||
293 | * @return $this |
||
294 | * @throws \InvalidArgumentException |
||
295 | */ |
||
296 | View Code Duplication | public function prepend($data) |
|
308 | |||
309 | /** |
||
310 | * @param $tagName |
||
311 | * @return $this |
||
312 | * @throws \InvalidArgumentException |
||
313 | */ |
||
314 | public function addTag($tagName) |
||
324 | |||
325 | /** |
||
326 | * @param array $tagNames |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function addTags(array $tagNames) |
||
337 | |||
338 | /** |
||
339 | * @param array $tags |
||
340 | * @return $this |
||
341 | * @throws \InvalidArgumentException |
||
342 | */ |
||
343 | public function setTags(array $tags) |
||
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | public function getTags() |
||
363 | |||
364 | /** |
||
365 | * @return string |
||
366 | */ |
||
367 | public function getTagsAsString($separator = ', ') |
||
371 | |||
372 | /** |
||
373 | * @param $tagName |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function removeTag($tagName) |
||
385 | |||
386 | /** |
||
387 | * @param array $tagNames |
||
388 | * @return $this |
||
389 | */ |
||
390 | public function removeTags(array $tagNames) |
||
398 | |||
399 | /** |
||
400 | * @return array |
||
401 | */ |
||
402 | public function getRemovedTags() |
||
406 | |||
407 | /** |
||
408 | * Return the data as a well-formatted string. |
||
409 | * Any scalar value will be casted to an array |
||
410 | * @param int $option json_encode() options |
||
411 | * @param int $depth json_encode() depth |
||
412 | * @return string |
||
413 | */ |
||
414 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
426 | |||
427 | /** |
||
428 | * Implements \JsonSerializable interface |
||
429 | * @return mixed |
||
430 | */ |
||
431 | public function jsonSerialize() |
||
435 | |||
436 | /** |
||
437 | * Prevent recursions for Debug (php 5.6+) |
||
438 | * @return array |
||
439 | */ |
||
440 | final public function __debugInfo() |
||
447 | } |
||
448 |
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.