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 | class Base |
||
| 21 | { |
||
| 22 | private $view; |
||
| 23 | private $context; |
||
| 24 | private $templatePaths; |
||
| 25 | private $path; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Initializes the client |
||
| 30 | * |
||
| 31 | * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
||
| 32 | * @param \Aimeos\MW\View\Iface $view View object |
||
| 33 | * @param array $templatePaths List of file system paths where the templates are stored |
||
| 34 | * @param string $path Name of the client separated by slashes, e.g "product/property" |
||
| 35 | */ |
||
| 36 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path ) |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Catch unknown methods |
||
| 47 | * |
||
| 48 | * @param string $name Name of the method |
||
| 49 | * @param array $param List of method parameter |
||
| 50 | * @throws \Aimeos\Admin\JsonAdm\Exception If method call failed |
||
| 51 | */ |
||
| 52 | public function __call( $name, array $param ) |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns the items with parent/child relationships |
||
| 60 | * |
||
| 61 | * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 62 | * @param array $include List of resource types that should be fetched |
||
| 63 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 64 | */ |
||
| 65 | protected function getChildItems( array $items, array $include ) |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns the context item object |
||
| 73 | * |
||
| 74 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 75 | */ |
||
| 76 | protected function getContext() |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the list of domains that are available as resources |
||
| 84 | * |
||
| 85 | * @param \Aimeos\MW\View\Iface $view View object with "resource" parameter |
||
| 86 | * @return array List of domain names |
||
| 87 | */ |
||
| 88 | protected function getDomains( \Aimeos\MW\View\Iface $view ) |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the IDs sent in the request body |
||
| 121 | * |
||
| 122 | * @param \stdClass $request Decoded request body |
||
| 123 | * @return array List of item IDs |
||
| 124 | */ |
||
| 125 | protected function getIds( $request ) |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the list items for association relationships |
||
| 145 | * |
||
| 146 | * @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 147 | * @param array $include List of resource types that should be fetched |
||
| 148 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 149 | */ |
||
| 150 | protected function getListItems( array $items, array $include ) |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the path to the client |
||
| 158 | * |
||
| 159 | * @return string Client path, e.g. "product/property" |
||
| 160 | */ |
||
| 161 | protected function getPath() |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the items associated via a lists table |
||
| 169 | * |
||
| 170 | * @param array $listItems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
||
| 171 | * @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 172 | */ |
||
| 173 | protected function getRefItems( array $listItems ) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns the paths to the template files |
||
| 198 | * |
||
| 199 | * @return array List of file system paths |
||
| 200 | */ |
||
| 201 | protected function getTemplatePaths() |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the view object |
||
| 209 | * |
||
| 210 | * @return \Aimeos\MW\View\Iface View object |
||
| 211 | */ |
||
| 212 | protected function getView() |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Initializes the criteria object based on the given parameter |
||
| 220 | * |
||
| 221 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 222 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 223 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 224 | */ |
||
| 225 | protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Initializes the criteria object with conditions based on the given parameter |
||
| 237 | * |
||
| 238 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 239 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 240 | */ |
||
| 241 | protected function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Initializes the criteria object with the slice based on the given parameter. |
||
| 257 | * |
||
| 258 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 259 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 260 | */ |
||
| 261 | protected function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Initializes the criteria object with sortations based on the given parameter |
||
| 272 | * |
||
| 273 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 274 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 275 | */ |
||
| 276 | protected function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Creates of updates several items at once |
||
| 299 | * |
||
| 300 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 301 | * @param \stdClass $request Object with request body data |
||
| 302 | * @return array List of items |
||
| 303 | */ |
||
| 304 | protected function saveData( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $request ) |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Saves and returns the new or updated item |
||
| 321 | * |
||
| 322 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 323 | * @param \stdClass $entry Object including "id" and "attributes" elements |
||
| 324 | * @return \Aimeos\MShop\Common\Item\Iface New or updated item |
||
| 325 | */ |
||
| 326 | protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Saves the item references associated via the list |
||
| 347 | * |
||
| 348 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
||
| 349 | * @param \Aimeos\MShop\Common\Item\Iface $item Domain item with an unique ID set |
||
| 350 | * @param \stdClass $relationships Object including the <domain>/data/attributes structure |
||
| 351 | */ |
||
| 352 | protected function saveRelationships( \Aimeos\MShop\Common\Manager\Iface $manager, |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Adds the data from the given object to the item |
||
| 382 | * |
||
| 383 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object |
||
| 384 | * @param \Aimeos\MShop\Common\Item\Iface $item Item object to add the data to |
||
| 385 | * @param \stdClass $data Object with "attributes" property |
||
| 386 | * @param string $domain Domain of the type item |
||
| 387 | * @return \Aimeos\MShop\Common\Item\Iface Item including the data |
||
| 388 | */ |
||
| 389 | protected function addItemData(\Aimeos\MShop\Common\Manager\Iface $manager, |
||
| 408 | } |
||
| 409 |