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 |
||
| 21 | abstract class Base |
||
| 22 | implements \Aimeos\Client\Html\Iface |
||
| 23 | { |
||
| 24 | private $view; |
||
| 25 | private $cache; |
||
| 26 | private $context; |
||
| 27 | private $subclients; |
||
| 28 | private $templatePaths; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Initializes the class instance. |
||
| 33 | * |
||
| 34 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 35 | * @param array $templatePaths Associative list of the file system paths to the core or the extensions as key |
||
| 36 | * and a list of relative paths inside the core or the extension as values |
||
| 37 | */ |
||
| 38 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths ) |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns the view object that will generate the HTML output. |
||
| 47 | * |
||
| 48 | * @return \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 49 | */ |
||
| 50 | public function getView() |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Modifies the cached body content to replace content based on sessions or cookies. |
||
| 62 | * |
||
| 63 | * @param string $content Cached content |
||
| 64 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 65 | * @return string Modified body content |
||
| 66 | */ |
||
| 67 | public function modifyBody( $content, $uid ) |
||
| 68 | { |
||
| 69 | $view = $this->getView(); |
||
| 70 | |||
| 71 | foreach( $this->getSubClients() as $subclient ) |
||
| 72 | { |
||
| 73 | $subclient->setView( $view ); |
||
| 74 | $content = $subclient->modifyBody( $content, $uid ); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $content; |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Modifies the cached header content to replace content based on sessions or cookies. |
||
| 83 | * |
||
| 84 | * @param string $content Cached content |
||
| 85 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 86 | * @return string Modified header content |
||
| 87 | */ |
||
| 88 | public function modifyHeader( $content, $uid ) |
||
| 89 | { |
||
| 90 | $view = $this->getView(); |
||
| 91 | |||
| 92 | foreach( $this->getSubClients() as $subclient ) |
||
| 93 | { |
||
| 94 | $subclient->setView( $view ); |
||
| 95 | $content = $subclient->modifyHeader( $content, $uid ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $content; |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Processes the input, e.g. store given values. |
||
| 104 | * A view must be available and this method doesn't generate any output |
||
| 105 | * besides setting view variables. |
||
| 106 | * |
||
| 107 | * @return boolean False if processing is stopped, otherwise all processing was completed successfully |
||
| 108 | */ |
||
| 109 | public function process() |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Sets the view object that will generate the HTML output. |
||
| 128 | * |
||
| 129 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 130 | * @return \Aimeos\Client\Html\Iface Reference to this object for fluent calls |
||
| 131 | */ |
||
| 132 | public function setView( \Aimeos\MW\View\Iface $view ) |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds the decorators to the client object |
||
| 141 | * |
||
| 142 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
| 143 | * @param array $templatePaths List of file system paths where the templates are stored |
||
| 144 | * @param array $decorators List of decorator name that should be wrapped around the client |
||
| 145 | * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Client\Html\Catalog\Decorator\" |
||
| 146 | * @return \Aimeos\Client\Html\Iface Client object |
||
| 147 | */ |
||
| 148 | protected function addDecorators( \Aimeos\Client\Html\Iface $client, array $templatePaths, |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Adds the decorators to the client object |
||
| 180 | * |
||
| 181 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
| 182 | * @param array $templatePaths List of file system paths where the templates are stored |
||
| 183 | * @param string $path Client string in lower case, e.g. "catalog/detail/basic" |
||
| 184 | * @return \Aimeos\Client\Html\Iface Client object |
||
| 185 | */ |
||
| 186 | protected function addClientDecorators( \Aimeos\Client\Html\Iface $client, array $templatePaths, $path ) |
||
| 187 | { |
||
| 188 | if( !is_string( $path ) || $path === '' ) { |
||
| 189 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid domain "%1$s"', $path ) ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) ); |
||
| 193 | $config = $this->context->getConfig(); |
||
| 194 | |||
| 195 | $decorators = $config->get( 'client/html/common/decorators/default', array() ); |
||
| 196 | $excludes = $config->get( 'client/html/' . $path . '/decorators/excludes', array() ); |
||
| 197 | |||
| 198 | foreach( $decorators as $key => $name ) |
||
| 199 | { |
||
| 200 | if( in_array( $name, $excludes ) ) { |
||
| 201 | unset( $decorators[$key] ); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | $classprefix = '\\Aimeos\\Client\\Html\\Common\\Decorator\\'; |
||
| 206 | $client = $this->addDecorators( $client, $templatePaths, $decorators, $classprefix ); |
||
| 207 | |||
| 208 | $classprefix = '\\Aimeos\\Client\\Html\\Common\\Decorator\\'; |
||
| 209 | $decorators = $config->get( 'client/html/' . $path . '/decorators/global', array() ); |
||
| 210 | $client = $this->addDecorators( $client, $templatePaths, $decorators, $classprefix ); |
||
| 211 | |||
| 212 | $classprefix = '\\Aimeos\\Client\\Html\\' . $localClass . '\\Decorator\\'; |
||
| 213 | $decorators = $config->get( 'client/html/' . $path . '/decorators/local', array() ); |
||
| 214 | $client = $this->addDecorators( $client, $templatePaths, $decorators, $classprefix ); |
||
| 215 | |||
| 216 | return $client; |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds the cache tags to the given list and sets a new expiration date if necessary based on the given item. |
||
| 222 | * |
||
| 223 | * @param array|\Aimeos\MShop\Common\Item\Iface $items Item or list of items, maybe with associated list items |
||
| 224 | * @param string $domain Name of the domain the item is from |
||
| 225 | * @param string|null &$expire Expiration date that will be overwritten if an earlier date is found |
||
| 226 | * @param array &$tags List of tags the new tags will be added to |
||
| 227 | */ |
||
| 228 | protected function addMetaItem( $items, $domain, &$expire, array &$tags ) |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Adds expire date and tags for a single item. |
||
| 280 | * |
||
| 281 | * @param \Aimeos\MShop\Common\Item\Iface $item Item, maybe with associated list items |
||
| 282 | * @param string $domain Name of the domain the item is from |
||
| 283 | * @param string|null &$expire Expiration date that will be overwritten if an earlier date is found |
||
| 284 | * @param array &$tags List of tags the new tags will be added to |
||
| 285 | * @param boolean $tagAll True of tags for all items should be added, false if only for the main item |
||
| 286 | */ |
||
| 287 | private function addMetaItemSingle( \Aimeos\MShop\Common\Item\Iface $item, $domain, &$expire, array &$tags, $tagAll ) |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Adds expire date and tags for referenced items |
||
| 312 | * |
||
| 313 | * @param \Aimeos\MShop\Common\Item\ListRef\Iface $item Item with associated list items |
||
| 314 | * @param array &$expire Expiration date that will be overwritten if an earlier date is found |
||
| 315 | * @param array &$tags List of tags the new tags will be added to |
||
| 316 | * @param boolean $tagAll True of tags for all items should be added, false if only for the main item |
||
| 317 | */ |
||
| 318 | private function addMetaItemRef( \Aimeos\MShop\Common\Item\ListRef\Iface $item, array &$expires, array &$tags, $tagAll ) |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Adds a new expiration date if a list item is activated in the future. |
||
| 335 | * |
||
| 336 | * @param array|string $ids Item ID or list of item IDs from the given domain |
||
| 337 | * @param string $domain Name of the domain the item IDs are from |
||
| 338 | * @param string|null &$expire Expiration date that will be overwritten if an start date in the future is available |
||
| 339 | */ |
||
| 340 | protected function addMetaList( $ids, $domain, &$expire ) |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the sub-client given by its name. |
||
| 361 | * |
||
| 362 | * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
||
| 363 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
| 364 | * @return \Aimeos\Client\Html\Iface Sub-part object |
||
| 365 | */ |
||
| 366 | protected function createSubClient( $path, $name ) |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the minimal expiration date. |
||
| 399 | * |
||
| 400 | * @param string|null $first First expiration date or null |
||
| 401 | * @param string|null $second Second expiration date or null |
||
| 402 | * @return string|null Expiration date |
||
| 403 | */ |
||
| 404 | protected function expires( $first, $second ) |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns the parameters used by the html client. |
||
| 412 | * |
||
| 413 | * @param array $params Associative list of all parameters |
||
| 414 | * @param array $prefixes List of prefixes the parameters must start with |
||
| 415 | * @return array Associative list of parameters used by the html client |
||
| 416 | */ |
||
| 417 | protected function getClientParams( array $params, array $prefixes = array( 'f', 'l', 'd', 'a' ) ) |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Returns the context object. |
||
| 434 | * |
||
| 435 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 436 | */ |
||
| 437 | protected function getContext() |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Generates an unique hash from based on the input suitable to be used as part of the cache key |
||
| 445 | * |
||
| 446 | * @param array $prefixes List of prefixes the parameters must start with |
||
| 447 | * @param string $key Unique identifier if the content is placed more than once on the same page |
||
| 448 | * @param array $config Multi-dimensional array of configuration options used by the client and sub-clients |
||
| 449 | * @return string Unique hash |
||
| 450 | */ |
||
| 451 | protected function getParamHash( array $prefixes = array( 'f', 'l', 'd' ), $key = '', array $config = array() ) |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns the list of sub-client names configured for the client. |
||
| 467 | * |
||
| 468 | * @return array List of HTML client names |
||
| 469 | */ |
||
| 470 | abstract protected function getSubClientNames(); |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
||
| 475 | * |
||
| 476 | * @return array List of sub-clients implementing \Aimeos\Client\Html\Iface ordered in the same way as the names |
||
| 477 | */ |
||
| 478 | protected function getSubClients() |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns the paths where the layout templates can be found |
||
| 495 | * |
||
| 496 | * @return array List of template paths |
||
| 497 | * @since 2015.09 |
||
| 498 | */ |
||
| 499 | protected function getTemplatePaths() |
||
| 503 | |||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns the attribute type item specified by the code. |
||
| 507 | * |
||
| 508 | * @param string $prefix Domain prefix for the manager, e.g. "media/type" |
||
| 509 | * @param string $domain Domain of the type item |
||
| 510 | * @param string $code Code of the type item |
||
| 511 | * @return \Aimeos\MShop\Common\Item\Type\Iface Type item |
||
| 512 | * @throws \Aimeos\Controller\Jobs\Exception If no item is found |
||
| 513 | */ |
||
| 514 | protected function getTypeItem( $prefix, $domain, $code ) |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns the cache entry for the given unique ID and type. |
||
| 539 | * |
||
| 540 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
| 541 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 542 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
| 543 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
| 544 | * @return string Cached entry or empty string if not available |
||
| 545 | */ |
||
| 546 | protected function getCached( $type, $uid, array $prefixes, $confkey ) |
||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * Returns the cache entry for the given type and unique ID. |
||
| 598 | * |
||
| 599 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
| 600 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 601 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
| 602 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
| 603 | * @param string $value Value string that should be stored for the given key |
||
| 604 | * @param array $tags List of tag strings that should be assoicated to the |
||
| 605 | * given value in the cache |
||
| 606 | * @param string|null $expire Date/time string in "YYYY-MM-DD HH:mm:ss" |
||
| 607 | * format when the cache entry expires |
||
| 608 | */ |
||
| 609 | protected function setCached( $type, $uid, array $prefixes, $confkey, $value, array $tags, $expire ) |
||
| 633 | |||
| 634 | |||
| 635 | /** |
||
| 636 | * Replaces the section in the content that is enclosed by the marker. |
||
| 637 | * |
||
| 638 | * @param string $content Cached content |
||
| 639 | * @param string $section New section content |
||
| 640 | * @param string $marker Name of the section marker without "<!-- " and " -->" parts |
||
| 641 | */ |
||
| 642 | protected function replaceSection( $content, $section, $marker ) |
||
| 659 | |||
| 660 | |||
| 661 | /** |
||
| 662 | * Sets the necessary parameter values in the view. |
||
| 663 | * |
||
| 664 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 665 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 666 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 667 | * @return \Aimeos\MW\View\Iface Modified view object |
||
| 668 | */ |
||
| 669 | protected function setViewParams( \Aimeos\MW\View\Iface $view, array &$tags = array(), &$expire = null ) |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * Translates the plugin error codes to human readable error strings. |
||
| 677 | * |
||
| 678 | * @param array $codes Associative list of scope and object as key and error code as value |
||
| 679 | * @return array List of translated error messages |
||
| 680 | */ |
||
| 681 | protected function translatePluginErrorCodes( array $codes ) |
||
| 697 | } |
||
| 698 |