Passed
Push — main ( a74a35...66bb0f )
by Fractal
03:01
created

ClassHelper::getPropertyMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ClassHelper::getMethodParameters() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Helper;
6
7
use Fp\Collections\ArrayList;
8
use JetBrains\PhpStorm\Immutable;
9
10
/** @internal */
11
#[Immutable]
12
final class ClassHelper
13
{
14
    private function __construct()
15
    {
16
    }
17
18 23
    public static function isNotBuiltinAndExists(string $className): bool
19
    {
20 23
        return (class_exists($className) || interface_exists($className))
21 23
            && !empty((new \ReflectionClass($className))->getNamespaceName())
22 23
            && !self::isEnum($className)
23
        ;
24
    }
25
26 23
    public static function isEnum(string $className): bool
27
    {
28 23
        return enum_exists($className);
29
    }
30
31 17
    public static function getShortName(string $className): string
32
    {
33
        try {
34 17
            return (new \ReflectionClass($className))->getShortName();
35 5
        } catch (\ReflectionException) {
36 5
            return $className;
37
        }
38
    }
39
40 11
    public static function isNameContains(string $className, string ...$haystack): bool
41
    {
42 11
        return ArrayList::collect($haystack)
43 11
            ->filter(static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value))
44 11
            ->isNonEmpty()
45
        ;
46
    }
47
48
    /** @return \ReflectionParameter[] */
49 6
    public static function getMethodParameters(string $className, string $classMethod): array
50
    {
51
        try {
52 6
            return (new \ReflectionMethod($className, $classMethod))->getParameters();
53 1
        } catch (\ReflectionException) {
54 1
            return [];
55
        }
56
    }
57
58 9
    public static function isArrayHasAllPropertiesFromClass(array $array, string $class): bool
59
    {
60
        try {
61 9
            $rClass = new \ReflectionClass($class);
62 1
        } catch (\ReflectionException) {
63 1
            return false;
64
        }
65
66 8
        foreach ($rClass->getProperties() as $property) {
67 8
            $propertyValue = $array[$property->getName()] ?? $array[StringHelper::toSnakeCase($property->getName())] ?? null;
68
69 8
            if (!$propertyValue) {
70 2
                return false;
71
            }
72
        }
73
74 6
        return true;
75
    }
76
}
77