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

EventDecoder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 37
cp 0
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
C exportEvents() 0 51 13
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