Conditions | 5 |
Paths | 5 |
Total Lines | 59 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public function getProvider( \Aimeos\MShop\Coupon\Item\Iface $item, $code ) |
||
32 | { |
||
33 | $names = explode( ',', $item->getProvider() ); |
||
34 | |||
35 | if( ( $providername = array_shift( $names ) ) === null ) { |
||
36 | throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Provider in "%1$s" not available', $item->getProvider() ) ); |
||
37 | } |
||
38 | |||
39 | if( ctype_alnum( $providername ) === false ) { |
||
40 | throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Invalid characters in provider name "%1$s"', $providername ) ); |
||
41 | } |
||
42 | |||
43 | $interface = '\\Aimeos\\MShop\\Coupon\\Provider\\Factory\\Iface'; |
||
44 | $classname = '\\Aimeos\\MShop\\Coupon\\Provider\\' . $providername; |
||
45 | |||
46 | if( class_exists( $classname ) === false ) { |
||
47 | throw new \Aimeos\MShop\Coupon\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
48 | } |
||
49 | |||
50 | $context = $this->getContext(); |
||
51 | $provider = new $classname( $context, $item, $code ); |
||
52 | |||
53 | if( ( $provider instanceof $interface ) === false ) |
||
54 | { |
||
55 | $msg = sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $interface ); |
||
56 | throw new \Aimeos\MShop\Coupon\Exception( $msg ); |
||
57 | } |
||
58 | |||
59 | /** mshop/coupon/provider/decorators |
||
60 | * Adds a list of decorators to all coupon provider objects automatcally |
||
61 | * |
||
62 | * Decorators extend the functionality of a class by adding new aspects |
||
63 | * (e.g. log what is currently done), executing the methods of the underlying |
||
64 | * class only in certain conditions (e.g. only for logged in users) or |
||
65 | * modify what is returned to the caller. |
||
66 | * |
||
67 | * This option allows you to wrap decorators |
||
68 | * ("\Aimeos\MShop\Coupon\Provider\Decorator\*") around the coupon provider. |
||
69 | * |
||
70 | * mshop/coupon/provider/decorators = array( 'decorator1' ) |
||
71 | * |
||
72 | * This would add the decorator named "decorator1" defined by |
||
73 | * "\Aimeos\MShop\Coupon\Provider\Decorator\Decorator1" to all coupon provider |
||
74 | * objects. |
||
75 | * |
||
76 | * @param array List of decorator names |
||
77 | * @since 2014.05 |
||
78 | * @category Developer |
||
79 | * @see client/html/common/decorators/default |
||
80 | * @see client/html/account/favorite/decorators/excludes |
||
81 | * @see client/html/account/favorite/decorators/local |
||
82 | */ |
||
83 | $decorators = $context->getConfig()->get( 'mshop/coupon/provider/decorators', [] ); |
||
84 | |||
85 | $object = $this->addCouponDecorators( $item, $code, $provider, $names ); |
||
86 | $object = $this->addCouponDecorators( $item, $code, $object, $decorators ); |
||
87 | |||
88 | return $object->setObject( $object ); |
||
89 | } |
||
90 | |||
129 | } |