ResolveException::create()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 4
nop 5
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\DotKey;
6
7
/**
8
 * Exception throw if path can't be resolved.
9
 */
10
class ResolveException extends \RuntimeException
11
{
12
    /**
13
     * Create a ResolveException, filling out %s with invalid path.
14
     *
15
     * @param string   $msg
16
     * @param string   $path
17
     * @param string   $delimiter
18
     * @param string[] $index
19
     * @throws ResolveException
20
     */
21 30
    public static function create(
22
        string $msg,
23
        string $path,
24
        string $delimiter,
25
        array $index,
26
        ?\Throwable $previous = null
27
    ): ResolveException {
28 30
        $len = ($index !== [] ? \strlen($delimiter) + \strlen(\join($delimiter, $index)) : 0);
29 30
        $invalidPath = $len > 0 ? \substr(\rtrim($path, $delimiter), 0, -1 * $len) : $path;
30
31 30
        return new ResolveException(sprintf($msg, $invalidPath), 0, $previous);
32
    }
33
}
34