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 |
||
| 21 | trait ItemExtendedTrait |
||
| 22 | { |
||
| 23 | /******************** |
||
| 24 | * |
||
| 25 | * PSR-6 Extended Methods |
||
| 26 | * |
||
| 27 | *******************/ |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function getUncommittedData() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return \DateTimeInterface |
||
| 40 | */ |
||
| 41 | public function getExpirationDate() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Alias of expireAt() with forced $expiration param |
||
| 48 | * |
||
| 49 | * @param \DateTimeInterface $expiration |
||
| 50 | * The point in time after which the item MUST be considered expired. |
||
| 51 | * If null is passed explicitly, a default value MAY be used. If none is set, |
||
| 52 | * the value should be stored permanently or for as long as the |
||
| 53 | * implementation allows. |
||
| 54 | * |
||
| 55 | * @return static |
||
| 56 | * The called object. |
||
| 57 | */ |
||
| 58 | public function setExpirationDate(\DateTimeInterface $expiration) |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @return \DateTimeInterface |
||
| 66 | * @throws \LogicException |
||
| 67 | */ |
||
| 68 | public function getCreationDate() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param \DateTimeInterface $date |
||
| 79 | * @return $this |
||
| 80 | * @throws \LogicException |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function setCreationDate(\DateTimeInterface $date) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return \DateTimeInterface |
||
| 94 | * @throws \LogicException |
||
| 95 | */ |
||
| 96 | public function getModificationDate() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param \DateTimeInterface $date |
||
| 107 | * @return $this |
||
| 108 | * @throws \LogicException |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function setModificationDate(\DateTimeInterface $date) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return int |
||
| 122 | */ |
||
| 123 | public function getTtl() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function isExpired() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param int $step |
||
| 143 | * @return $this |
||
| 144 | * @throws \InvalidArgumentException |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function increment($step = 1) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param int $step |
||
| 160 | * @return $this |
||
| 161 | * @throws \InvalidArgumentException |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function decrement($step = 1) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param array|string $data |
||
| 177 | * @return $this |
||
| 178 | * @throws \InvalidArgumentException |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function append($data) |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @param array|string $data |
||
| 196 | * @return $this |
||
| 197 | * @throws \InvalidArgumentException |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function prepend($data) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param $tagName |
||
| 214 | * @return $this |
||
| 215 | * @throws \InvalidArgumentException |
||
| 216 | */ |
||
| 217 | public function addTag($tagName) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param array $tagNames |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function addTags(array $tagNames) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param array $tags |
||
| 243 | * @return $this |
||
| 244 | * @throws \InvalidArgumentException |
||
| 245 | */ |
||
| 246 | public function setTags(array $tags) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getTags() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getTagsAsString($separator = ', ') |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $tagName |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function removeTag($tagName) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param array $tagNames |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function removeTags(array $tagNames) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function getRemovedTags() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Return the data as a well-formatted string. |
||
| 312 | * Any scalar value will be casted to an array |
||
| 313 | * @param int $option json_encode() options |
||
| 314 | * @param int $depth json_encode() depth |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getDataAsJsonString($option = 0, $depth = 512) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Implements \JsonSerializable interface |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | public function jsonSerialize() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Prevent recursions for Debug (php 5.6+) |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | final public function __debugInfo() |
||
| 350 | } |
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: