PathValidation::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Json;
5
6
use function array_key_exists;
7
use function assert;
8
use function is_array;
9
10
trait PathValidation
11
{
12
    /** @throws InvalidOffset */
13
    private function mustBeValid(
14
        $context,
15
        string $offset,
16
        string ...$path
17
    ): void {
18
        if ($this->isValid($context, $offset)) {
19
            return;
20
        }
21
        $inTheJson = $this;
22
        assert($inTheJson instanceof Json);
23
        throw PathLeadsNowhere::didNotFind($offset, $inTheJson, ...$path);
24
    }
25
26
    private function isValid($context, string $offset): bool
27
    {
28
        return is_array($context) && array_key_exists($offset, $context);
29
    }
30
}
31