Passed
Branch feature/first-release (4212f2)
by Andrea Marco
10:46
created

State::mutateByToken()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 9
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 10
rs 7.6666

How to fix   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
3
namespace Cerbero\JsonParser;
4
5
use Cerbero\JsonParser\Pointers\Pointers;
6
use Cerbero\JsonParser\Tokens\Token;
7
use Cerbero\JsonParser\Tokens\Tokens;
8
9
/**
10
 * The JSON parsing state.
11
 *
12
 */
13
final class State
14
{
15
    /**
16
     * The JSON tree.
17
     *
18
     * @var Tree
19
     */
20
    private Tree $tree;
21
22
    /**
23
     * The JSON buffer.
24
     *
25
     * @var string
26
     */
27
    private string $buffer = '';
28
29
    /**
30
     * Whether an object key is expected.
31
     *
32
     * @var bool
33
     */
34
    public bool $expectsKey = false;
35
36
    /**
37
     * The expected token.
38
     *
39
     * @var int
40
     */
41
    public int $expectedToken = Tokens::COMPOUND_BEGIN;
42
43
    /**
44
     * Instantiate the class.
45
     *
46
     * @param Pointers $pointers
47
     */
48 131
    public function __construct(private Pointers $pointers)
49
    {
50 131
        $this->tree = new Tree($this->pointers);
51
    }
52
53
    /**
54
     * Retrieve the JSON tree
55
     *
56
     * @return Tree
57
     */
58 127
    public function tree(): Tree
59
    {
60 127
        return $this->tree;
61
    }
62
63
    /**
64
     * Retrieve the current key of the JSON tree
65
     *
66
     * @return string|int
67
     */
68 93
    public function key(): string|int
69
    {
70 93
        return $this->tree->currentKey();
71
    }
72
73
    /**
74
     * Determine whether the parser can stop parsing
75
     *
76
     * @return bool
77
     */
78 124
    public function canStopParsing(): bool
79
    {
80 124
        return $this->pointers->wereFoundInTree($this->tree);
81
    }
82
83
    /**
84
     * Call the current pointer callback
85
     *
86
     * @param mixed $value
87
     * @param mixed $key
88
     * @return mixed
89
     */
90 92
    public function callPointer(mixed $value, mixed $key): mixed
91
    {
92 92
        return $this->pointers->matching()->call($value, $key);
93
    }
94
95
    /**
96
     * Mutate state depending on the given token
97
     *
98
     * @param Token $token
99
     * @return void
100
     */
101 127
    public function mutateByToken(Token $token): void
102
    {
103 127
        $shouldTrackTree = $this->tree->shouldBeTracked();
104
105 127
        if ($shouldTrackTree && $this->expectsKey) {
106 87
            $this->tree->traverseKey($token);
107 127
        } elseif ($shouldTrackTree && $token->isValue() && !$this->tree->inObject()) {
108 127
            $this->tree->traverseArray();
109
        }
110
111 127
        if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) {
112 93
            $this->buffer .= $token;
113 93
            $this->pointers->markAsFound();
114
        }
115
116 127
        $token->mutateState($this);
117
    }
118
119
    /**
120
     * Determine whether the buffer contains tokens
121
     *
122
     * @return bool
123
     */
124 125
    public function hasBuffer(): bool
125
    {
126 125
        return $this->buffer != '';
127
    }
128
129
    /**
130
     * Retrieve the value from the buffer and reset it
131
     *
132
     * @return string
133
     */
134 93
    public function value(): string
135
    {
136 93
        $buffer = $this->buffer;
137
138 93
        $this->buffer = '';
139
140 93
        return $buffer;
141
    }
142
}
143