| Total Complexity | 42 |
| Total Lines | 430 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| 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 array $type; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Adds a filter callback for an item type |
||
| 29 | * |
||
| 30 | * @param string $iface Interface name of the item to apply the filter to |
||
| 31 | * @param \Closure $fcn Anonymous function receiving the item to check as first parameter |
||
| 32 | */ |
||
| 33 | public function addFilter( string $iface, \Closure $fcn ) |
||
| 34 | { |
||
| 35 | if( !isset( $this->filterFcn[$iface] ) ) { |
||
| 36 | $this->filterFcn[$iface] = []; |
||
| 37 | } |
||
| 38 | |||
| 39 | $this->filterFcn[$iface][] = $fcn; |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns the class names of the manager and used decorators. |
||
| 45 | * |
||
| 46 | * @return array List of class names |
||
| 47 | */ |
||
| 48 | public function classes() : array |
||
| 49 | { |
||
| 50 | return [get_class( $this )]; |
||
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Removes old entries from the storage |
||
| 56 | * |
||
| 57 | * @param iterable $siteids List of IDs for sites whose entries should be deleted |
||
| 58 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 59 | */ |
||
| 60 | public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 61 | { |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a new empty item instance |
||
| 68 | * |
||
| 69 | * @param array $values Values the item should be initialized with |
||
| 70 | * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object |
||
| 71 | */ |
||
| 72 | public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
||
| 73 | { |
||
| 74 | return new \Aimeos\MShop\Common\Item\Base( $this->prefix(), $values ); |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Creates a new cursor based on the filter criteria |
||
| 80 | * |
||
| 81 | * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc. |
||
| 82 | * @return \Aimeos\MShop\Common\Cursor\Iface Cursor object |
||
| 83 | */ |
||
| 84 | public function cursor( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Cursor\Iface |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * Deletes one or more items. |
||
| 92 | * |
||
| 93 | * @param \Aimeos\MShop\Common\Item\Iface|\Aimeos\Map|array|string $items Item object, ID or a list of them |
||
| 94 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 95 | */ |
||
| 96 | public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Creates a filter object. |
||
| 104 | * |
||
| 105 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
| 106 | * @param bool $site TRUE for adding site criteria to limit items by the site of related items |
||
| 107 | * @return \Aimeos\Base\Criteria\Iface Returns the filter object |
||
| 108 | */ |
||
| 109 | public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Creates objects from the given array |
||
| 117 | * |
||
| 118 | * @param iterable $entries List of associative arrays with key/value pairs |
||
| 119 | * @param array $refs List of domains to retrieve list items and referenced items for |
||
| 120 | * @param array $excludes List of keys which shouldn't be used when creating the items |
||
| 121 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface |
||
| 122 | */ |
||
| 123 | public function from( iterable $entries, array $refs = [], array $excludes = [] ) : \Aimeos\Map |
||
| 124 | { |
||
| 125 | $list = []; |
||
| 126 | $keys = array_flip( $excludes ); |
||
| 127 | |||
| 128 | foreach( $entries as $key => $entry ) |
||
| 129 | { |
||
| 130 | $entry = array_diff_key( $entry, $keys ); |
||
| 131 | $list[$key] = $this->create()->fromArray( $entry, true ); |
||
| 132 | } |
||
| 133 | |||
| 134 | return map( $list ); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the item specified by its ID |
||
| 140 | * |
||
| 141 | * @param string $id Id of item |
||
| 142 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
| 143 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
| 144 | * @return \Aimeos\MShop\Common\Item\Iface Item object |
||
| 145 | */ |
||
| 146 | public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 147 | { |
||
| 148 | throw new \LogicException( 'Not implemented' ); |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns the additional column/search definitions |
||
| 154 | * |
||
| 155 | * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface |
||
| 156 | */ |
||
| 157 | public function getSaveAttributes() : array |
||
| 158 | { |
||
| 159 | return []; |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the attributes that can be used for searching. |
||
| 165 | * |
||
| 166 | * @param bool $withsub Return also attributes of sub-managers if true |
||
| 167 | * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items |
||
| 168 | */ |
||
| 169 | public function getSearchAttributes( bool $withsub = true ) : array |
||
| 170 | { |
||
| 171 | return []; |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns a new manager for attribute extensions |
||
| 177 | * |
||
| 178 | * @param string $manager Name of the sub manager type in lower case |
||
| 179 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
| 180 | * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc. |
||
| 181 | */ |
||
| 182 | public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 183 | { |
||
| 184 | throw new \LogicException( 'Not implemented' ); |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Iterates over all matched items and returns the found ones |
||
| 190 | * |
||
| 191 | * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor |
||
| 192 | * @param string[] $ref List of domains whose items should be fetched too |
||
| 193 | * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
||
| 194 | */ |
||
| 195 | public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map |
||
| 196 | { |
||
| 197 | return null; |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Adds or updates an item object or a list of them. |
||
| 203 | * |
||
| 204 | * @param \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved |
||
| 205 | * @param bool $fetch True if the new ID should be returned in the item |
||
| 206 | * @return \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface Saved item or items |
||
| 207 | */ |
||
| 208 | public function save( $items, bool $fetch = true ) |
||
| 209 | { |
||
| 210 | return $items; |
||
| 211 | } |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Saves the dependent items of the item |
||
| 216 | * |
||
| 217 | * @param \Aimeos\MShop\Common\Item\Iface $item Item object |
||
| 218 | * @param bool $fetch True if the new ID should be returned in the item |
||
| 219 | * @return \Aimeos\MShop\Common\Item\Iface Updated item |
||
| 220 | */ |
||
| 221 | public function saveRefs( \Aimeos\MShop\Common\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Common\Item\Iface |
||
| 222 | { |
||
| 223 | return $item; |
||
| 224 | } |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * Searches for all items matching the given critera. |
||
| 229 | * |
||
| 230 | * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc. |
||
| 231 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
| 232 | * @param int &$total Number of items that are available in total |
||
| 233 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
||
| 234 | */ |
||
| 235 | public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map |
||
| 236 | { |
||
| 237 | return map(); |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Merges the data from the given map and the referenced items |
||
| 243 | * |
||
| 244 | * @param array $entries Associative list of ID as key and the associative list of property key/value pairs as values |
||
| 245 | * @param array $ref List of referenced items to fetch and add to the entries |
||
| 246 | * @return array Associative list of ID as key and the updated entries as value |
||
| 247 | */ |
||
| 248 | public function searchRefs( array $entries, array $ref ) : array |
||
| 249 | { |
||
| 250 | return $entries; |
||
| 251 | } |
||
| 252 | |||
| 253 | |||
| 254 | /** |
||
| 255 | * Injects the reference of the outmost object |
||
| 256 | * |
||
| 257 | * @param \Aimeos\MShop\Common\Manager\Iface $object Reference to the outmost manager or decorator |
||
| 258 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 259 | */ |
||
| 260 | public function setObject( \Aimeos\MShop\Common\Manager\Iface $object ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 261 | { |
||
| 262 | $this->object = $object; |
||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the type of the mananger as separate parts |
||
| 269 | * |
||
| 270 | * @return string[] List of manager part names |
||
| 271 | */ |
||
| 272 | public function type() : array |
||
| 273 | { |
||
| 274 | if( !isset( $this->type ) ) |
||
| 275 | { |
||
| 276 | $parts = array_slice( explode( '\\', strtolower( get_class( $this ) ) ), 2, -1 ); |
||
| 277 | unset( $parts[1] ); |
||
| 278 | $this->type = array_values( $parts ); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this->type; |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Starts a database transaction on the connection identified by the given name |
||
| 287 | * |
||
| 288 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 289 | */ |
||
| 290 | public function begin() : \Aimeos\MShop\Common\Manager\Iface |
||
| 291 | { |
||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Commits the running database transaction on the connection identified by the given name |
||
| 298 | * |
||
| 299 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 300 | */ |
||
| 301 | public function commit() : \Aimeos\MShop\Common\Manager\Iface |
||
| 302 | { |
||
| 303 | return $this; |
||
| 304 | } |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Rolls back the running database transaction on the connection identified by the given name |
||
| 309 | * |
||
| 310 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
||
| 311 | */ |
||
| 312 | public function rollback() : \Aimeos\MShop\Common\Manager\Iface |
||
| 313 | { |
||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Applies the filters for the item type to the item |
||
| 320 | * |
||
| 321 | * @param object $item Item to apply the filter to |
||
| 322 | * @return object|null Object if the item should be used, null if not |
||
| 323 | */ |
||
| 324 | protected function applyFilter( $item ) |
||
| 325 | { |
||
| 326 | foreach( $this->filterFcn as $iface => $fcnList ) |
||
| 327 | { |
||
| 328 | if( $item instanceof $iface ) |
||
| 329 | { |
||
| 330 | foreach( $fcnList as $fcn ) |
||
| 331 | { |
||
| 332 | if( $fcn( $item ) === null ) { |
||
| 333 | return null; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | return $item; |
||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Creates the criteria attribute items from the list of entries |
||
| 345 | * |
||
| 346 | * @param array $list Associative array of code as key and array with properties as values |
||
| 347 | * @return \Aimeos\Base\Criteria\Attribute\Standard[] List of criteria attribute items |
||
| 348 | */ |
||
| 349 | protected function createAttributes( array $list ) : array |
||
| 350 | { |
||
| 351 | $attr = []; |
||
| 352 | |||
| 353 | foreach( $list as $key => $fields ) |
||
| 354 | { |
||
| 355 | $fields['code'] = $fields['code'] ?? $key; |
||
| 356 | $fields['internalcode'] = $fields['internalcode'] ?? $key; |
||
| 357 | $attr[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $fields ); |
||
| 358 | } |
||
| 359 | |||
| 360 | return $attr; |
||
| 361 | } |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the attribute helper functions for searching defined by the manager. |
||
| 366 | * |
||
| 367 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 368 | * @return array Associative array of attribute code and helper function |
||
| 369 | */ |
||
| 370 | protected function getSearchFunctions( array $attributes ) : array |
||
| 371 | { |
||
| 372 | $list = []; |
||
| 373 | |||
| 374 | foreach( $attributes as $key => $item ) { |
||
| 375 | $list[$item->getCode()] = $item->getFunction(); |
||
| 376 | } |
||
| 377 | |||
| 378 | return $list; |
||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the attribute translations for searching defined by the manager. |
||
| 384 | * |
||
| 385 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 386 | * @return array Associative array of attribute code and internal attribute code |
||
| 387 | */ |
||
| 388 | protected function getSearchTranslations( array $attributes ) : array |
||
| 389 | { |
||
| 390 | $list = []; |
||
| 391 | |||
| 392 | foreach( $attributes as $key => $item ) { |
||
| 393 | $list[$item->getCode()] = $item->getInternalCode(); |
||
| 394 | } |
||
| 395 | |||
| 396 | return $list; |
||
| 397 | } |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the attribute types for searching defined by the manager. |
||
| 402 | * |
||
| 403 | * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
||
| 404 | * @return array Associative array of attribute code and internal attribute type |
||
| 405 | */ |
||
| 406 | protected function getSearchTypes( array $attributes ) : array |
||
| 407 | { |
||
| 408 | $list = []; |
||
| 409 | |||
| 410 | foreach( $attributes as $key => $item ) { |
||
| 411 | $list[$item->getCode()] = $item->getInternalType(); |
||
| 412 | } |
||
| 413 | |||
| 414 | return $list; |
||
| 415 | } |
||
| 416 | |||
| 417 | |||
| 418 | /** |
||
| 419 | * Checks if the given domain is in the list of domains |
||
| 420 | * |
||
| 421 | * @param array $ref List of domains |
||
| 422 | * @param string $domain Domain to check for |
||
| 423 | * @return bool True if domain is in the list, false if not |
||
| 424 | */ |
||
| 425 | protected function hasRef( array $ref, string $domain ) : bool |
||
| 426 | { |
||
| 427 | return isset( $ref[$domain] ) || in_array( $domain, $ref ); |
||
| 428 | } |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns the outmost decorator of the decorator stack |
||
| 433 | * |
||
| 434 | * @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object |
||
| 435 | */ |
||
| 436 | protected function object() : \Aimeos\MShop\Common\Manager\Iface |
||
| 437 | { |
||
| 438 | return $this->object ?? $this; |
||
| 439 | } |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Returns the prefix for the item properties and search keys. |
||
| 444 | * |
||
| 445 | * @return string Prefix for the item properties and search keys |
||
| 446 | */ |
||
| 447 | protected function prefix() : string |
||
| 450 | } |
||
| 451 | } |
||
| 452 |