Passed
Push — latest ( adbbf9...7e49ae )
by Colin
24:26 queued 22:27
created

InvalidConfigurationException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
wmc 7
eloc 11
c 0
b 0
f 0
dl 0
loc 46
ccs 4
cts 15
cp 0.2667
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromValidation() 0 3 1
A getDebugValue() 0 7 2
A forConfigOption() 0 8 2
A forParameter() 0 3 1
A missingOption() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Exception;
15
16
use Nette\Schema\ValidationException;
17
18
final class InvalidConfigurationException extends \UnexpectedValueException
19
{
20
    /**
21
     * @param string  $option      Name/path of the option
22
     * @param mixed   $valueGiven  The invalid option that was provided
23
     * @param ?string $description Additional text describing the issue (optional)
24
     */
25
    public static function forConfigOption(string $option, $valueGiven, ?string $description = null): self
26
    {
27
        $message = \sprintf('Invalid config option for "%s": %s', $option, self::getDebugValue($valueGiven));
28
        if ($description !== null) {
29
            $message .= \sprintf(' (%s)', $description);
30
        }
31
32
        return new self($message);
33
    }
34
35
    /**
36
     * @param string $option     Description of the option
37
     * @param mixed  $valueGiven The invalid option that was provided
38
     */
39
    public static function forParameter(string $option, $valueGiven): self
40
    {
41
        return new self(\sprintf('Invalid %s: %s', $option, self::getDebugValue($valueGiven)));
42
    }
43
44 33
    public static function fromValidation(ValidationException $ex): self
45
    {
46 33
        return new self($ex->getMessage(), 0, $ex);
47
    }
48
49 6
    public static function missingOption(string $option): self
50
    {
51 6
        return new self(\sprintf('Config option "%s" does not exist', $option));
52
    }
53
54
    /**
55
     * @param mixed $value
56
     */
57
    private static function getDebugValue($value): string
58
    {
59
        if (\is_object($value)) {
60
            return \get_class($value);
61
        }
62
63
        return \print_r($value, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return print_r($value, true) could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
}
66