Conditions | 8 |
Paths | 12 |
Total Lines | 51 |
Code Lines | 15 |
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 |
||
106 | protected function getArticle( \Aimeos\MShop\Product\Item\Iface $productItem, array $variant ) |
||
107 | { |
||
108 | $items = []; |
||
109 | $context = $this->getContext(); |
||
110 | |||
111 | /** controller/frontend/basket/require-variant |
||
112 | * A variant of a selection product must be chosen |
||
113 | * |
||
114 | * Selection products normally consist of several article variants and |
||
115 | * by default exactly one article variant of a selection product can be |
||
116 | * put into the basket. |
||
117 | * |
||
118 | * By setting this option to false, the selection product including the |
||
119 | * chosen attributes (if any attribute values were selected) can be put |
||
120 | * into the basket as well. This makes it possible to get all articles |
||
121 | * or a subset of articles (e.g. all of a color) at once. |
||
122 | * |
||
123 | * This option replace the "client/html/basket/require-variant" setting. |
||
124 | * |
||
125 | * @param boolean True if a variant must be chosen, false if also the selection product with attributes can be added |
||
126 | * @since 2018.01 |
||
127 | * @category Developer |
||
128 | * @category User |
||
129 | */ |
||
130 | $requireVariant = $context->getConfig()->get( 'controller/frontend/basket/require-variant', true ); |
||
131 | |||
132 | foreach( $productItem->getRefItems( 'product', 'default', 'default' ) as $item ) |
||
133 | { |
||
134 | foreach( $variant as $id ) |
||
135 | { |
||
136 | if( $item->getListItem( 'attribute', 'variant', $id ) === null ) { |
||
137 | continue 2; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $items[] = $item; |
||
142 | } |
||
143 | |||
144 | if( count( $items ) > 1 ) |
||
145 | { |
||
146 | $msg = $context->getI18n()->dt( 'controller/frontend', 'No unique article found for selected attributes and product ID "%1$s"' ); |
||
147 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) ); |
||
148 | } |
||
149 | |||
150 | if( empty( $items ) && $requireVariant != false ) // count == 0 |
||
151 | { |
||
152 | $msg = $context->getI18n()->dt( 'controller/frontend', 'No article found for selected attributes and product ID "%1$s"' ); |
||
153 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $productItem->getId() ) ); |
||
154 | } |
||
155 | |||
156 | return current( $items ) ?: $productItem; |
||
157 | } |
||
159 |