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

ConstraintExtractor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 9 2
A extractConstraints() 0 16 4
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