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