ModelValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 2
b 0
f 0
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateModel() 0 18 5
1
<?php
2
declare(strict_types=1);
3
4
namespace PrinsFrank\PhpStrictModels\Validation;
5
6
use PrinsFrank\PhpStrictModels\Enum\Visibility;
7
use PrinsFrank\PhpStrictModels\Model;
8
use PrinsFrank\PhpStrictModels\Rule\Rule;
9
use Reflection;
10
use ReflectionClass;
11
12
class ModelValidator
13
{
14
    /**
15
     * @param ReflectionClass<Model> $reflectionClass
16
     */
17 3
    public static function validateModel(ReflectionClass $reflectionClass): ValidationResult
18
    {
19 3
        $validationResult = new ValidationResult();
20 3
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
21 2
            if (in_array(Visibility::PUBLIC->value, Reflection::getModifierNames($reflectionProperty->getModifiers()), true) === false) {
22 1
                continue;
23
            }
24
25 1
            foreach ($reflectionProperty->getAttributes() as $reflectionAttribute) {
26 1
                if ($reflectionAttribute->newInstance() instanceof Rule) {
27 1
                    $validationResult->addError('Public properties can\'t have validation rules, but a public property with name "' . $reflectionProperty->getName() . '" does.');
28
29 1
                    break;
30
                }
31
            }
32
        }
33
34 3
        return $validationResult;
35
    }
36
}
37