Passed
Pull Request — master (#33)
by
unknown
10:20
created

PositionResolver::startArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 10
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 14
ccs 8
cts 11
cp 0.7272
crap 2.0811
rs 9.9332
1
<?php
2
3
namespace Swaggest\JsonCli\FilePosition;
4
5
use JsonStreamingParser\Listener\ListenerInterface;
6
use JsonStreamingParser\Listener\PositionAwareInterface;
7
use Swaggest\JsonDiff\JsonPointer;
8
9
class PositionResolver implements ListenerInterface, PositionAwareInterface
10
{
11
    /** @var string[] map of pointer to file positions */
12
    public $resolved;
13
    private $pathState;
14
    private $stack = [];
15 1
16
    public function __construct()
17 1
    {
18 1
        $this->pathState = new PathState();
19 1
        $this->pathState->path = '';
20
    }
21 1
22
    public function startDocument(): void
23 1
    {
24
    }
25 1
26
    public function endDocument(): void
27 1
    {
28
    }
29 1
30
    public function startObject(): void
31 1
    {
32 1
        $path = $this->pathState->path;
33 1
        if ($this->pathState->isArray) {
34 1
            $index = $this->pathState->arrayIndex++;
35 1
            $path = $this->pathState->path . '/' . $index;
36
            $this->resolved[$path] = $this->currentLine . ':' . $this->currentChar;
37
        }
38 1
39 1
        $this->stack[] = $this->pathState;
40 1
        $pathState = new PathState();
41 1
        $pathState->path = $path;
42 1
        $this->pathState = $pathState;
43
    }
44 1
45
    public function endObject(): void
46 1
    {
47 1
        $this->pathState = array_pop($this->stack);
48 1
        if ($this->pathState->isKey) {
49
            $this->pathState = array_pop($this->stack);
50 1
        }
51
    }
52
53 1
54
    public function startArray(): void
55 1
    {
56 1
        $path = $this->pathState->path;
57
        if ($this->pathState->isArray) {
58
            $index = $this->pathState->arrayIndex++;
59
            $path = $this->pathState->path . '/' . $index;
60
            $this->resolved[$path] = $this->currentLine . ':' . $this->currentChar;
61
        }
62 1
63 1
        $this->stack[] = $this->pathState;
64 1
        $pathState = new PathState();
65 1
        $pathState->path = $path;
66 1
        $pathState->isArray = true;
67 1
        $this->pathState = $pathState;
68
    }
69 1
70
    public function endArray(): void
71 1
    {
72 1
        $this->pathState = array_pop($this->stack);
73 1
        if ($this->pathState->isKey) {
74
            $this->pathState = array_pop($this->stack);
75 1
        }
76
    }
77 1
78
    public function key($key): void
79 1
    {
80
        $path = $this->pathState->path . '/' . JsonPointer::escapeSegment($key);
81 1
82 1
        $this->stack[] = $this->pathState;
83 1
        $pathState = new PathState();
84 1
        $pathState->path = $path;
85 1
        $pathState->isKey = true;
86
        $this->pathState = $pathState;
87
88 1
89 1
        $this->resolved[$path] = $this->currentLine . ':' . $this->currentChar;
90
    }
91 1
92
    public function value($value)
93 1
    {
94 1
        if ($this->pathState->isArray) {
95 1
            $index = $this->pathState->arrayIndex++;
96 1
            $itemPath = $this->pathState->path . '/' . $index;
97 1
            $this->resolved[$itemPath] = $this->currentLine . ':' . $this->currentChar;
98 1
        } elseif ($this->pathState->isKey) {
99
            $this->pathState = array_pop($this->stack);
100 1
        }
101
    }
102
103
    public function whitespace($whitespace): void
104
    {
105
    }
106
107
    private $currentLine;
108
    private $currentChar;
109 1
110
    public function setFilePosition(int $line, int $char): void
111 1
    {
112 1
        $this->currentLine = $line;
113 1
        $this->currentChar = $char;
114
    }
115
}