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 AbstractSource 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 AbstractSource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractSource implements |
||
| 36 | SourceInterface, |
||
| 37 | ConfigurableInterface, |
||
| 38 | LoggerAwareInterface |
||
| 39 | { |
||
| 40 | use ConfigurableTrait; |
||
| 41 | use LoggerAwareTrait; |
||
| 42 | use FilterAwareTrait; |
||
| 43 | use ModelAwareTrait; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array $properties |
||
| 47 | */ |
||
| 48 | private $properties = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Array of `Order` object |
||
| 52 | * @var array $orders |
||
| 53 | */ |
||
| 54 | protected $orders = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The `Pagination` object |
||
| 58 | * @var Pagination|null $pagination |
||
| 59 | */ |
||
| 60 | protected $pagination = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param array|\ArrayAccess $dependencies The class dependencies. |
||
| 64 | * @return void |
||
|
|
|||
| 65 | */ |
||
| 66 | public function __construct($dependencies) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Reset everything but the model. |
||
| 73 | * |
||
| 74 | * @return AbstractSource Chainable |
||
| 75 | */ |
||
| 76 | public function reset() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initialize the source's properties with an array of data. |
||
| 87 | * |
||
| 88 | * @param array $data The source data. |
||
| 89 | * @return AbstractSource Chainable |
||
| 90 | */ |
||
| 91 | public function setData(array $data) |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Set the properties of the source to fetch. |
||
| 108 | * |
||
| 109 | * This method accepts an array of property identifiers (property ident, as string) |
||
| 110 | * that will, if supported, be fetched from the source. |
||
| 111 | * |
||
| 112 | * If no properties are set, it is assumed that all the Model's properties are to be fetched. |
||
| 113 | * |
||
| 114 | * @param array $properties The properties. |
||
| 115 | * @return ColelectionLoader Chainable |
||
| 116 | */ |
||
| 117 | public function setProperties(array $properties) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function properties() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $property Property ident. |
||
| 136 | * @throws InvalidArgumentException If property is not a string or empty. |
||
| 137 | * @return CollectionLoader Chainable |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function addProperty($property) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @return FilterInterface |
||
| 157 | */ |
||
| 158 | protected function createFilter() |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @return FilterGroupInterface |
||
| 167 | */ |
||
| 168 | protected function createFilterGroup() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param array $orders The orders to set. |
||
| 176 | * @return CollectionLoader Chainable |
||
| 177 | */ |
||
| 178 | public function setOrders(array $orders) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function orders() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string|array|Order $param The order property, or an Order object / array. |
||
| 197 | * @param string $mode Optional. |
||
| 198 | * @param array $orderOptions Optional. |
||
| 199 | * @throws InvalidArgumentException If the param argument is invalid. |
||
| 200 | * @return CollectionLoader Chainable |
||
| 201 | */ |
||
| 202 | public function addOrder($param, $mode = 'asc', array $orderOptions = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return OrderInterface |
||
| 241 | */ |
||
| 242 | protected function createOrder() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param mixed $param The pagination object or array. |
||
| 250 | * @throws InvalidArgumentException If the argument is not an object or array. |
||
| 251 | * @return CollectionLoader Chainable |
||
| 252 | */ |
||
| 253 | public function setPagination($param) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the pagination object. |
||
| 271 | * |
||
| 272 | * If the pagination wasn't set previously, a new (default / blank) Pagination object will be created. |
||
| 273 | * (Always return a `PaginationInterface` object) |
||
| 274 | * |
||
| 275 | * @return Pagination |
||
| 276 | */ |
||
| 277 | public function pagination() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return PaginationInterface |
||
| 287 | */ |
||
| 288 | protected function createPagination() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param integer $page The page number. |
||
| 296 | * @throws InvalidArgumentException If the page argument is not numeric. |
||
| 297 | * @return CollectionLoader Chainable |
||
| 298 | */ |
||
| 299 | public function setPage($page) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return integer |
||
| 312 | */ |
||
| 313 | public function page() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param integer $num The number of items to retrieve per page. |
||
| 320 | * @throws InvalidArgumentException If the num per page argument is not numeric. |
||
| 321 | * @return CollectionLoader Chainable |
||
| 322 | */ |
||
| 323 | public function setNumPerPage($num) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return integer |
||
| 336 | */ |
||
| 337 | public function numPerPage() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * ConfigurableTrait > createConfig() |
||
| 344 | * |
||
| 345 | * @param array $data Optional. |
||
| 346 | * @return SourceConfig |
||
| 347 | */ |
||
| 348 | public function createConfig(array $data = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param mixed $ident The ID of the item to load. |
||
| 359 | * @param StorableInterface $item Optional item to load into. |
||
| 360 | * @return StorableInterface |
||
| 361 | */ |
||
| 362 | abstract public function loadItem($ident, StorableInterface $item = null); |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param StorableInterface|null $item The model to load items from. |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | abstract public function loadItems(StorableInterface $item = null); |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Save an item (create a new row) in storage. |
||
| 372 | * |
||
| 373 | * @param StorableInterface $item The object to save. |
||
| 374 | * @return mixed The created item ID, or false in case of an error. |
||
| 375 | */ |
||
| 376 | abstract public function saveItem(StorableInterface $item); |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Update an item in storage. |
||
| 380 | * |
||
| 381 | * @param StorableInterface $item The object to update. |
||
| 382 | * @param array $properties The list of properties to update, if not all. |
||
| 383 | * @return boolean Success / Failure |
||
| 384 | */ |
||
| 385 | abstract public function updateItem(StorableInterface $item, array $properties = null); |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Delete an item from storage |
||
| 389 | * |
||
| 390 | * @param StorableInterface $item Optional item to delete. If none, the current model object will be used.. |
||
| 391 | * @return boolean Success / Failure |
||
| 392 | */ |
||
| 393 | abstract public function deleteItem(StorableInterface $item = null); |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Allow an object to define how the key getter are called. |
||
| 397 | * |
||
| 398 | * @param string $key The key to get the getter from. |
||
| 399 | * @param string $case Optional. The type of case to return. camel, pascal or snake. |
||
| 400 | * @return string The getter method name, for a given key. |
||
| 401 | */ |
||
| 402 | View Code Duplication | protected function getter($key, $case = 'camel') |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Allow an object to define how the key setter are called. |
||
| 417 | * |
||
| 418 | * @param string $key The key to get the setter from. |
||
| 419 | * @param string $case Optional. The type of case to return. camel, pascal or snake. |
||
| 420 | * @return string The setter method name, for a given key. |
||
| 421 | */ |
||
| 422 | View Code Duplication | protected function setter($key, $case = 'camel') |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Transform a snake_case string to camelCase. |
||
| 437 | * |
||
| 438 | * @param string $str The snake_case string to camelize. |
||
| 439 | * @return string The camelCase string. |
||
| 440 | */ |
||
| 441 | private function camelize($str) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Transform a snake_case string to PamelCase. |
||
| 448 | * |
||
| 449 | * @param string $str The snake_case string to pascalize. |
||
| 450 | * @return string The PamelCase string. |
||
| 451 | */ |
||
| 452 | private function pascalize($str) |
||
| 456 | } |
||
| 457 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.