Passed
Branch feature/first-release (5d23f8)
by Andrea Marco
10:49
created

CompoundBegin::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Tokens;
4
5
use Cerbero\JsonParser\State;
6
7
/**
8
 * The token that begins compound data (JSON arrays or objects).
9
 *
10
 */
11
final class CompoundBegin extends Token
12
{
13
    /**
14
     * Whether this compound should be lazy loaded.
15
     *
16
     * @var bool
17
     */
18
    public bool $shouldLazyLoad = false;
19
20
    /**
21
     * Mutate the given state
22
     *
23
     * @param State $state
24
     * @return void
25
     */
26 136
    public function mutateState(State $state): void
27
    {
28 136
        $tree = $state->tree();
29
30 136
        if ($this->shouldLazyLoad = $this->shouldLazyLoad && $tree->depth() >= 0) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $this->shouldLazyLoad = ...&& $tree->depth() >= 0), Probably Intended Meaning: ($this->shouldLazyLoad =... && $tree->depth() >= 0
Loading history...
31 9
            $state->expectedToken = $tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
32 9
            return;
33
        }
34
35 136
        $state->expectsKey = $beginsObject = $this->value == '{';
36 136
        $state->expectedToken = $beginsObject ? Tokens::AFTER_OBJECT_BEGIN : Tokens::AFTER_ARRAY_BEGIN;
37 136
        $tree->deepen($beginsObject);
38
    }
39
40
    /**
41
     * Set the token value
42
     *
43
     * @param string $value
44
     * @return static
45
     */
46 136
    public function setValue(string $value): static
47
    {
48 136
        $this->shouldLazyLoad = false;
49
50 136
        return parent::setValue($value);
51
    }
52
53
    /**
54
     * Determine whether this token ends a JSON chunk
55
     *
56
     * @return bool
57
     */
58 136
    public function endsChunk(): bool
59
    {
60 136
        return $this->shouldLazyLoad;
61
    }
62
}
63