Completed
Push — master ( c16c79...ab8250 )
by Edward
04:36
created

EventDecoder::exportEvents()   C

Complexity

Conditions 13
Paths 31

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 38
c 1
b 0
f 0
nc 31
nop 1
dl 0
loc 51
ccs 0
cts 37
cp 0
crap 182
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Export;
5
6
use Iterator;
7
use Remorhaz\JSON\Data\Event\AfterArrayEventInterface;
8
use Remorhaz\JSON\Data\Event\AfterElementEventInterface;
9
use Remorhaz\JSON\Data\Event\AfterObjectEventInterface;
10
use Remorhaz\JSON\Data\Event\AfterPropertyEventInterface;
11
use Remorhaz\JSON\Data\Event\BeforeArrayEventInterface;
12
use Remorhaz\JSON\Data\Event\BeforeElementEventInterface;
13
use Remorhaz\JSON\Data\Event\BeforeObjectEventInterface;
14
use Remorhaz\JSON\Data\Event\BeforePropertyEventInterface;
15
use Remorhaz\JSON\Data\Event\ScalarEventInterface;
16
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory;
17
use Remorhaz\JSON\Data\Value\NodeValueInterface;
18
19
final class EventDecoder implements EventDecoderInterface
20
{
21
22
    public function exportEvents(Iterator $events): ?NodeValueInterface
23
    {
24
        $buffer = [];
25
        $structures = [];
26
        $structure = null;
27
        foreach ($events as $event) {
28
            switch (true) {
29
                case $event instanceof ScalarEventInterface:
30
                    $buffer[] = $event->getData();
31
                    break;
32
33
                case $event instanceof BeforeArrayEventInterface:
34
                case $event instanceof BeforeObjectEventInterface:
35
                    $structures[] = $structure;
36
                    $structure = [];
37
                    break;
38
39
                case $event instanceof BeforeElementEventInterface:
40
                case $event instanceof BeforePropertyEventInterface:
41
                    break;
42
43
                case $event instanceof AfterElementEventInterface:
44
                    $structure[$event->getIndex()] = array_pop($buffer);
45
                    break;
46
47
                case $event instanceof AfterPropertyEventInterface:
48
                    $structure[$event->getName()] = array_pop($buffer);
49
                    break;
50
51
                case $event instanceof AfterArrayEventInterface:
52
                    $buffer[] = $structure;
53
                    $structure = array_pop($structures);
54
                    break;
55
56
                case $event instanceof AfterObjectEventInterface:
57
                    $buffer[] = (object) $structure;
58
                    $structure = array_pop($structures);
59
                    break;
60
61
                default:
62
                    throw new Exception\UnknownEventException($event);
0 ignored issues
show
Bug introduced by
The type Remorhaz\JSON\Data\Expor...n\UnknownEventException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
            }
64
        }
65
        if (empty($buffer)) {
66
            return null;
67
        }
68
        $data = array_pop($buffer);
69
        if (empty($buffer)) {
70
            return (new NodeValueFactory)->createValue($data);
71
        }
72
        throw new Exception\InvalidDataBufferException($buffer);
0 ignored issues
show
Bug introduced by
The type Remorhaz\JSON\Data\Expor...alidDataBufferException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
    }
74
}
75