Completed
Push — master ( a79f71...19c9d7 )
by Tim
12:15
created

ReflectionUtility::getClassTagValues()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 7
nop 2
crap 12
1
<?php
2
3
/**
4
 * Reflection helper.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
/**
11
 * Reflection helper.
12
 */
13
class ReflectionUtility
14
{
15
    /**
16
     * Check if the given class is instantiable.
17
     *
18
     * @param string $className
19
     */
20
    public static function isInstantiable($className): bool
21
    {
22
        $reflectionClass = new \ReflectionClass($className);
23
24
        return (bool)$reflectionClass->isInstantiable();
25
    }
26
27
    /**
28
     * Get the name of the parent class.
29
     *
30
     * @param string $className
31
     *
32
     * @return string
33
     */
34
    public static function getParentClassName($className)
35
    {
36
        $reflectionClass = new \ReflectionClass($className);
37
38
        return $reflectionClass->getParentClass()->getName();
39
    }
40
41
    /**
42
     * Check if the first class is found in the Hierarchy of the second.
43
     */
44
    public static function isClassInOtherClassHierarchy(string $searchClass, string $checkedClass): bool
45
    {
46
        $searchClass = trim($searchClass, '\\');
47
        if (!class_exists($searchClass)) {
48
            return false;
49
        }
50
        $checked = trim($checkedClass, '\\');
51
52
        try {
53
            if ($searchClass === $checked) {
54
                return true;
55
            }
56
            $reflection = new \ReflectionClass($searchClass);
57
            while ($reflection = $reflection->getParentClass()) {
58
                if ($checked === trim($reflection->getName(), '\\')) {
59
                    return true;
60
                }
61
            }
62
63
            return false;
64
        } catch (\Exception $exception) {
65
            return false;
66
        }
67
    }
68
69
    /**
70
     * Get properties of the given class, that are als declared in the given class.
71
     *
72
     * @return array
73
     */
74
    public static function getDeclaringProperties(string $className)
75
    {
76
        $classReflection = new \ReflectionClass($className);
77
        $own = array_filter($classReflection->getProperties(), function ($property) use ($className) {
78
            return trim((string)$property->class, '\\') === trim($className, '\\');
79
        });
80
81
        return array_map(function ($item) {
82
            return (string)$item->name;
83
        }, $own);
84
    }
85
}
86