Conditions | 24 |
Paths | 65 |
Total Lines | 108 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
119 | protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] ) |
||
120 | { |
||
121 | $id = $item->getId(); |
||
122 | $type = $item->getResourceType(); |
||
123 | |||
124 | if( isset( $this->map[$type][$id] ) || !$item->isAvailable() ) { |
||
125 | return; |
||
126 | } |
||
127 | |||
128 | $attributes = $item->toArray(); |
||
129 | |||
130 | if( isset( $fields[$type] ) ) { |
||
131 | $attributes = array_intersect_key( $attributes, $fields[$type] ); |
||
132 | } |
||
133 | |||
134 | $entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes]; |
||
135 | |||
136 | if( isset( $fcn[$type] ) && $fcn[$type] instanceof \Closure ) { |
||
137 | $entry = $fcn[$type]( $item, $entry ); |
||
138 | } |
||
139 | |||
140 | $this->map[$type][$id] = $entry; // first content, avoid infinite loops |
||
141 | |||
142 | if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface ) |
||
143 | { |
||
144 | foreach( $item->getChildren() as $childItem ) |
||
145 | { |
||
146 | if( $childItem->isAvailable() ) |
||
147 | { |
||
148 | $rtype = $childItem->getResourceType(); |
||
149 | $entry['relationships'][$rtype]['data'][] = ['id' => $childItem->getId(), 'type' => $rtype]; |
||
150 | $this->map( $childItem, $fields, $fcn ); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) |
||
156 | { |
||
157 | foreach( $item->getListItems() as $listItem ) |
||
158 | { |
||
159 | if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() ) |
||
160 | { |
||
161 | $ltype = $listItem->getResourceType(); |
||
162 | $rtype = $refItem->getResourceType(); |
||
163 | $attributes = $listItem->toArray(); |
||
164 | |||
165 | if( isset( $fields[$ltype] ) ) { |
||
166 | $attributes = array_intersect_key( $attributes, $fields[$ltype] ); |
||
167 | } |
||
168 | |||
169 | $data = ['id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $attributes]; |
||
170 | $entry['relationships'][$rtype]['data'][] = $data; |
||
171 | $this->map( $refItem, $fields, $fcn ); |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface ) |
||
177 | { |
||
178 | foreach( $item->getPropertyItems() as $propItem ) |
||
179 | { |
||
180 | if( $propItem->isAvailable() ) |
||
181 | { |
||
182 | $propId = $propItem->getId(); |
||
183 | $rtype = $propItem->getResourceType(); |
||
184 | $entry['relationships'][$rtype]['data'][] = ['id' => $propId, 'type' => $rtype]; |
||
185 | $this->map( $propItem, $fields, $fcn ); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | |||
190 | if( $item instanceof \Aimeos\MShop\Product\Item\Iface ) |
||
191 | { |
||
192 | foreach( $item->getCatalogItems() as $catItem ) |
||
193 | { |
||
194 | if( $catItem->isAvailable() ) |
||
195 | { |
||
196 | $catId = $catItem->getId(); |
||
197 | $rtype = $catItem->getResourceType(); |
||
198 | $entry['relationships'][$rtype]['data'][] = ['id' => $catId, 'type' => $rtype]; |
||
199 | $this->map( $catItem, $fields, $fcn ); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | foreach( $item->getSupplierItems() as $supItem ) |
||
204 | { |
||
205 | if( $supItem->isAvailable() ) |
||
206 | { |
||
207 | $supId = $supItem->getId(); |
||
208 | $rtype = $supItem->getResourceType(); |
||
209 | $entry['relationships'][$rtype]['data'][] = ['id' => $supId, 'type' => $rtype]; |
||
210 | $this->map( $supItem, $fields, $fcn ); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | foreach( $item->getStockItems() as $stockItem ) |
||
215 | { |
||
216 | if( $stockItem->isAvailable() ) |
||
217 | { |
||
218 | $stockId = $stockItem->getId(); |
||
219 | $rtype = $stockItem->getResourceType(); |
||
220 | $entry['relationships'][$rtype]['data'][] = ['id' => $stockId, 'type' => $rtype]; |
||
221 | $this->map( $stockItem, $fields, $fcn ); |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $this->map[$type][$id] = $entry; // full content |
||
227 | } |
||
229 |