| Conditions | 11 |
| Paths | 5 |
| Total Lines | 39 |
| 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 |
||
| 20 | public function toArray(): array |
||
| 21 | { |
||
| 22 | $result = []; |
||
| 23 | |||
| 24 | $items = get_object_vars($this); |
||
| 25 | foreach ($items as $key => $item) { |
||
| 26 | if (is_array($item)) { |
||
| 27 | foreach ($item as $subKey => $subItem) { |
||
| 28 | if ($subItem instanceof ModelInterface) { |
||
| 29 | |||
| 30 | switch (get_class($subItem)) { |
||
| 31 | case Psr0::class: |
||
| 32 | $subKey = 'psr-0'; |
||
| 33 | break; |
||
| 34 | case Psr4::class: |
||
| 35 | $subKey = 'psr-4'; |
||
| 36 | break; |
||
| 37 | case Classmap::class: |
||
| 38 | $subKey = 'classmap'; |
||
| 39 | break; |
||
| 40 | case Files::class: |
||
| 41 | $subKey = 'files'; |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | |||
| 45 | $result[$this->normalize($key)][$subKey] = $subItem->toArray(); |
||
| 46 | } else { |
||
| 47 | $result[$this->normalize($key)][$subKey] = $subItem; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } elseif ($item instanceof ModelInterface) { |
||
| 51 | $result[$this->normalize($key)] = $item->toArray(); |
||
| 52 | } elseif (!empty($item)) { |
||
| 53 | $result[$this->normalize($key)] = $item; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return $result; |
||
| 58 | } |
||
| 59 | |||
| 98 |