Passed
Push — master ( c81bf2...c3afe7 )
by Aleksei
09:08 queued 01:17
created

ValidationException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Exception\Resolver;
6
7
use Spiral\Core\Exception\Traits\ClosureRendererTrait;
8
9
abstract class ValidationException extends ResolvingException
10
{
11
    use ClosureRendererTrait;
0 ignored issues
show
Bug introduced by
The trait Spiral\Core\Exception\Traits\ClosureRendererTrait requires the property $class which is not provided by Spiral\Core\Exception\Resolver\ValidationException.
Loading history...
12
13 24
    final private function __construct(
14
        protected readonly \ReflectionFunctionAbstract $reflection,
15
        protected readonly string $parameter,
16
        ?string $message = null,
17
    ) {
18 24
        $message ??= $this->getValidationMessage($reflection, $parameter);
19 24
        parent::__construct($this->renderFunctionAndParameter($reflection, $message));
20
    }
21
22 24
    public static function createWithParam(\ReflectionFunctionAbstract $reflection, string $parameter): static
23
    {
24 24
        return new static($reflection, $parameter);
25
    }
26
27 10
    public function getParameter(): string
28
    {
29 10
        return $this->parameter;
30
    }
31
32 7
    protected static function createStatic(string $message, ?\Throwable $previous): static
33
    {
34 7
        $previous instanceof self or throw new \InvalidArgumentException(
35 7
            \sprintf('Previous exception must be an instance of %s.', self::class),
36 7
        );
37 7
        return new static($previous->reflection, $previous->parameter, $message);
0 ignored issues
show
Bug introduced by
Accessing reflection on the interface Throwable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing parameter on the interface Throwable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
    }
39
40
    abstract protected function getValidationMessage(
41
        \ReflectionFunctionAbstract $reflection,
42
        string $parameter,
43
    ): string;
44
}
45