Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Standard |
||
| 22 | extends \Aimeos\Client\Html\Common\Client\Factory\Base |
||
| 23 | implements \Aimeos\Client\Html\Common\Client\Factory\Iface |
||
| 24 | { |
||
| 25 | /** client/html/account/watch/standard/subparts |
||
| 26 | * List of HTML sub-clients rendered within the account watch section |
||
| 27 | * |
||
| 28 | * The output of the frontend is composed of the code generated by the HTML |
||
| 29 | * clients. Each HTML client can consist of serveral (or none) sub-clients |
||
| 30 | * that are responsible for rendering certain sub-parts of the output. The |
||
| 31 | * sub-clients can contain HTML clients themselves and therefore a |
||
| 32 | * hierarchical tree of HTML clients is composed. Each HTML client creates |
||
| 33 | * the output that is placed inside the container of its parent. |
||
| 34 | * |
||
| 35 | * At first, always the HTML code generated by the parent is printed, then |
||
| 36 | * the HTML code of its sub-clients. The order of the HTML sub-clients |
||
| 37 | * determines the order of the output of these sub-clients inside the parent |
||
| 38 | * container. If the configured list of clients is |
||
| 39 | * |
||
| 40 | * array( "subclient1", "subclient2" ) |
||
| 41 | * |
||
| 42 | * you can easily change the order of the output by reordering the subparts: |
||
| 43 | * |
||
| 44 | * client/html/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
| 45 | * |
||
| 46 | * You can also remove one or more parts if they shouldn't be rendered: |
||
| 47 | * |
||
| 48 | * client/html/<clients>/subparts = array( "subclient1" ) |
||
| 49 | * |
||
| 50 | * As the clients only generates structural HTML, the layout defined via CSS |
||
| 51 | * should support adding, removing or reordering content by a fluid like |
||
| 52 | * design. |
||
| 53 | * |
||
| 54 | * @param array List of sub-client names |
||
| 55 | * @since 2014.03 |
||
| 56 | * @category Developer |
||
| 57 | */ |
||
| 58 | private $subPartPath = 'client/html/account/watch/standard/subparts'; |
||
| 59 | private $subPartNames = array(); |
||
| 60 | private $cache; |
||
|
|
|||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the HTML code for insertion into the body. |
||
| 65 | * |
||
| 66 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 67 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 68 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 69 | * @return string HTML code |
||
| 70 | */ |
||
| 71 | public function getBody( $uid = '', array &$tags = array(), &$expire = null ) |
||
| 72 | { |
||
| 73 | $context = $this->getContext(); |
||
| 74 | $view = $this->getView(); |
||
| 75 | |||
| 76 | try |
||
| 77 | { |
||
| 78 | $view = $this->setViewParams( $view, $tags, $expire ); |
||
| 79 | |||
| 80 | $html = ''; |
||
| 81 | foreach( $this->getSubClients() as $subclient ) { |
||
| 82 | $html .= $subclient->setView( $view )->getBody( $uid, $tags, $expire ); |
||
| 83 | } |
||
| 84 | $view->watchBody = $html; |
||
| 85 | } |
||
| 86 | catch( \Aimeos\Client\Html\Exception $e ) |
||
| 87 | { |
||
| 88 | $error = array( $this->getContext()->getI18n()->dt( 'client', $e->getMessage() ) ); |
||
| 89 | $view->watchErrorList = $view->get( 'watchErrorList', array() ) + $error; |
||
| 90 | } |
||
| 91 | catch( \Aimeos\Controller\Frontend\Exception $e ) |
||
| 92 | { |
||
| 93 | $error = array( $this->getContext()->getI18n()->dt( 'controller/frontend', $e->getMessage() ) ); |
||
| 94 | $view->watchErrorList = $view->get( 'watchErrorList', array() ) + $error; |
||
| 95 | } |
||
| 96 | catch( \Aimeos\MShop\Exception $e ) |
||
| 97 | { |
||
| 98 | $error = array( $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
| 99 | $view->watchErrorList = $view->get( 'watchErrorList', array() ) + $error; |
||
| 100 | } |
||
| 101 | catch( \Exception $e ) |
||
| 102 | { |
||
| 103 | $context->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() ); |
||
| 104 | |||
| 105 | $error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) ); |
||
| 106 | $view->watchErrorList = $view->get( 'watchErrorList', array() ) + $error; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** client/html/account/watch/standard/template-body |
||
| 110 | * Relative path to the HTML body template of the account watch client. |
||
| 111 | * |
||
| 112 | * The template file contains the HTML code and processing instructions |
||
| 113 | * to generate the result shown in the body of the frontend. The |
||
| 114 | * configuration string is the path to the template file relative |
||
| 115 | * to the templates directory (usually in client/html/templates). |
||
| 116 | * |
||
| 117 | * You can overwrite the template file configuration in extensions and |
||
| 118 | * provide alternative templates. These alternative templates should be |
||
| 119 | * named like the default one but with the string "standard" replaced by |
||
| 120 | * an unique name. You may use the name of your project for this. If |
||
| 121 | * you've implemented an alternative client class as well, "standard" |
||
| 122 | * should be replaced by the name of the new class. |
||
| 123 | * |
||
| 124 | * @param string Relative path to the template creating code for the HTML page body |
||
| 125 | * @since 2014.03 |
||
| 126 | * @category Developer |
||
| 127 | * @see client/html/account/watch/standard/template-header |
||
| 128 | */ |
||
| 129 | $tplconf = 'client/html/account/watch/standard/template-body'; |
||
| 130 | $default = 'account/watch/body-default.php'; |
||
| 131 | |||
| 132 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the HTML string for insertion into the header. |
||
| 138 | * |
||
| 139 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 140 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 141 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 142 | * @return string|null String including HTML tags for the header on error |
||
| 143 | */ |
||
| 144 | public function getHeader( $uid = '', array &$tags = array(), &$expire = null ) |
||
| 145 | { |
||
| 146 | try |
||
| 147 | { |
||
| 148 | $view = $this->setViewParams( $this->getView(), $tags, $expire ); |
||
| 149 | |||
| 150 | $html = ''; |
||
| 151 | foreach( $this->getSubClients() as $subclient ) { |
||
| 152 | $html .= $subclient->setView( $view )->getHeader( $uid, $tags, $expire ); |
||
| 153 | } |
||
| 154 | $view->watchHeader = $html; |
||
| 155 | |||
| 156 | /** client/html/account/watch/standard/template-header |
||
| 157 | * Relative path to the HTML header template of the account watch client. |
||
| 158 | * |
||
| 159 | * The template file contains the HTML code and processing instructions |
||
| 160 | * to generate the HTML code that is inserted into the HTML page header |
||
| 161 | * of the rendered page in the frontend. The configuration string is the |
||
| 162 | * path to the template file relative to the templates directory (usually |
||
| 163 | * in client/html/templates). |
||
| 164 | * |
||
| 165 | * You can overwrite the template file configuration in extensions and |
||
| 166 | * provide alternative templates. These alternative templates should be |
||
| 167 | * named like the default one but with the string "standard" replaced by |
||
| 168 | * an unique name. You may use the name of your project for this. If |
||
| 169 | * you've implemented an alternative client class as well, "standard" |
||
| 170 | * should be replaced by the name of the new class. |
||
| 171 | * |
||
| 172 | * @param string Relative path to the template creating code for the HTML page head |
||
| 173 | * @since 2014.03 |
||
| 174 | * @category Developer |
||
| 175 | * @see client/html/account/watch/standard/template-body |
||
| 176 | */ |
||
| 177 | $tplconf = 'client/html/account/watch/standard/template-header'; |
||
| 178 | $default = 'account/watch/header-default.php'; |
||
| 179 | |||
| 180 | return $view->render( $view->config( $tplconf, $default ) ); |
||
| 181 | } |
||
| 182 | catch( \Exception $e ) |
||
| 183 | { |
||
| 184 | $this->getContext()->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() ); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the sub-client given by its name. |
||
| 191 | * |
||
| 192 | * @param string $type Name of the client type |
||
| 193 | * @param string|null $name Name of the sub-client (Default if null) |
||
| 194 | * @return \Aimeos\Client\Html\Iface Sub-client object |
||
| 195 | */ |
||
| 196 | public function getSubClient( $type, $name = null ) |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Processes the input, e.g. store given values. |
||
| 278 | * A view must be available and this method doesn't generate any output |
||
| 279 | * besides setting view variables. |
||
| 280 | */ |
||
| 281 | public function process() |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Tests if the maximum number of entries per user is already reached |
||
| 354 | * |
||
| 355 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Customer list manager |
||
| 356 | * @param string $typeId List type ID of the referenced items |
||
| 357 | * @param string $userId Unique user ID |
||
| 358 | * @param integer $max Maximum number of items that are allowed |
||
| 359 | * @param integer $cnt Number of items that should be added |
||
| 360 | * @return boolean True if items can be added, false if not |
||
| 361 | */ |
||
| 362 | protected function checkLimit( \Aimeos\MShop\Common\Manager\Iface $manager, $typeId, $userId, $max, $cnt ) |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Adds one or more list items to the given user |
||
| 386 | * |
||
| 387 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Customer list manager |
||
| 388 | * @param array $listItems Associative list of the reference IDs as keys and the list items as values |
||
| 389 | * @param array $ids List of referenced IDs |
||
| 390 | * @param string $typeId List type ID of the referenced items |
||
| 391 | * @param string $userId Unique user ID |
||
| 392 | */ |
||
| 393 | protected function addItems( \Aimeos\MShop\Common\Manager\Iface $manager, array $listItems, array $ids, $typeId, $userId ) |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Removes the list items for the given reference IDs |
||
| 417 | * |
||
| 418 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Customer list manager |
||
| 419 | * @param array $listItems Associative list of the reference IDs as keys and the list items as values |
||
| 420 | * @param array $ids List of referenced IDs |
||
| 421 | */ |
||
| 422 | protected function deleteItems( \Aimeos\MShop\Common\Manager\Iface $manager, array $listItems, array $ids ) |
||
| 435 | |||
| 436 | |||
| 437 | /** |
||
| 438 | * Updates the list items for the given reference IDs |
||
| 439 | * |
||
| 440 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Customer list manager |
||
| 441 | * @param array $listItems Associative list of the reference IDs as keys and the list items as values |
||
| 442 | * @param array $ids List of referenced IDs |
||
| 443 | * @param array $config Configuration settins with "timeframe", "pricevalue", "price", "stock" and "currency" |
||
| 444 | */ |
||
| 445 | protected function editItems( \Aimeos\MShop\Common\Manager\Iface $manager, array $listItems, array $ids, array $config ) |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the list items associated to the given user ID |
||
| 465 | * |
||
| 466 | * @param \Aimeos\MShop\Common\Manager\Iface $manager Customer list manager |
||
| 467 | * @param array $refIds IDs of the referenced items |
||
| 468 | * @param string $typeId List type ID of the referenced items |
||
| 469 | * @param string $userId Unique user ID |
||
| 470 | * @return array Associative list of the reference IDs as keys and the list items as values |
||
| 471 | */ |
||
| 472 | protected function getListItems( \Aimeos\MShop\Common\Manager\Iface $manager, array $refIds, $typeId, $userId ) |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns the list of sub-client names configured for the client. |
||
| 494 | * |
||
| 495 | * @return array List of HTML client names |
||
| 496 | */ |
||
| 497 | protected function getSubClientNames() |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns the sanitized page from the parameters for the product list. |
||
| 505 | * |
||
| 506 | * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters |
||
| 507 | * @return integer Page number starting from 1 |
||
| 508 | */ |
||
| 509 | protected function getProductListPage( \Aimeos\MW\View\Iface $view ) |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns the sanitized page size from the parameters for the product list. |
||
| 518 | * |
||
| 519 | * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters |
||
| 520 | * @return integer Page size |
||
| 521 | */ |
||
| 522 | protected function getProductListSize( \Aimeos\MW\View\Iface $view ) |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * Sets the necessary parameter values in the view. |
||
| 552 | * |
||
| 553 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 554 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 555 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 556 | * @return \Aimeos\MW\View\Iface Modified view object |
||
| 557 | */ |
||
| 558 | protected function setViewParams( \Aimeos\MW\View\Iface $view, array &$tags = array(), &$expire = null ) |
||
| 624 | } |