Path   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A copyWithProperty() 0 3 1
A contains() 0 5 1
A getElements() 0 3 1
A equals() 0 3 1
A copyParent() 0 7 2
A __construct() 0 3 1
A copyWithElement() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Data\Path;
6
7
use function array_slice;
8
use function count;
9
10
final class Path implements PathInterface
11
{
12
13
    private $elements;
14
15
    public function __construct(...$elements)
16
    {
17
        $this->elements = $elements;
18
    }
19
20
    public function copyWithElement(int $index): PathInterface
21
    {
22
        return new self(...$this->elements, ...[$index]);
23
    }
24
25
    public function copyWithProperty(string $name): PathInterface
26
    {
27
        return new self(...$this->elements, ...[$name]);
28
    }
29
30
    public function copyParent(): PathInterface
31
    {
32
        if (empty($this->elements)) {
33
            throw new Exception\ParentNotFoundException($this);
34
        }
35
36
        return new self(...array_slice($this->elements, 0, -1));
37
    }
38
39
    public function getElements(): array
40
    {
41
        return $this->elements;
42
    }
43
44
    public function equals(PathInterface $path): bool
45
    {
46
        return $path->getElements() === $this->elements;
47
    }
48
49
    public function contains(PathInterface $path): bool
50
    {
51
        $subPath = array_slice($path->getElements(), 0, count($this->elements));
52
53
        return $subPath === $this->elements;
54
    }
55
}
56