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