Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Base |
||
| 21 | { |
||
| 22 | private $context; |
||
| 23 | private $aimeos; |
||
| 24 | private $view; |
||
| 25 | private $path; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Initializes the client |
||
| 30 | * |
||
| 31 | * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
||
| 32 | * @param string $path Name of the client separated by slashes, e.g "product/property" |
||
| 33 | */ |
||
| 34 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, $path ) |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Catch unknown methods |
||
| 43 | * |
||
| 44 | * @param string $name Name of the method |
||
| 45 | * @param array $param List of method parameter |
||
| 46 | * @throws \Aimeos\Admin\JsonAdm\Exception If method call failed |
||
| 47 | */ |
||
| 48 | public function __call( $name, array $param ) |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the Aimeos bootstrap object |
||
| 56 | * |
||
| 57 | * @return \Aimeos\Bootstrap The Aimeos bootstrap object |
||
| 58 | */ |
||
| 59 | public function getAimeos() |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the Aimeos bootstrap object |
||
| 71 | * |
||
| 72 | * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object |
||
| 73 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
| 74 | */ |
||
| 75 | public function setAimeos( \Aimeos\Bootstrap $aimeos ) |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the view object that will generate the admin output. |
||
| 84 | * |
||
| 85 | * @return \Aimeos\MW\View\Iface The view object which generates the admin output |
||
| 86 | */ |
||
| 87 | public function getView() |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Sets the view object that will generate the admin output. |
||
| 99 | * |
||
| 100 | * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
||
| 101 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
| 102 | */ |
||
| 103 | public function setView( \Aimeos\MW\View\Iface $view ) |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the items with parent/child relationships |
||
| 112 | * |
||
| 113 | * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 114 | * @param array $include List of resource types that should be fetched |
||
| 115 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 116 | */ |
||
| 117 | protected function getChildItems( array $items, array $include ) |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the context item object |
||
| 125 | * |
||
| 126 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 127 | */ |
||
| 128 | protected function getContext() |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the list of domains that are available as resources |
||
| 136 | * |
||
| 137 | * @param \Aimeos\MW\View\Iface $view View object with "resource" parameter |
||
| 138 | * @return array List of domain names |
||
| 139 | */ |
||
| 140 | protected function getDomains( \Aimeos\MW\View\Iface $view ) |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the IDs sent in the request body |
||
| 174 | * |
||
| 175 | * @param \stdClass $request Decoded request body |
||
| 176 | * @return array List of item IDs |
||
| 177 | */ |
||
| 178 | protected function getIds( $request ) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the list items for association relationships |
||
| 198 | * |
||
| 199 | * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 200 | * @param array $include List of resource types that should be fetched |
||
| 201 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 202 | */ |
||
| 203 | protected function getListItems( array $items, array $include ) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the path to the client |
||
| 211 | * |
||
| 212 | * @return string Client path, e.g. "product/property" |
||
| 213 | */ |
||
| 214 | protected function getPath() |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns the items associated via a lists table |
||
| 222 | * |
||
| 223 | * @param array $listItems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 224 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 225 | */ |
||
| 226 | protected function getRefItems( array $listItems ) |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns the list of allowed resources |
||
| 251 | * |
||
| 252 | * @param \Aimeos\MW\View\Iface $view View object with "access" helper |
||
| 253 | * @param array List of all available resources |
||
| 254 | * @return array List of allowed resources |
||
| 255 | */ |
||
| 256 | protected function getAllowedResources( \Aimeos\MW\View\Iface $view, array $resources ) |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns the list of additional resources |
||
| 274 | * |
||
| 275 | * @param \Aimeos\MW\View\Iface $view View object with "resource" parameter |
||
| 276 | * @return array List of domain names |
||
| 277 | */ |
||
| 278 | protected function getResources( \Aimeos\MW\View\Iface $view ) |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Initializes the criteria object based on the given parameter |
||
| 314 | * |
||
| 315 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 316 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 317 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 318 | */ |
||
| 319 | protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Initializes the criteria object with conditions based on the given parameter |
||
| 331 | * |
||
| 332 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 333 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 334 | */ |
||
| 335 | protected function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 347 | |||
| 348 | |||
| 349 | /** |
||
| 350 | * Initializes the criteria object with the slice based on the given parameter. |
||
| 351 | * |
||
| 352 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 353 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 354 | */ |
||
| 355 | protected function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Initializes the criteria object with sortations based on the given parameter |
||
| 366 | * |
||
| 367 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 368 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 369 | */ |
||
| 370 | protected function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Creates of updates several items at once |
||
| 393 | * |
||
| 394 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 395 | * @param \stdClass $request Object with request body data |
||
| 396 | * @return array List of items |
||
| 397 | */ |
||
| 398 | protected function saveData( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $request ) |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * Saves and returns the new or updated item |
||
| 415 | * |
||
| 416 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 417 | * @param \stdClass $entry Object including "id" and "attributes" elements |
||
| 418 | * @return \Aimeos\MShop\Common\Item\Iface New or updated item |
||
| 419 | */ |
||
| 420 | protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Saves the item references associated via the list |
||
| 444 | * |
||
| 445 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 446 | * @param \Aimeos\MShop\Common\Item\Iface $item Domain item with an unique ID set |
||
| 447 | * @param \stdClass $relationships Object including the <domain>/data/attributes structure |
||
| 448 | */ |
||
| 449 | protected function saveRelationships( \Aimeos\MShop\Common\Manager\Iface $manager, |
||
| 479 | } |
||
| 480 |