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 |
||
| 16 | trait ManagesItemsTrait |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The items stored in the manager |
||
| 20 | * @var array $items Items governed by manager |
||
| 21 | */ |
||
| 22 | protected $_items = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Name of the property to hold the data items. Internal use only |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $nameOfItemsRepository = '_items'; |
||
| 29 | |||
| 30 | /** @var array Array of protected aliases */ |
||
| 31 | protected $protectedItems = []; |
||
| 32 | |||
| 33 | /* The user may also set $dataItemsName */ |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Initializes a new manager instance. |
||
| 37 | * |
||
| 38 | * This is useful for implementations that have their own __construct method |
||
| 39 | * This is an alias for reset() |
||
| 40 | * |
||
| 41 | * @param array $items |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function initManager($items = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Hydrate with external data, optionally append |
||
| 67 | * |
||
| 68 | * @param $data array The data to be hydrated into the manager |
||
| 69 | * @param bool $append When true, data will be appended to the current set |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function hydrate(array $data, $append = false) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Adds a single item. |
||
| 85 | * |
||
| 86 | * Allow for dot notation (one.two.three) and item nesting. |
||
| 87 | * |
||
| 88 | * @param string $alias Key to be stored |
||
| 89 | * @param mixed $item Value to be stored |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function add($alias, $item = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Updates an item |
||
| 133 | * |
||
| 134 | * @param string $alias |
||
| 135 | * @param null $item |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function set($alias, $item = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Push a value or values onto the end of an array inside manager |
||
| 146 | * @param string $alias The level of nested data |
||
| 147 | * @param mixed $value The first value to append |
||
| 148 | * @param null|mixed $other Optional other values to apend |
||
| 149 | * @return int Number of items pushed |
||
| 150 | * @throws ItemNotFoundException If pushing to unset array |
||
| 151 | */ |
||
| 152 | public function push($alias, $value, $other = null) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get a single item |
||
| 177 | * |
||
| 178 | * @param string $alias |
||
| 179 | * @param string $fallback Defaults to '_michaels_no_fallback' so null can be a fallback |
||
| 180 | * @throws \Michaels\Manager\Exceptions\ItemNotFoundException If item not found |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function get($alias, $fallback = '_michaels_no_fallback') |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Return an item if it exist |
||
| 201 | * @param $alias |
||
| 202 | * @return NoItemFoundMessage |
||
| 203 | */ |
||
| 204 | public function getIfExists($alias) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Return an item if it exist |
||
| 220 | * Alias of getIfExists() |
||
| 221 | * |
||
| 222 | * @param $alias |
||
| 223 | * @return NoItemFoundMessage |
||
| 224 | */ |
||
| 225 | public function getIfHas($alias) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return all items as array |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getAll() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return all items as array |
||
| 243 | * Alias of getAll() |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function all() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Confirm or deny that an item exists |
||
| 253 | * |
||
| 254 | * @param $alias |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function exists($alias) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Confirm or deny that an item exists |
||
| 278 | * Alias of exists() |
||
| 279 | * |
||
| 280 | * @param $alias |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function has($alias) |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Confirm that manager has no items |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | public function isEmpty() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Deletes an item |
||
| 301 | * |
||
| 302 | * @param $alias |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function remove($alias) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Clear the manager |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function clear() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Reset the manager with an array of items |
||
| 335 | * Alias of initManager() |
||
| 336 | * |
||
| 337 | * @param array $items |
||
| 338 | * @return mixed |
||
| 339 | */ |
||
| 340 | public function reset($items) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Guard an alias from being modified |
||
| 347 | * @param $item |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function protect($item) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Merge a set of defaults with the current items |
||
| 358 | * @param array $defaults |
||
| 359 | * @return $this |
||
| 360 | */ |
||
| 361 | public function loadDefaults(array $defaults) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the name of the property that holds data items |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function getItemsName() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sets the name of the property that holds data items |
||
| 378 | * @param $nameOfItemsRepository |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function setItemsName($nameOfItemsRepository) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the collection of items as JSON. |
||
| 389 | * |
||
| 390 | * @param int $options |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function toJson($options = 0) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * When manager instance is used as a string, return json of items |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function __toString() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Prepare the returned value |
||
| 409 | * @param $value |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | protected function prepareReturnedValue($value) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Recursively merge defaults array and items array |
||
| 425 | * @param array $defaults |
||
| 426 | * @param string $level |
||
| 427 | */ |
||
| 428 | protected function mergeDefaults(array $defaults, $level = '') |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Cycle through the nests to see if an item is protected |
||
| 448 | * @param $item |
||
| 449 | */ |
||
| 450 | protected function checkIfProtected($item) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Throws an exception if item is protected |
||
| 468 | * @param $item |
||
| 469 | */ |
||
| 470 | protected function performProtectedCheck($item) |
||
| 476 | } |
||
| 477 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: