IndexMap   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 27
c 1
b 1
f 0
dl 0
loc 80
ccs 36
cts 36
cp 1
rs 10
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A outerIndexExists() 0 3 1
A split() 0 3 1
A join() 0 10 3
A count() 0 4 1
A getOuterIndexes() 0 3 1
A equals() 0 3 1
A __construct() 0 3 1
A getOuterIndex() 0 7 2
A getInnerIndexes() 0 3 1
A isCompatible() 0 18 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Value;
6
7
use ReturnTypeWillChange;
0 ignored issues
show
Bug introduced by
The type ReturnTypeWillChange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
use function array_keys;
10
use function count;
11
use function in_array;
12
13
final class IndexMap implements IndexMapInterface
14
{
15
16 35
    private $outerIndexes;
17
18 35
    public function __construct(?int ...$outerIndexes)
19 35
    {
20
        $this->outerIndexes = $outerIndexes;
21 11
    }
22
23 11
    #[ReturnTypeWillChange]
24
    public function count()
25
    {
26 8
        return count($this->outerIndexes);
27
    }
28 8
29
    public function getInnerIndexes(): array
30
    {
31 21
        return array_keys($this->outerIndexes);
32
    }
33 21
34
    public function getOuterIndexes(): array
35
    {
36 3
        return $this->outerIndexes;
37
    }
38 3
39 2
    public function getOuterIndex(int $innerIndex): int
40
    {
41
        if (!isset($this->outerIndexes[$innerIndex])) {
42 1
            throw new Exception\OuterIndexNotFoundException($innerIndex, $this);
43
        }
44
45 5
        return $this->outerIndexes[$innerIndex];
46
    }
47 5
48
    public function outerIndexExists(int $outerIndex): bool
49
    {
50 4
        return in_array($outerIndex, $this->outerIndexes, true);
51
    }
52 4
53
    public function split(): IndexMapInterface
54
    {
55 4
        return new self(...$this->getInnerIndexes());
56
    }
57 4
58 4
    public function join(IndexMapInterface $indexMap): IndexMapInterface
59 3
    {
60 3
        $outerIndexes = [];
61 1
        foreach ($indexMap->getOuterIndexes() as $innerIndex => $outerIndex) {
62
            $outerIndexes[] = $this->outerIndexExists($innerIndex)
63
                ? $outerIndex
64 4
                : null;
65
        }
66
67 3
        return new self(...$outerIndexes);
68
    }
69 3
70
    public function equals(IndexMapInterface $indexMap): bool
71
    {
72 7
        return $this->outerIndexes === $indexMap->getOuterIndexes();
73
    }
74 7
75 1
    public function isCompatible(IndexMapInterface $indexMap): bool
76
    {
77
        if (count($indexMap) != count($this)) {
78 6
            return false;
79 6
        }
80 5
81 4
        $anotherMap = $indexMap->getOuterIndexes();
82
        foreach ($this->outerIndexes as $innerIndex => $outerIndex) {
83
            if (!isset($outerIndex, $anotherMap[$innerIndex])) {
84 2
                continue;
85 1
            }
86
87
            if ($outerIndex !== $anotherMap[$innerIndex]) {
88
                return false;
89 5
            }
90
        }
91
92
        return true;
93
    }
94
}
95