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 | implements \Aimeos\Admin\JQAdm\Iface |
||
|
|
|||
| 22 | { |
||
| 23 | private $view; |
||
| 24 | private $context; |
||
| 25 | private $subclients; |
||
| 26 | private $templatePaths; |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Initializes the class instance. |
||
| 31 | * |
||
| 32 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 33 | * @param array $templatePaths Associative list of the file system paths to the core or the extensions as key |
||
| 34 | * and a list of relative paths inside the core or the extension as values |
||
| 35 | */ |
||
| 36 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths ) |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Catches unknown methods |
||
| 45 | * |
||
| 46 | * @param string $name Name of the method |
||
| 47 | * @param array $param List of method parameter |
||
| 48 | * @return boolean False for every call |
||
| 49 | */ |
||
| 50 | public function __call( $name, array $param ) |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns the view object that will generate the admin output. |
||
| 58 | * |
||
| 59 | * @return \Aimeos\MW\View\Iface $view The view object which generates the admin output |
||
| 60 | */ |
||
| 61 | public function getView() |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Sets the view object that will generate the admin output. |
||
| 73 | * |
||
| 74 | * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output |
||
| 75 | * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls |
||
| 76 | */ |
||
| 77 | public function setView( \Aimeos\MW\View\Iface $view ) |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Deletes a resource |
||
| 86 | * |
||
| 87 | * @return string|null admin output to display or null for redirecting to the list |
||
| 88 | */ |
||
| 89 | public function delete() |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns a list of resource according to the conditions |
||
| 96 | * |
||
| 97 | * @return string admin output to display |
||
| 98 | */ |
||
| 99 | public function search() |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Adds the decorators to the client object |
||
| 106 | * |
||
| 107 | * @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
||
| 108 | * @param array $templatePaths List of file system paths where the templates are stored |
||
| 109 | * @param array $decorators List of decorator name that should be wrapped around the client |
||
| 110 | * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\" |
||
| 111 | * @return \Aimeos\Admin\JQAdm\Iface Admin object |
||
| 112 | */ |
||
| 113 | protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $templatePaths, |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Adds the decorators to the client object |
||
| 145 | * |
||
| 146 | * @param \Aimeos\Admin\JQAdm\Iface $client Admin object |
||
| 147 | * @param array $templatePaths List of file system paths where the templates are stored |
||
| 148 | * @param string $path Admin string in lower case, e.g. "catalog/detail/basic" |
||
| 149 | * @return \Aimeos\Admin\JQAdm\Iface Admin object |
||
| 150 | */ |
||
| 151 | protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $templatePaths, $path ) |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the sub-client given by its name. |
||
| 187 | * |
||
| 188 | * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
||
| 189 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
| 190 | * @return \Aimeos\Admin\JQAdm\Iface Sub-part object |
||
| 191 | */ |
||
| 192 | protected function createSubClient( $path, $name ) |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the known client parameters and their values |
||
| 228 | * |
||
| 229 | * @param array $names List of parameter names |
||
| 230 | * @return array Associative list of parameters names as key and their values |
||
| 231 | */ |
||
| 232 | protected function getClientParams( $names = array( 'resource', 'site', 'lang', 'fields', 'filter', 'page', 'sort' ) ) |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the context object. |
||
| 249 | * |
||
| 250 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 251 | */ |
||
| 252 | protected function getContext() |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the list of sub-client names configured for the client. |
||
| 260 | * |
||
| 261 | * @return array List of admin client names |
||
| 262 | */ |
||
| 263 | abstract protected function getSubClientNames(); |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
||
| 268 | * |
||
| 269 | * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names |
||
| 270 | */ |
||
| 271 | protected function getSubClients() |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the paths where the layout templates can be found |
||
| 288 | * |
||
| 289 | * @return array List of template paths |
||
| 290 | */ |
||
| 291 | protected function getTemplatePaths() |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Initializes the criteria object based on the given parameter |
||
| 299 | * |
||
| 300 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 301 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 302 | * @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
||
| 303 | */ |
||
| 304 | protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Initializes the criteria object with conditions based on the given parameter |
||
| 316 | * |
||
| 317 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 318 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 319 | */ |
||
| 320 | private function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * Initializes the criteria object with the slice based on the given parameter. |
||
| 344 | * |
||
| 345 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 346 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 347 | */ |
||
| 348 | private function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Initializes the criteria object with sortations based on the given parameter |
||
| 359 | * |
||
| 360 | * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
||
| 361 | * @param array $params List of criteria data with condition, sorting and paging |
||
| 362 | */ |
||
| 363 | private function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
||
| 382 | } |
||
| 383 |