Passed
Push — main ( 1304a4...1c2c64 )
by Fractal
03:50
created

ClassHelper::isEnum()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
/**
8
 * @internal
9
 */
10
final class ClassHelper
11
{
12 23
    public static function isNotBuiltinAndExists(string $className): bool
13
    {
14 23
        return class_exists($className) && !empty((new \ReflectionClass($className))->getNamespaceName()) && !self::isEnum($className);
15
    }
16
17 23
    public static function isEnum(string $className): bool
18
    {
19 23
        return enum_exists($className);
20
    }
21
22 17
    public static function getShortName(string $className): string
23
    {
24
        try {
25 17
            return (new \ReflectionClass($className))->getShortName();
26 5
        } catch (\ReflectionException) {
27 5
            return $className;
28
        }
29
    }
30
31 11
    public static function isNameContains(string $className, string ...$haystack): bool
32
    {
33 11
        $filter = static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value);
34
35 11
        return \count(array_filter($haystack, $filter)) > 0;
36
    }
37
38 4
    public static function getPropertyMapping(string $className): array
39
    {
40
        try {
41 4
            $properties = (new \ReflectionClass($className))->getProperties();
42 1
        } catch (\ReflectionException) {
43 1
            $properties = [];
44
        }
45
46 4
        $map = static fn (\ReflectionProperty $p): array => match (true) {
47 3
            ConstraintsHelper::hasArrayTypeAttribute($p) => [SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => [ConstraintsHelper::getArrayTypeAttribute($p)->typeName]],
48 3
            default => [SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => $p->getType()?->/** @scrutinizer ignore-call */ getName()],
49
        };
50
51 4
        return array_merge(...array_map($map, $properties));
52
    }
53
54
    /** @return \ReflectionParameter[] */
55 6
    public static function getMethodParameters(string $className, string $classMethod): array
56
    {
57
        try {
58 6
            return (new \ReflectionMethod($className, $classMethod))->getParameters();
59 1
        } catch (\ReflectionException) {
60 1
            return [];
61
        }
62
    }
63
}
64