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 | * |
||
| 58 | * PSR-6 Methods |
||
| 59 | * |
||
| 60 | *******************/ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getKey() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return mixed |
||
| 72 | * @throws \InvalidArgumentException |
||
| 73 | */ |
||
| 74 | public function get() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param mixed $value |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function set($value) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return bool |
||
| 98 | * @throws \InvalidArgumentException |
||
| 99 | */ |
||
| 100 | public function isHit() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param \DateTimeInterface $expiration |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function expiresAt($expiration) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Sets the expiration time for this cache item. |
||
| 122 | * |
||
| 123 | * @param int|\DateInterval $time |
||
| 124 | * The period of time from the present after which the item MUST be considered |
||
| 125 | * expired. An integer parameter is understood to be the time in seconds until |
||
| 126 | * expiration. If null is passed explicitly, a default value MAY be used. |
||
| 127 | * If none is set, the value should be stored permanently or for as long as the |
||
| 128 | * implementation allows. |
||
| 129 | * |
||
| 130 | * @return static |
||
| 131 | * The called object. |
||
| 132 | * |
||
| 133 | * @deprecated Use CacheItemInterface::expiresAfter() instead |
||
| 134 | */ |
||
| 135 | public function touch($time) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param \DateInterval|int $time |
||
| 144 | * @return $this |
||
| 145 | * @throws \InvalidArgumentException |
||
| 146 | */ |
||
| 147 | public function expiresAfter($time) |
||
| 148 | { |
||
| 149 | if (is_numeric($time)) { |
||
| 150 | if ($time <= 0) { |
||
| 151 | /** |
||
| 152 | * 5 years, however memcached or memory cached will gone when u restart it |
||
| 153 | * just recommended for sqlite. files |
||
| 154 | */ |
||
| 155 | $time = 30 * 24 * 3600 * 5; |
||
| 156 | } |
||
| 157 | $this->expirationDate = $this->expirationDate->add(new \DateInterval(sprintf('PT%dS', $time))); |
||
| 158 | } else if ($time instanceof \DateInterval) { |
||
| 159 | $this->expirationDate = $this->expirationDate->add($time); |
||
| 160 | } else { |
||
| 161 | throw new \InvalidArgumentException('Invalid date format'); |
||
| 162 | } |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /******************** |
||
| 168 | * |
||
| 169 | * PSR-6 Extended Methods |
||
| 170 | * |
||
| 171 | *******************/ |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | public function getUncommittedData() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return \DateTimeInterface |
||
| 184 | */ |
||
| 185 | public function getExpirationDate() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function getTtl() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isExpired() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param int $step |
||
| 213 | * @return $this |
||
| 214 | * @throws \InvalidArgumentException |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function increment($step = 1) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param int $step |
||
| 230 | * @return $this |
||
| 231 | * @throws \InvalidArgumentException |
||
| 232 | */ |
||
| 233 | View Code Duplication | public function decrement($step = 1) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param array|string $data |
||
| 247 | * @return $this |
||
| 248 | * @throws \InvalidArgumentException |
||
| 249 | */ |
||
| 250 | View Code Duplication | public function append($data) |
|
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param array|string $data |
||
| 266 | * @return $this |
||
| 267 | * @throws \InvalidArgumentException |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function prepend($data) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param $tagName |
||
| 284 | * @return $this |
||
| 285 | * @throws \InvalidArgumentException |
||
| 286 | */ |
||
| 287 | public function addTag($tagName) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $tagNames |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function addTags(array $tagNames) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param array $tags |
||
| 313 | * @return $this |
||
| 314 | * @throws \InvalidArgumentException |
||
| 315 | */ |
||
| 316 | public function setTags(array $tags) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function getTags() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getTagsAsString($separator = ', ') |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param $tagName |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function removeTag($tagName) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param array $tagNames |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function removeTags(array $tagNames) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | public function getRemovedTags() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @throws \RuntimeException |
||
| 382 | */ |
||
| 383 | /* final public function __sleep() |
||
| 384 | { |
||
| 385 | $info = get_object_vars($this); |
||
| 386 | $info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
||
| 387 | |||
| 388 | return (array) $info; |
||
| 389 | }*/ |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Prevent recursions for Debug (php 5.6+) |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | final public function __debugInfo() |
||
| 402 | } |
||
| 403 |
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.