Test Failed
Push — main ( 5edb73...d2cb63 )
by Fractal
02:48
created

ClassHelper::isNameContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
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
    public static function isNotBuiltinAndExists(string $className): bool
13
    {
14
        return class_exists($className) && !empty((new \ReflectionClass($className))->getNamespaceName());
15
    }
16
17
    public static function getShortName(string $className): string
18
    {
19
        try {
20
            return (new \ReflectionClass($className))->getShortName();
21
        } catch (\ReflectionException) {
22
            return $className;
23
        }
24
    }
25
26
    public static function isNameContains(string $className, string ...$haystack): bool
27
    {
28
        $filter = static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value);
29
30
        return \count(array_filter($haystack, $filter)) > 0;
31
    }
32
33
    public static function getPropertyMapping(string $className): array
34
    {
35
        try {
36
            $properties = (new \ReflectionClass($className))->getProperties();
37
        } catch (\ReflectionException) {
38
            $properties = [];
39
        }
40
41
        $map = static fn (\ReflectionProperty $p): array => [
42
            SerializerHelper::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
            SerializerHelper::getSerializedNameAttribute($p)->getSerializedName() => $p->getType()?->/** @scrutinizer ignore-call */ getName(),
Loading history...
43
        ];
44
45
        return array_merge(...array_map($map, $properties));
46
    }
47
48
    /** @return \ReflectionParameter[] */
49
    public static function getMethodParameters(string $class, string $method): array
50
    {
51
        try {
52
            return (new \ReflectionMethod($class, $method))->getParameters();
53
        } catch (\ReflectionException) {
54
            return [];
55
        }
56
    }
57
}
58