Passed
Branch feature/first-release (4f172a)
by Andrea Marco
01:55
created

State::mutateByToken()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 6
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace Cerbero\JsonParser\ValueObjects;
4
5
use Cerbero\JsonParser\Pointers\Pointers;
6
use Cerbero\JsonParser\Tokens\CompoundBegin;
7
use Cerbero\JsonParser\Tokens\Parser;
8
use Cerbero\JsonParser\Tokens\Token;
9
use Cerbero\JsonParser\Tokens\Tokens;
10
use Closure;
11
12
/**
13
 * The JSON parsing state.
14
 *
15
 */
16
final class State
17
{
18
    /**
19
     * The JSON tree.
20
     *
21
     * @var Tree
22
     */
23
    private Tree $tree;
24
25
    /**
26
     * The JSON buffer.
27
     *
28
     * @var Parser|string
29
     */
30
    private Parser|string $buffer = '';
31
32
    /**
33
     * Whether an object key is expected.
34
     *
35
     * @var bool
36
     */
37
    public bool $expectsKey = false;
38
39
    /**
40
     * The expected token.
41
     *
42
     * @var int
43
     */
44
    public int $expectedToken = Tokens::COMPOUND_BEGIN;
45
46
    /**
47
     * Instantiate the class.
48
     *
49
     * @param Pointers $pointers
50
     * @param Closure $lazyLoad
51
     */
52 330
    public function __construct(private Pointers $pointers, private Closure $lazyLoad)
53
    {
54 330
        $this->tree = new Tree($this->pointers);
55
    }
56
57
    /**
58
     * Retrieve the JSON tree
59
     *
60
     * @return Tree
61
     */
62 326
    public function tree(): Tree
63
    {
64 326
        return $this->tree;
65
    }
66
67
    /**
68
     * Retrieve the current key of the JSON tree
69
     *
70
     * @return string|int
71
     */
72 238
    public function key(): string|int
73
    {
74 238
        return $this->tree->currentKey();
75
    }
76
77
    /**
78
     * Determine whether the parser can stop parsing
79
     *
80
     * @return bool
81
     */
82 323
    public function canStopParsing(): bool
83
    {
84 323
        return $this->pointers->wereFoundInTree($this->tree);
85
    }
86
87
    /**
88
     * Call the current pointer callback
89
     *
90
     * @param mixed $value
91
     * @param mixed $key
92
     * @return mixed
93
     */
94 237
    public function callPointer(mixed $value, mixed $key): mixed
95
    {
96 237
        return $this->pointers->matching()->call($value, $key);
97
    }
98
99
    /**
100
     * Mutate state depending on the given token
101
     *
102
     * @param Token $token
103
     * @return void
104
     */
105 326
    public function mutateByToken(Token $token): void
106
    {
107 326
        $this->tree->traverseToken($token, $this->expectsKey);
108
109 326
        if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) {
110 238
            $shouldLazyLoad = $token instanceof CompoundBegin && $this->pointers->matching()->isLazy();
111
            /** @phpstan-ignore-next-line */
112 238
            $this->buffer = $shouldLazyLoad ? ($this->lazyLoad)() : $this->buffer . $token;
0 ignored issues
show
Bug introduced by
Are you sure $this->buffer of type Cerbero\JsonParser\Tokens\Parser|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $this->buffer = $shouldLazyLoad ? ($this->lazyLoad)() : /** @scrutinizer ignore-type */ $this->buffer . $token;
Loading history...
113
            /** @var CompoundBegin $token */
114 238
            $shouldLazyLoad && $token->shouldLazyLoad = true;
115
        }
116
117 326
        $token->mutateState($this);
118
    }
119
120
    /**
121
     * Determine whether the buffer contains tokens
122
     *
123
     * @return bool
124
     */
125 324
    public function hasBuffer(): bool
126
    {
127 324
        return $this->buffer != '';
128
    }
129
130
    /**
131
     * Retrieve the value from the buffer and reset it
132
     *
133
     * @return Parser|string
134
     */
135 238
    public function value(): Parser|string
136
    {
137 238
        $buffer = $this->buffer;
138
139 238
        $this->buffer = '';
140
141 238
        return $buffer;
142
    }
143
}
144