UnexpectedRuleException   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 0
cp 0
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Exception;
6
7
use InvalidArgumentException;
8
use Throwable;
9
10
/**
11 20
 * An exception used by rule handlers to guarantee that passed rule have desired type. Every handler's validation code
12
 * must start with this check. An example for `MyRule` and `MyRuleHandler`:
13 20
 *
14 20
 * ```php
15
 * use Yiisoft\Validator\Exception\UnexpectedRuleException;
16 20
 * use Yiisoft\Validator\Result;
17
 * use Yiisoft\Validator\RuleHandlerInterface;
18
 * use Yiisoft\Validator\ValidationContext;
19
 *
20
 * final class MyRuleHandler implements RuleHandlerInterface
21
 * {
22
 *     public function validate(mixed $value, object $rule, ValidationContext $context): Result
23
 *     {
24
 *         if (!$rule instanceof MyRule) {
25
 *             throw new UnexpectedRuleException(MyRule::class, $rule);
26
 *         }
27
 *
28
 *         // ...
29
 *         $result = new Result();
30
 *         // ...
31
 *
32
 *         return $result;
33
 *     }
34
 * }
35
 * ```
36
 */
37
final class UnexpectedRuleException extends InvalidArgumentException
38
{
39
    public function __construct(
40
        /**
41
         * @var string Expected class name of a rule.
42
         */
43
        string $expectedClassName,
44
        /**
45
         * @var object An actual given object that's not an instance of `$expectedClassName`.
46
         */
47
        object $actualObject,
48
        /**
49
         * @var int The Exception code.
50
         */
51
        int $code = 0,
52
        /**
53
         * @var Throwable|null The previous throwable used for the exception chaining.
54
         */
55
        ?Throwable $previous = null,
56
    ) {
57
        parent::__construct(
58
            sprintf(
59
                'Expected "%s", but "%s" given.',
60
                $expectedClassName,
61
                $actualObject::class
62
            ),
63
            $code,
64
            $previous,
65
        );
66
    }
67
}
68