Passed
Push — main ( 2dd0b4...7b6409 )
by Fractal
03:40 queued 12s
created

PropertyHelper::getTypeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
use Fp\Collections\ArrayList;
8
use FRZB\Component\PhpDocReader\Exception\ReaderException;
9
use FRZB\Component\PhpDocReader\Reader\ReaderInterface as PhpDocReader;
10
use JetBrains\PhpStorm\Immutable;
11
12
/** @internal */
13
#[Immutable]
14
final class PropertyHelper
15
{
16
    private function __construct()
17
    {
18
    }
19
20 19
    public static function getMapping(string $className, mixed $value, PhpDocReader $reader): array
21
    {
22
        try {
23 19
            $properties = ArrayList::collect((new \ReflectionClass($className))->getProperties());
24
        } catch (\ReflectionException) {
25
            return [];
26
        }
27
28
        return $properties
29 19
            ->map(static fn (\ReflectionProperty $p) => match (true) {
30 19
                ConstraintsHelper::hasArrayTypeAttribute($p) => [self::getName($p) => ArrayList::range(0, \count($value))->map(fn () => self::getTypeFromAttribute($p))->toArray()],
31 19
                ConstraintsHelper::hasArrayDocBlock($p, $reader) => [self::getName($p) => ArrayList::range(0, \count($value))->map(fn () => self::getTypeFromDocBlock($p, $reader))->toArray()],
32 19
                !ConstraintsHelper::hasArrayTypeAttribute($p) => [self::getName($p) => self::getTypeName($p)],
33
                !ConstraintsHelper::hasArrayDocBlock($p, $reader) => [self::getName($p) => self::getTypeName($p)],
34 19
                default => [SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => []],
35
            })
36 19
            ->reduce(static fn (array $prev, array $next) => [...$prev, ...$next])
37 19
            ->getOrElse([])
38
        ;
39
    }
40
41
    public static function getTypeFromDocBlock(\ReflectionProperty $property, PhpDocReader $reader): ?string
42
    {
43
        try {
44
            return StringHelper::removeNotWordCharacters($reader->getPropertyClass($property));
0 ignored issues
show
Bug introduced by
It seems like $reader->getPropertyClass($property) can also be of type null; however, parameter $value of FRZB\Component\RequestMa...moveNotWordCharacters() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

44
            return StringHelper::removeNotWordCharacters(/** @scrutinizer ignore-type */ $reader->getPropertyClass($property));
Loading history...
45
        } catch (ReaderException) {
46
            return null;
47
        }
48
    }
49
50
    public static function getTypeFromAttribute(\ReflectionProperty $property): ?string
51
    {
52
        return StringHelper::removeNotWordCharacters(ConstraintsHelper::getArrayTypeAttribute($property)?->typeName ?? '');
53
    }
54
55 19
    public static function getName(\ReflectionProperty $property): string
56
    {
57 19
        return SerializerHelper::getSerializedNameAttribute($property)->getSerializedName();
58
    }
59
60 20
    public static function getTypeName(\ReflectionProperty $property): ?string
61
    {
62 20
        return StringHelper::removeNotWordCharacters($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

62
        return StringHelper::removeNotWordCharacters($property->getType()?->/** @scrutinizer ignore-call */ getName() ?? '');
Loading history...
63
    }
64
}
65