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 |
||
| 20 | class Standard |
||
| 21 | extends \Aimeos\Controller\Frontend\Base |
||
| 22 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
| 23 | { |
||
| 24 | private $conditions = []; |
||
| 25 | private $filter; |
||
| 26 | private $manager; |
||
| 27 | private $sort; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Common initialization for controller classes. |
||
| 32 | * |
||
| 33 | * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object |
||
| 34 | */ |
||
| 35 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Clones objects in controller and resets values |
||
| 65 | */ |
||
| 66 | public function __clone() |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Returns the aggregated count of products for the given key. |
||
| 74 | * |
||
| 75 | * @param string $key Search key to aggregate for, e.g. "index.attribute.id" |
||
| 76 | * @return array Associative list of key values as key and the product count for this key as value |
||
| 77 | * @since 2019.04 |
||
| 78 | */ |
||
| 79 | public function aggregate( $key ) |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Adds attribute IDs for filtering where products must reference all IDs |
||
| 88 | * |
||
| 89 | * @param array|string $attrIds Attribute ID or list of IDs |
||
| 90 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 91 | * @since 2019.04 |
||
| 92 | */ |
||
| 93 | public function allOf( $attrIds ) |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * Adds catalog IDs for filtering |
||
| 107 | * |
||
| 108 | * @param array|string $catIds Catalog ID or list of IDs |
||
| 109 | * @param string $listtype List type of the products referenced by the categories |
||
| 110 | * @param integer $level Constant from \Aimeos\MW\Tree\Manager\Base if products in subcategories are matched too |
||
| 111 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 112 | * @since 2019.04 |
||
| 113 | */ |
||
| 114 | public function category( $catIds, $listtype = 'default', $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Adds generic condition for filtering products |
||
| 147 | * |
||
| 148 | * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
||
| 149 | * @param string $key Search key defined by the product manager, e.g. "product.status" |
||
| 150 | * @param array|string $value Value or list of values to compare to |
||
| 151 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 152 | * @since 2019.04 |
||
| 153 | */ |
||
| 154 | public function compare( $operator, $key, $value ) |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the product for the given product ID |
||
| 163 | * |
||
| 164 | * @param string $id Unique product ID |
||
| 165 | * @param string[] $domains Domain names of items that are associated with the products and that should be fetched too |
||
| 166 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
| 167 | * @since 2019.04 |
||
| 168 | */ |
||
| 169 | public function get( $id, $domains = ['media', 'price', 'text'] ) |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the product for the given product code |
||
| 177 | * |
||
| 178 | * @param string $code Unique product code |
||
| 179 | * @param string[] $domains Domain names of items that are associated with the products and that should be fetched too |
||
| 180 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
| 181 | * @since 2019.04 |
||
| 182 | */ |
||
| 183 | public function find( $code, $domains = ['media', 'price', 'text'] ) |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Adds attribute IDs for filtering where products must reference at least one ID |
||
| 191 | * |
||
| 192 | * If an array of ID lists is given, each ID list is added separately as condition. |
||
| 193 | * |
||
| 194 | * @param array|string $attrIds Attribute ID, list of IDs or array of lists with IDs |
||
| 195 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 196 | * @since 2019.04 |
||
| 197 | */ |
||
| 198 | public function oneOf( $attrIds ) |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Parses the given array and adds the conditions to the list of conditions |
||
| 218 | * |
||
| 219 | * @param array $conditions List of conditions, e.g. [['>' => ['product.status' => 0]], ['==' => ['product.type' => 'default']]] |
||
| 220 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 221 | * @since 2019.04 |
||
| 222 | */ |
||
| 223 | public function parse( array $conditions ) |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Adds product IDs for filtering |
||
| 232 | * |
||
| 233 | * @param array|string $prodIds Product ID or list of IDs |
||
| 234 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 235 | * @since 2019.04 |
||
| 236 | */ |
||
| 237 | public function product( $prodIds ) |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the products filtered by the previously assigned conditions |
||
| 249 | * |
||
| 250 | * @param string[] $domains Domain names of items that are associated with the products and that should be fetched too |
||
| 251 | * @param integer &$total Parameter where the total number of found products will be stored in |
||
| 252 | * @return array Ordered list of product items implementing \Aimeos\MShop\Product\Item\Iface |
||
| 253 | * @since 2019.04 |
||
| 254 | */ |
||
| 255 | public function search( $domains = ['media', 'price', 'text'], &$total = null ) |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets the start value and the number of returned products for slicing the list of found products |
||
| 264 | * |
||
| 265 | * @param integer $start Start value of the first product in the list |
||
| 266 | * @param integer $limit Number of returned products |
||
| 267 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 268 | * @since 2019.04 |
||
| 269 | */ |
||
| 270 | public function slice( $start, $limit ) |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Sets the sorting of the product list |
||
| 279 | * |
||
| 280 | * @param string|null $key Sortation of the product list like "name", "-name", "price", "-price", "code", "-code", "ctime, "-ctime" and "relevance", null for no sortation |
||
| 281 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 282 | * @since 2019.04 |
||
| 283 | */ |
||
| 284 | public function sort( $key = null ) |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Adds supplier IDs for filtering |
||
| 339 | * |
||
| 340 | * @param array|string $supIds Supplier ID or list of IDs |
||
| 341 | * @param string $listtype List type of the products referenced by the suppliers |
||
| 342 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 343 | * @since 2019.04 |
||
| 344 | */ |
||
| 345 | public function supplier( $supIds, $listtype = 'default' ) |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Adds input string for full text search |
||
| 364 | * |
||
| 365 | * @param string|null $text User input for full text search |
||
| 366 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 367 | * @since 2019.04 |
||
| 368 | */ |
||
| 369 | public function text( $text ) |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Returns the list of catalog IDs for the given catalog tree |
||
| 385 | * |
||
| 386 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item with children |
||
| 387 | * @return array List of catalog IDs |
||
| 388 | */ |
||
| 389 | protected function getCatalogIdsFromTree( \Aimeos\MShop\Catalog\Item\Iface $item ) |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Validates the given IDs as integers |
||
| 407 | * |
||
| 408 | * @param array $ids List of IDs to validate |
||
| 409 | * @return array List of validated IDs |
||
| 410 | */ |
||
| 411 | protected function validateIds( array $ids ) |
||
| 424 | } |
||
| 425 |