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() |
||
| 79 | { |
||
| 80 | return $this->data; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param mixed $value |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function set($value) |
||
| 88 | { |
||
| 89 | /** |
||
| 90 | * The user set a value, |
||
| 91 | * therefore there is no need to |
||
| 92 | * fetch from source anymore |
||
| 93 | */ |
||
| 94 | $this->fetched = true; |
||
| 95 | $this->data = $value; |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return bool |
||
| 102 | * @throws \InvalidArgumentException |
||
| 103 | */ |
||
| 104 | public function isHit() |
||
| 105 | { |
||
| 106 | return $this->isHit; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param bool $isHit |
||
| 111 | * @return $this |
||
| 112 | * @throws \InvalidArgumentException |
||
| 113 | */ |
||
| 114 | public function setHit($isHit) |
||
| 115 | { |
||
| 116 | if (is_bool($isHit)) { |
||
| 117 | $this->isHit = $isHit; |
||
| 118 | |||
| 119 | return $this; |
||
| 120 | } else { |
||
| 121 | throw new \InvalidArgumentException('$isHit must be a boolean'); |
||
| 122 | } |
||
| 123 | } |
||
| 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 | /** |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | public function getUncommittedData() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return \DateTimeInterface |
||
| 204 | */ |
||
| 205 | public function getExpirationDate() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function getTtl() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function isExpired() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param int $step |
||
| 233 | * @return $this |
||
| 234 | * @throws \InvalidArgumentException |
||
| 235 | */ |
||
| 236 | View Code Duplication | public function increment($step = 1) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param int $step |
||
| 250 | * @return $this |
||
| 251 | * @throws \InvalidArgumentException |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function decrement($step = 1) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param array|string $data |
||
| 267 | * @return $this |
||
| 268 | * @throws \InvalidArgumentException |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function append($data) |
|
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array|string $data |
||
| 286 | * @return $this |
||
| 287 | * @throws \InvalidArgumentException |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function prepend($data) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param $tagName |
||
| 304 | * @return $this |
||
| 305 | * @throws \InvalidArgumentException |
||
| 306 | */ |
||
| 307 | public function addTag($tagName) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param array $tagNames |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function addTags(array $tagNames) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $tags |
||
| 333 | * @return $this |
||
| 334 | * @throws \InvalidArgumentException |
||
| 335 | */ |
||
| 336 | public function setTags(array $tags) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getTags() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getTagsAsString($separator = ', ') |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param $tagName |
||
| 367 | * @return $this |
||
| 368 | */ |
||
| 369 | public function removeTag($tagName) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param array $tagNames |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | public function removeTags(array $tagNames) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function getRemovedTags() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Return the data as a well-formatted string. |
||
| 402 | * Any scalar value will be casted to an array |
||
| 403 | * @param int $option json_encode() options |
||
| 404 | * @param int $depth json_encode() depth |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @throws \RuntimeException |
||
| 422 | */ |
||
| 423 | /* final public function __sleep() |
||
| 424 | { |
||
| 425 | $info = get_object_vars($this); |
||
| 426 | $info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
||
| 427 | |||
| 428 | return (array) $info; |
||
| 429 | }*/ |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Prevent recursions for Debug (php 5.6+) |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | final public function __debugInfo() |
||
| 442 | } |
||
| 443 |
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.