Passed
Pull Request — master (#110)
by Dmitriy
03:29
created

ClassExistsHandler::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 10
cc 3
nc 3
nop 3
crap 3.2098
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Validator;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleHandlerInterface;
10
use Yiisoft\Validator\ValidationContext;
11
12
/**
13
 * An inline validator that checks if the attribute value refers to an existing class name.
14
 */
15
final class ClassExistsHandler implements RuleHandlerInterface
16
{
17 4
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
18
    {
19 4
        if (!$rule instanceof ClassExistsRule) {
20
            throw new UnexpectedRuleException(ClassExistsRule::class, $rule);
21
        }
22
23 4
        $result = new Result();
24 4
        if (!class_exists($value)) {
25
            $result->addError("Class '$value' does not exist or has syntax error.");
26
        }
27
28 4
        return $result;
29
    }
30
}
31