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 string |
||
| 38 | */ |
||
| 39 | public function getEncodedKey() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | public function getUncommittedData() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return \DateTimeInterface |
||
| 54 | */ |
||
| 55 | public function getExpirationDate() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Alias of expireAt() with forced $expiration param |
||
| 62 | * |
||
| 63 | * @param \DateTimeInterface $expiration |
||
| 64 | * The point in time after which the item MUST be considered expired. |
||
| 65 | * If null is passed explicitly, a default value MAY be used. If none is set, |
||
| 66 | * the value should be stored permanently or for as long as the |
||
| 67 | * implementation allows. |
||
| 68 | * |
||
| 69 | * @return static |
||
| 70 | * The called object. |
||
| 71 | */ |
||
| 72 | public function setExpirationDate(\DateTimeInterface $expiration) |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * @return \DateTimeInterface |
||
| 80 | * @throws \LogicException |
||
| 81 | */ |
||
| 82 | public function getCreationDate() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param \DateTimeInterface $date |
||
| 93 | * @return $this |
||
| 94 | * @throws \LogicException |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function setCreationDate(\DateTimeInterface $date) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @return \DateTimeInterface |
||
| 108 | * @throws \LogicException |
||
| 109 | */ |
||
| 110 | public function getModificationDate() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param \DateTimeInterface $date |
||
| 121 | * @return $this |
||
| 122 | * @throws \LogicException |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function setModificationDate(\DateTimeInterface $date) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | public function getTtl() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isExpired() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param int $step |
||
| 157 | * @return $this |
||
| 158 | * @throws \InvalidArgumentException |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function increment($step = 1) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $step |
||
| 174 | * @return $this |
||
| 175 | * @throws \InvalidArgumentException |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function decrement($step = 1) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param array|string $data |
||
| 191 | * @return $this |
||
| 192 | * @throws \InvalidArgumentException |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function append($data) |
|
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param array|string $data |
||
| 210 | * @return $this |
||
| 211 | * @throws \InvalidArgumentException |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function prepend($data) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param $tagName |
||
| 228 | * @return $this |
||
| 229 | * @throws \InvalidArgumentException |
||
| 230 | */ |
||
| 231 | public function addTag($tagName) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param array $tagNames |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function addTags(array $tagNames) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $tags |
||
| 257 | * @return $this |
||
| 258 | * @throws \InvalidArgumentException |
||
| 259 | */ |
||
| 260 | public function setTags(array $tags) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getTags() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getTagsAsString($separator = ', ') |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $tagName |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function removeTag($tagName) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param array $tagNames |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function removeTags(array $tagNames) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function getRemovedTags() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Return the data as a well-formatted string. |
||
| 326 | * Any scalar value will be casted to an array |
||
| 327 | * @param int $option json_encode() options |
||
| 328 | * @param int $depth json_encode() depth |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Implements \JsonSerializable interface |
||
| 346 | * @return mixed |
||
| 347 | */ |
||
| 348 | public function jsonSerialize() |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Set the EventManager instance |
||
| 356 | * |
||
| 357 | * @param EventManager $em |
||
| 358 | */ |
||
| 359 | public function setEventManager(EventManager $em) |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Prevent recursions for Debug (php 5.6+) |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | final public function __debugInfo() |
||
| 376 | } |
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: