| Total Complexity | 58 |
| Total Lines | 487 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Methods 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.
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 Methods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait Methods |
||
| 21 | { |
||
| 22 | private ?\Aimeos\MShop\Common\Manager\Iface $object = null; |
||
| 23 | private array $filterFcn = []; |
||
| 24 | private string $domain; |
||
| 25 | private string $subpath; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Adds a filter callback for an item type |
||
| 30 | * |
||
| 31 | * @param string $iface Interface name of the item to apply the filter to |
||
| 32 | * @param \Closure $fcn Anonymous function receiving the item to check as first parameter |
||
| 33 | */ |
||
| 34 | public function addFilter( string $iface, \Closure $fcn ) |
||
| 35 | { |
||
| 36 | if( !isset( $this->filterFcn[$iface] ) ) { |
||
| 37 | $this->filterFcn[$iface] = []; |
||
| 38 | } |
||
| 39 | |||
| 40 | $this->filterFcn[$iface][] = $fcn; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the class names of the manager and used decorators. |
||
| 46 | * |
||
| 47 | * @return array List of class names |
||
| 48 | */ |
||
| 49 | public function classes() : array |
||
| 50 | { |
||
| 51 | return [get_class( $this )]; |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Removes old entries from the storage |
||
| 57 | * |
||
| 58 | * @param iterable $siteids List of IDs for sites whose entries should be deleted |
||
| 59 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 60 | */ |
||
| 61 | public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 62 | { |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Creates a new empty item instance |
||
| 69 | * |
||
| 70 | * @param array $values Values the item should be initialized with |
||
| 71 | * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object |
||
| 72 | */ |
||
| 73 | public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
||
| 74 | { |
||
| 75 | return new \Aimeos\MShop\Common\Item\Base( '', $values ); |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Creates a new cursor based on the filter criteria |
||
| 81 | * |
||
| 82 | * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc. |
||
| 83 | * @return \Aimeos\MShop\Common\Cursor\Iface Cursor object |
||
| 84 | */ |
||
| 85 | public function cursor( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Cursor\Iface |
||
| 86 | { |
||
| 87 | return new \Aimeos\MShop\Common\Cursor\Standard( $filter ); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Deletes one or more items. |
||
| 93 | * |
||
| 94 | * @param \Aimeos\MShop\Common\Item\Iface|\Aimeos\Map|array|string $items Item object, ID or a list of them |
||
| 95 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 96 | */ |
||
| 97 | public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 98 | { |
||
| 99 | return $this; |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates a filter object. |
||
| 105 | * |
||
| 106 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
| 107 | * @param bool $site TRUE for adding site criteria to limit items by the site of related items |
||
| 108 | * @return \Aimeos\Base\Criteria\Iface Returns the filter object |
||
| 109 | */ |
||
| 110 | public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
||
| 111 | { |
||
| 112 | throw new \LogicException( 'Not implemented' ); |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the item specified by its ID |
||
| 118 | * |
||
| 119 | * @param string $id Id of item |
||
| 120 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
| 121 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
| 122 | * @return \Aimeos\MShop\Common\Item\Iface Item object |
||
| 123 | */ |
||
| 124 | public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 125 | { |
||
| 126 | throw new \LogicException( 'Not implemented' ); |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the available manager types |
||
| 132 | * |
||
| 133 | * @param bool $withsub Return also the resource type of sub-managers if true |
||
| 134 | * @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
||
| 135 | */ |
||
| 136 | public function getResourceType( bool $withsub = true ) : array |
||
| 137 | { |
||
| 138 | return [$this->getManagerPath()]; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns the additional column/search definitions |
||
| 144 | * |
||
| 145 | * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface |
||
| 146 | */ |
||
| 147 | public function getSaveAttributes() : array |
||
| 148 | { |
||
| 149 | return []; |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the attributes that can be used for searching. |
||
| 155 | * |
||
| 156 | * @param bool $withsub Return also attributes of sub-managers if true |
||
| 157 | * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items |
||
| 158 | */ |
||
| 159 | public function getSearchAttributes( bool $withsub = true ) : array |
||
| 160 | { |
||
| 161 | return []; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a new manager for attribute extensions |
||
| 167 | * |
||
| 168 | * @param string $manager Name of the sub manager type in lower case |
||
| 169 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
| 170 | * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc. |
||
| 171 | */ |
||
| 172 | public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 173 | { |
||
| 174 | throw new \LogicException( 'Not implemented' ); |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Iterates over all matched items and returns the found ones |
||
| 180 | * |
||
| 181 | * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor |
||
| 182 | * @param string[] $ref List of domains whose items should be fetched too |
||
| 183 | * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
||
| 184 | */ |
||
| 185 | public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map |
||
| 186 | { |
||
| 187 | return null; |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Adds or updates an item object or a list of them. |
||
| 193 | * |
||
| 194 | * @param \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved |
||
| 195 | * @param bool $fetch True if the new ID should be returned in the item |
||
| 196 | * @return \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface Saved item or items |
||
| 197 | */ |
||
| 198 | public function save( $items, bool $fetch = true ) |
||
| 201 | } |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Searches for all items matching the given critera. |
||
| 206 | * |
||
| 207 | * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc. |
||
| 208 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
| 209 | * @param int &$total Number of items that are available in total |
||
| 210 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
||
| 211 | */ |
||
| 212 | public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Injects the reference of the outmost object |
||
| 220 | * |
||
| 221 | * @param \Aimeos\MShop\Common\Manager\Iface $object Reference to the outmost manager or decorator |
||
| 222 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 223 | */ |
||
| 224 | public function setObject( \Aimeos\MShop\Common\Manager\Iface $object ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Starts a database transaction on the connection identified by the given name |
||
| 233 | * |
||
| 234 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 235 | */ |
||
| 236 | public function begin() : \Aimeos\MShop\Common\Manager\Iface |
||
| 237 | { |
||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Commits the running database transaction on the connection identified by the given name |
||
| 244 | * |
||
| 245 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 246 | */ |
||
| 247 | public function commit() : \Aimeos\MShop\Common\Manager\Iface |
||
| 248 | { |
||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * Rolls back the running database transaction on the connection identified by the given name |
||
| 255 | * |
||
| 256 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 257 | */ |
||
| 258 | public function rollback() : \Aimeos\MShop\Common\Manager\Iface |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Applies the filters for the item type to the item |
||
| 266 | * |
||
| 267 | * @param object $item Item to apply the filter to |
||
| 268 | * @return object|null Object if the item should be used, null if not |
||
| 269 | */ |
||
| 270 | protected function applyFilter( $item ) |
||
| 271 | { |
||
| 272 | foreach( $this->filterFcn as $iface => $fcnList ) |
||
| 273 | { |
||
| 274 | if( is_object( $item ) && $item instanceof $iface ) |
||
| 275 | { |
||
| 276 | foreach( $fcnList as $fcn ) |
||
| 277 | { |
||
| 278 | if( $fcn( $item ) === null ) { |
||
| 279 | return null; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return $item; |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Creates the criteria attribute items from the list of entries |
||
| 291 | * |
||
| 292 | * @param array $list Associative array of code as key and array with properties as values |
||
| 293 | * @return \Aimeos\Base\Criteria\Attribute\Standard[] List of criteria attribute items |
||
| 294 | */ |
||
| 295 | protected function createAttributes( array $list ) : array |
||
| 306 | } |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Returns the table alias name. |
||
| 311 | * |
||
| 312 | * @return string Table alias name |
||
| 313 | */ |
||
| 314 | protected function getAlias() : string |
||
| 315 | { |
||
| 316 | $parts = explode( '/', $this->getSubPath() ); |
||
| 317 | $str = 'm' . substr( $this->getDomain(), 0, 3 ); |
||
| 318 | |||
| 319 | foreach( $parts as $part ) { |
||
| 320 | $str .= substr( $part, 0, 2 ); |
||
| 321 | } |
||
| 322 | |||
| 323 | return $str; |
||
| 324 | } |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the full configuration key for the passed last part |
||
| 329 | * |
||
| 330 | * @param string $name Configuration last part |
||
| 331 | * @return string Full configuration key |
||
| 332 | */ |
||
| 333 | protected function getConfigKey( string $name ) : string |
||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the manager domain |
||
| 342 | * |
||
| 343 | * @return string Manager domain e.g. "product" |
||
| 344 | */ |
||
| 345 | protected function getDomain() : string |
||
| 346 | { |
||
| 347 | if( !isset( $this->domain ) ) { |
||
| 348 | $this->initMethods(); |
||
| 349 | } |
||
| 350 | |||
| 351 | return $this->domain; |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the manager path |
||
| 357 | * |
||
| 358 | * @return string Manager path e.g. "product/lists/type" |
||
| 359 | */ |
||
| 360 | protected function getManagerPath() : string |
||
| 361 | { |
||
| 362 | $subPath = $this->getSubPath(); |
||
| 363 | return $this->getDomain() . ( $subPath ? '/' . $subPath : '' ); |
||
| 364 | } |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the attribute helper functions for searching defined by the manager. |
||
| 369 | * |
||
| 370 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 371 | * @return array Associative array of attribute code and helper function |
||
| 372 | */ |
||
| 373 | protected function getSearchFunctions( array $attributes ) : array |
||
| 374 | { |
||
| 375 | $list = []; |
||
| 376 | $iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
||
| 377 | |||
| 378 | foreach( $attributes as $key => $item ) |
||
| 379 | { |
||
| 380 | if( $item instanceof $iface ) { |
||
| 381 | $list[$item->getCode()] = $item->getFunction(); |
||
| 382 | } else if( isset( $item['code'] ) ) { |
||
| 383 | $list[$item['code']] = $item['function'] ?? null; |
||
| 384 | } else { |
||
| 385 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | return $list; |
||
| 390 | } |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the item search key for the passed name |
||
| 395 | * |
||
| 396 | * @return string Item prefix e.g. "product.lists.type.id" |
||
| 397 | */ |
||
| 398 | protected function getSearchKey( string $name = '' ) : string |
||
| 399 | { |
||
| 400 | $subPath = $this->getSubPath(); |
||
| 401 | return $this->getDomain() . ( $subPath ? '.' . $subPath : '' ) . ( $name ? '.' . $name : '' ); |
||
| 402 | } |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the attribute translations for searching defined by the manager. |
||
| 407 | * |
||
| 408 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 409 | * @return array Associative array of attribute code and internal attribute code |
||
| 410 | */ |
||
| 411 | protected function getSearchTranslations( array $attributes ) : array |
||
| 412 | { |
||
| 413 | $translations = []; |
||
| 414 | $iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
||
| 415 | |||
| 416 | foreach( $attributes as $key => $item ) |
||
| 417 | { |
||
| 418 | if( $item instanceof $iface ) { |
||
| 419 | $translations[$item->getCode()] = $item->getInternalCode(); |
||
| 420 | } else if( isset( $item['code'] ) ) { |
||
| 421 | $translations[$item['code']] = $item['internalcode']; |
||
| 422 | } else { |
||
| 423 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | return $translations; |
||
| 428 | } |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns the attribute types for searching defined by the manager. |
||
| 433 | * |
||
| 434 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 435 | * @return array Associative array of attribute code and internal attribute type |
||
| 436 | */ |
||
| 437 | protected function getSearchTypes( array $attributes ) : array |
||
| 438 | { |
||
| 439 | $types = []; |
||
| 440 | $iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
||
| 441 | |||
| 442 | foreach( $attributes as $key => $item ) |
||
| 443 | { |
||
| 444 | if( $item instanceof $iface ) { |
||
| 445 | $types[$item->getCode()] = $item->getInternalType(); |
||
| 446 | } else if( isset( $item['code'] ) ) { |
||
| 447 | $types[$item['code']] = $item['internaltype']; |
||
| 448 | } else { |
||
| 449 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | return $types; |
||
| 454 | } |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns the manager domain sub-path |
||
| 459 | * |
||
| 460 | * @return string Manager domain sub-path e.g. "lists/type" |
||
| 461 | */ |
||
| 462 | protected function getSubPath() : string |
||
| 463 | { |
||
| 464 | if( !isset( $this->subpath ) ) { |
||
| 465 | $this->initMethods(); |
||
| 466 | } |
||
| 467 | |||
| 468 | return $this->subpath; |
||
| 469 | } |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * Returns the name of the used table |
||
| 474 | * |
||
| 475 | * @return string Table name e.g. "mshop_product_lists_type" |
||
| 476 | */ |
||
| 477 | protected function getTable() : string |
||
| 481 | } |
||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * Initializes the trait |
||
| 486 | */ |
||
| 487 | protected function initMethods() |
||
| 496 | } |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Returns the outmost decorator of the decorator stack |
||
| 501 | * |
||
| 502 | * @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object |
||
| 503 | */ |
||
| 504 | protected function object() : \Aimeos\MShop\Common\Manager\Iface |
||
| 507 | } |
||
| 508 | } |
||
| 509 |