Passed
Push — main ( 337254...02aafc )
by Fractal
02:28
created

ConstraintExtractor::extractConstraints()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Extractor;
6
7
use FRZB\Component\DependencyInjection\Attribute\AsService;
8
use FRZB\Component\RequestMapper\Utils\ClassUtil;
9
use FRZB\Component\RequestMapper\Utils\ConstraintsUtil;
10
use FRZB\Component\RequestMapper\Utils\SerializerUtil;
11
use Symfony\Component\Validator\Constraints\Collection;
12
13
#[AsService]
14
class ConstraintExtractor
15
{
16 15
    public function extract(string $class): ?Collection
17
    {
18
        try {
19 15
            $rClass = new \ReflectionClass($class);
20 1
        } catch (\ReflectionException) {
21 1
            return null;
22
        }
23
24 15
        return ConstraintsUtil::createCollection($this->extractConstraints($rClass));
25
    }
26
27 15
    private function extractConstraints(\ReflectionClass $rClass): array
28
    {
29 15
        $constraints = [];
30
31 15
        if ($parentClass = $rClass->getParentClass()) {
32 1
            $constraints = $this->extractConstraints($parentClass);
33
        }
34
35 15
        foreach ($rClass->getProperties() as $property) {
36 15
            $propertyName = SerializerUtil::getSerializedNameAttribute($property)->getSerializedName();
37 15
            $constraints[$propertyName] = ClassUtil::isNotBuiltinAndExists($propertyClass = $property->getType()?->getName())
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $constraints[$propertyName] = ClassUtil::isNotBuiltinAndExists($propertyClass = $property->getType()?->/** @scrutinizer ignore-call */ getName())
Loading history...
38 1
                ? ConstraintsUtil::createCollection($this->extractConstraints(new \ReflectionClass($propertyClass)))
39 15
                : ConstraintsUtil::fromProperty($property);
40
        }
41
42 15
        return $constraints;
43
    }
44
}
45