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