| Conditions | 12 |
| Paths | 21 |
| Total Lines | 49 |
| 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 |
||
| 27 | public function exportEvents(Iterator $events): ?NodeValueInterface |
||
| 28 | { |
||
| 29 | $buffer = []; |
||
| 30 | $structures = []; |
||
| 31 | $structure = null; |
||
| 32 | foreach ($events as $event) { |
||
| 33 | switch (true) { |
||
| 34 | case $event instanceof ScalarEventInterface: |
||
| 35 | $buffer[] = $event->getData(); |
||
| 36 | break; |
||
| 37 | |||
| 38 | case $event instanceof BeforeArrayEventInterface: |
||
| 39 | case $event instanceof BeforeObjectEventInterface: |
||
| 40 | $structures[] = $structure; |
||
| 41 | $structure = []; |
||
| 42 | break; |
||
| 43 | |||
| 44 | case $event instanceof BeforeElementEventInterface: |
||
| 45 | case $event instanceof BeforePropertyEventInterface: |
||
| 46 | break; |
||
| 47 | |||
| 48 | case $event instanceof AfterElementEventInterface: |
||
| 49 | $structure[$event->getIndex()] = array_pop($buffer); |
||
| 50 | break; |
||
| 51 | |||
| 52 | case $event instanceof AfterPropertyEventInterface: |
||
| 53 | $structure[$event->getName()] = array_pop($buffer); |
||
| 54 | break; |
||
| 55 | |||
| 56 | case $event instanceof AfterArrayEventInterface: |
||
| 57 | $buffer[] = $structure; |
||
| 58 | $structure = array_pop($structures); |
||
| 59 | break; |
||
| 60 | |||
| 61 | case $event instanceof AfterObjectEventInterface: |
||
| 62 | $buffer[] = (object) $structure; |
||
| 63 | $structure = array_pop($structures); |
||
| 64 | break; |
||
| 65 | |||
| 66 | default: |
||
| 67 | throw new Exception\UnknownEventException($event); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | if (empty($buffer)) { |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | $data = array_pop($buffer); |
||
| 74 | |||
| 75 | return (new NodeValueFactory)->createValue($data); |
||
| 76 | } |
||
| 88 |