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

ClassExistsHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 3
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