Passed
Push — main ( d2afe6...18c779 )
by Fractal
02:44
created

ClassUtil::getShortName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Utils;
6
7
/**
8
 * @internal
9
 */
10
final class ClassUtil
11
{
12 23
    public static function isNotBuiltinAndExists(string $className): bool
13
    {
14 23
        return class_exists($className) && !empty((new \ReflectionClass($className))->getNamespaceName());
15
    }
16
17 17
    public static function getShortName(string $className): string
18
    {
19
        try {
20 17
            return (new \ReflectionClass($className))->getShortName();
21 5
        } catch (\ReflectionException) {
22 5
            return $className;
23
        }
24
    }
25
26 11
    public static function isNameContains(string $className, string ...$haystack): bool
27
    {
28 11
        $filter = static fn (string $value): bool => StringUtil::contains(self::getShortName($className), $value);
29
30 11
        return \count(array_filter($haystack, $filter)) > 0;
31
    }
32
33 23
    public static function getPropertyMapping(string $className): array
34
    {
35
        try {
36 23
            $properties = (new \ReflectionClass($className))->getProperties();
37 1
        } catch (\ReflectionException) {
38 1
            $properties = [];
39
        }
40
41 23
        $map = static fn (\ReflectionProperty $p): array => [
42 22
            SerializerUtil::getSerializedNameAttribute($p)->getSerializedName() => $p->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

42
            SerializerUtil::getSerializedNameAttribute($p)->getSerializedName() => $p->getType()?->/** @scrutinizer ignore-call */ getName(),
Loading history...
43
        ];
44
45 23
        return array_merge(...array_map($map, $properties));
46
    }
47
48
    /** @return \ReflectionParameter[] */
49 6
    public static function getMethodParameters(string $class, string $method): array
50
    {
51
        try {
52 6
            return (new \ReflectionMethod($class, $method))->getParameters();
53 1
        } catch (\ReflectionException) {
54 1
            return [];
55
        }
56
    }
57
}
58