Completed
Push — master ( 3e1989...d15db6 )
by Rafael
11:38
created

Utils::getClassProperties()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 20
nc 17
nop 1
1
<?php
2
3
/*
4
 * This file is part of the rafrsr/lib-array2object package.
5
 *
6
 * (c) Rafael SR <https://github.com/rafrsr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rafrsr\LibArray2Object;
13
14
/**
15
 * Class Utils.
16
 */
17
class Utils
18
{
19
    private static $classPropertyes = [];
20
21
    private static $propertyTypes = [];
22
23
    /**
24
     * Get array of class properties including parents private properties.
25
     *
26
     * @param \ReflectionClass|string $class
27
     *
28
     * @return array|\ReflectionProperty[]
29
     *
30
     * @throws \ReflectionException
31
     * @throws \InvalidArgumentException
32
     */
33
    public static function getClassProperties($class)
34
    {
35
        if (is_string($class)) {
36
            if (isset(self::$classPropertyes[$class])) {
37
                return self::$classPropertyes[$class];
38
            }
39
40
            $class = new \ReflectionClass($class);
41
        }
42
43
        if (!$class instanceof \ReflectionClass) {
44
            throw new \InvalidArgumentException('The arguments must be a valid class name or reflection');
45
        }
46
47
        if (isset(self::$classPropertyes[$class->getName()])) {
48
            return self::$classPropertyes[$class->getName()];
49
        }
50
51
        $props = $class->getProperties();
52
        $props_arr = [];
53
        foreach ($props as $prop) {
54
            $f = $prop->getName();
55
56
            $props_arr[$f] = $prop;
57
        }
58
        if ($parentClass = $class->getParentClass()) {
59
            $parent_props_arr = static::getClassProperties($parentClass);//RECURSION
60
            if (count($parent_props_arr) > 0) {
61
                $props_arr = array_merge($parent_props_arr, $props_arr);
62
            }
63
        }
64
65
        self::$classPropertyes[$class->getName()] = $props_arr;
66
67
        return $props_arr;
68
    }
69
70
    /**
71
     * @param \ReflectionProperty $property
72
     *
73
     * @return array
74
     */
75
    public static function getPropertyTypes(\ReflectionProperty $property)
76
    {
77
        $hash = $property->class.'-'.$property->getName();
78
79
        if (isset(self::$propertyTypes[$hash])) {
80
            return self::$propertyTypes[$hash];
81
        }
82
83
        $doc = $property->getDocComment();
84
        preg_match('/@var\s([\w\\\|\[\]]+)/', $doc, $matches);
85
        $types = [];
86
        if (isset($matches[1])) {
87
            $types = explode('|', $matches[1]);
88
        }
89
90
        self::$propertyTypes[$hash] = $types;
91
92
        return $types;
93
    }
94
}
95