Passed
Pull Request — main (#3)
by Viktor
19:55
created

ModelValidator::validateModel()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 5
nop 1
crap 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