Completed
Push — master ( 33cd17...4bddcc )
by Arnold
02:40
created

ResolveException::create()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
c 0
b 0
f 0
nc 8
nop 6
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 4
rs 10
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
     * @noinspection PhpTooManyParametersInspection
16
     *
17
     * @param string   $msg
18
     * @param string   $path
19
     * @param string   $delimiter
20
     * @param string[] $index
21
     * @param string   $key
22
     * @throws ResolveException
23
     */
24 30
    public static function create(
25
        string $msg,
26
        string $path,
27
        string $delimiter,
28
        array $index,
29
        ?string $key = null,
30
        ?\Throwable $previous = null
31
    ): ResolveException {
32 30
        $len = ($index !== [] ? \strlen($delimiter) + \strlen(\join($delimiter, $index)) : 0)
33 30
            + ($key !== null ? \strlen($delimiter) + \strlen($key) : 0);
34 30
        $invalidPath = $len > 0 ? \substr(\rtrim($path, $delimiter), 0, -1 * $len) : $path;
35
36 30
        return new ResolveException(sprintf($msg, $invalidPath), 0, $previous);
37
    }
38
}
39