Conditions | 17 |
Paths | 33 |
Total Lines | 72 |
Code Lines | 36 |
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 |
||
115 | protected function map( \Aimeos\MShop\Common\Item\Iface $item, array $fields, array $fcn = [] ) |
||
116 | { |
||
117 | $id = $item->getId(); |
||
118 | $type = $item->getResourceType(); |
||
119 | |||
120 | if( isset( $this->map[$type][$id] ) || !$item->isAvailable() ) { |
||
121 | return; |
||
122 | } |
||
123 | |||
124 | $attributes = $item->toArray(); |
||
125 | |||
126 | if( isset( $fields[$type] ) ) { |
||
127 | $attributes = array_intersect_key( $attributes, $fields[$type] ); |
||
128 | } |
||
129 | |||
130 | $entry = ['id' => $id, 'type' => $type, 'attributes' => $attributes]; |
||
131 | |||
132 | if( isset( $fcn[$type] ) && $fcn[$type] instanceof \Closure ) { |
||
133 | $entry = $fcn[$type]( $item, $entry ); |
||
134 | } |
||
135 | |||
136 | $this->map[$type][$id] = $entry; // first content, avoid infinite loops |
||
137 | |||
138 | if( $item instanceof \Aimeos\MShop\Common\Item\Tree\Iface ) |
||
139 | { |
||
140 | foreach( $item->getChildren() as $childItem ) |
||
141 | { |
||
142 | if( $childItem->isAvailable() ) |
||
143 | { |
||
144 | $rtype = $childItem->getResourceType(); |
||
145 | $entry['relationships'][$rtype]['data'][] = ['id' => $childItem->getId(), 'type' => $rtype]; |
||
146 | $this->map( $childItem, $fields, $fcn ); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) |
||
152 | { |
||
153 | foreach( $item->getListItems() as $listItem ) |
||
154 | { |
||
155 | if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() ) |
||
156 | { |
||
157 | $ltype = $listItem->getResourceType(); |
||
158 | $rtype = $refItem->getResourceType(); |
||
159 | $attributes = $listItem->toArray(); |
||
160 | |||
161 | if( isset( $fields[$ltype] ) ) { |
||
162 | $attributes = array_intersect_key( $attributes, $fields[$ltype] ); |
||
163 | } |
||
164 | |||
165 | $data = ['id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $attributes]; |
||
166 | $entry['relationships'][$rtype]['data'][] = $data; |
||
167 | $this->map( $refItem, $fields, $fcn ); |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | if( $item instanceof \Aimeos\MShop\Common\Item\PropertyRef\Iface ) |
||
173 | { |
||
174 | foreach( $item->getPropertyItems() as $propItem ) |
||
175 | { |
||
176 | if( $propItem->isAvailable() ) |
||
177 | { |
||
178 | $propId = $propItem->getId(); |
||
179 | $rtype = $propItem->getResourceType(); |
||
180 | $entry['relationships'][$rtype]['data'][] = ['id' => $propId, 'type' => $rtype]; |
||
181 | $this->map( $propItem, $fields, $fcn ); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $this->map[$type][$id] = $entry; // full content |
||
187 | } |
||
189 |