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 ManagesItemsTrait 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 ManagesItemsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 18 | trait ManagesItemsTrait | ||
| 19 | { | ||
| 20 | /** | ||
| 21 | * The items stored in the manager | ||
| 22 | * @var array $items Items governed by manager | ||
| 23 | */ | ||
| 24 | protected $_items; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Name of the property to hold the data items. Internal use only | ||
| 28 | * @var string | ||
| 29 | */ | ||
| 30 | protected $nameOfItemsRepository = '_items'; | ||
| 31 | |||
| 32 | /** @var array Array of protected aliases */ | ||
| 33 | protected $protectedItems = []; | ||
| 34 | |||
| 35 | /* The user may also set $dataItemsName */ | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Initializes a new manager instance. | ||
| 39 | * | ||
| 40 | * This is useful for implementations that have their own __construct method | ||
| 41 | * This is an alias for reset() | ||
| 42 | * | ||
| 43 | * @param array $items | ||
| 44 | * @return $this | ||
| 45 | */ | ||
| 46 | public function initManager($items = null) | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Hydrate with external data, optionally append | ||
| 69 | * | ||
| 70 | * @param $data string The data to be hydrated into the manager | ||
| 71 | * @param bool $append When true, data will be appended to the current set | ||
| 72 | * @return $this | ||
| 73 | */ | ||
| 74 | public function hydrate($data, $append = false) | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Adds a single item. | ||
| 86 | * | ||
| 87 | * Allow for dot notation (one.two.three) and item nesting. | ||
| 88 | * | ||
| 89 | * @param string $alias Key to be stored | ||
| 90 | * @param mixed $item Value to be stored | ||
| 91 | * @return $this | ||
| 92 | */ | ||
| 93 | public function add($alias, $item = null) | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Updates an item | ||
| 134 | * | ||
| 135 | * @param string $alias | ||
| 136 | * @param null $item | ||
| 137 | * | ||
| 138 | * @return $this | ||
| 139 | */ | ||
| 140 | public function set($alias, $item = null) | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Push a value or values onto the end of an array inside manager | ||
| 147 | * @param string $alias The level of nested data | ||
| 148 | * @param mixed $value The first value to append | ||
| 149 | * @param null|mixed $_ Optional other values to apend | ||
| 150 | * @return int Number of items pushed | ||
| 151 | * @throws ItemNotFoundException If pushing to unset array | ||
| 152 | */ | ||
| 153 | public function push($alias, $value, $_ = null) | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Get a single item | ||
| 178 | * | ||
| 179 | * @param string $alias | ||
| 180 | * @param string $fallback Defaults to '_michaels_no_fallback' so null can be a fallback | ||
| 181 | * @throws \Michaels\Manager\Exceptions\ItemNotFoundException If item not found | ||
| 182 | * @return mixed | ||
| 183 | */ | ||
| 184 | public function get($alias, $fallback = '_michaels_no_fallback') | ||
| 199 | |||
| 200 | /** | ||
| 201 | * Return an item if it exist | ||
| 202 | * @param $alias | ||
| 203 | * @return NoItemFoundMessage | ||
| 204 | */ | ||
| 205 | public function getIfExists($alias) | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Return an item if it exist | ||
| 221 | * Alias of getIfExists() | ||
| 222 | * | ||
| 223 | * @param $alias | ||
| 224 | * @return NoItemFoundMessage | ||
| 225 | */ | ||
| 226 | public function getIfHas($alias) | ||
| 230 | |||
| 231 | /** | ||
| 232 | * Return all items as array | ||
| 233 | * | ||
| 234 | * @return array | ||
| 235 | */ | ||
| 236 | public function getAll() | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Return all items as array | ||
| 244 | * Alias of getAll() | ||
| 245 | * @return array | ||
| 246 | */ | ||
| 247 | public function all() | ||
| 251 | |||
| 252 | /** | ||
| 253 | * Confirm or deny that an item exists | ||
| 254 | * | ||
| 255 | * @param $alias | ||
| 256 | * @return bool | ||
| 257 | */ | ||
| 258 | public function exists($alias) | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Confirm or deny that an item exists | ||
| 274 | * Alias of exists() | ||
| 275 | * | ||
| 276 | * @param $alias | ||
| 277 | * @return bool | ||
| 278 | */ | ||
| 279 | public function has($alias) | ||
| 283 | |||
| 284 | |||
| 285 | /** | ||
| 286 | * Confirm that manager has no items | ||
| 287 | * @return boolean | ||
| 288 | */ | ||
| 289 | public function isEmpty() | ||
| 294 | |||
| 295 | /** | ||
| 296 | * Deletes an item | ||
| 297 | * | ||
| 298 | * @param $alias | ||
| 299 | * @return $this | ||
| 300 | */ | ||
| 301 | public function remove($alias) | ||
| 317 | |||
| 318 | /** | ||
| 319 | * Clear the manager | ||
| 320 | * @return $this | ||
| 321 | */ | ||
| 322 | public function clear() | ||
| 328 | |||
| 329 | /** | ||
| 330 | * Reset the manager with an array of items | ||
| 331 | * Alias of initManager() | ||
| 332 | * | ||
| 333 | * @param array $items | ||
| 334 | * @return mixed | ||
| 335 | */ | ||
| 336 | public function reset($items) | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Guard an alias from being modified | ||
| 343 | * @param $item | ||
| 344 | * @return $this | ||
| 345 | */ | ||
| 346 | public function protect($item) | ||
| 351 | |||
| 352 | /** | ||
| 353 | * Merge a set of defaults with the current items | ||
| 354 | * @param array $defaults | ||
| 355 | * @return $this | ||
| 356 | */ | ||
| 357 | public function loadDefaults(array $defaults) | ||
| 362 | |||
| 363 | /** | ||
| 364 | * Returns the name of the property that holds data items | ||
| 365 | * @return string | ||
| 366 | */ | ||
| 367 | public function getItemsName() | ||
| 371 | |||
| 372 | /** | ||
| 373 | * Sets the name of the property that holds data items | ||
| 374 | * @param $nameOfItemsRepository | ||
| 375 | * @return $this | ||
| 376 | */ | ||
| 377 | public function setItemsName($nameOfItemsRepository) | ||
| 382 | |||
| 383 | /** | ||
| 384 | * Get the collection of items as JSON. | ||
| 385 | * | ||
| 386 | * @param int $options | ||
| 387 | * @return string | ||
| 388 | */ | ||
| 389 | public function toJson($options = 0) | ||
| 393 | |||
| 394 | /** | ||
| 395 | * When manager instance is used as a string, return json of items | ||
| 396 | * @return string | ||
| 397 | */ | ||
| 398 | public function __toString() | ||
| 402 | |||
| 403 | /** | ||
| 404 | * Prepare the returned value | ||
| 405 | * @param $value | ||
| 406 | * @return mixed | ||
| 407 | */ | ||
| 408 | protected function prepareReturnedValue($value) | ||
| 418 | |||
| 419 | /** | ||
| 420 | * Recursively merge defaults array and items array | ||
| 421 | * @param array $defaults | ||
| 422 | * @param string $level | ||
| 423 | */ | ||
| 424 | protected function mergeDefaults(array $defaults, $level = '') | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Cycle through the nests to see if an item is protected | ||
| 444 | * @param $item | ||
| 445 | */ | ||
| 446 | protected function checkIfProtected($item) | ||
| 461 | |||
| 462 | /** | ||
| 463 | * Throws an exception if item is protected | ||
| 464 | * @param $item | ||
| 465 | */ | ||
| 466 | protected function performProtectedCheck($item) | ||
| 472 | } | ||
| 473 | 
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: