Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

CanUseDotPath   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 16
ccs 7
cts 9
cp 0.7778
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
use VGirol\JsonApiStructure\Exception\DotPathException;
8
9
trait CanUseDotPath
10
{
11 441
    protected function retrieve($path, $array)
12
    {
13 441
        $segments = explode('.', $path);
14 441
        $value = $array;
15 441
        while ($segment = \array_shift($segments)) {
16 441
            if (!\array_key_exists($segment, $value)) {
17
                throw new DotPathException(
18
                    sprintf('Path "%s" is not valid : segment "%s" does not exists.', $path, $segment)
19
                );
20
            }
21 441
            $value = $value[$segment];
22
        }
23
24 441
        return $value;
25
    }
26
}
27