Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

Utils::getPropertyTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object;
11
12
/**
13
 * Class Utils
14
 */
15
class Utils
16
{
17
    /**
18
     * Get array of class properties including parents private properties
19
     *
20
     * @param \ReflectionClass $refClass
21
     *
22
     * @return array|\ReflectionProperty[]
23
     */
24
    public static function getClassProperties(\ReflectionClass $refClass)
25
    {
26
        $props = $refClass->getProperties();
27
        $props_arr = [];
28
        foreach ($props as $prop) {
29
            $f = $prop->getName();
30
31
            $props_arr[$f] = $prop;
32
        }
33
        if ($parentClass = $refClass->getParentClass()) {
34
            $parent_props_arr = static::getClassProperties($parentClass);//RECURSION
35
            if (count($parent_props_arr) > 0) {
36
                $props_arr = array_merge($parent_props_arr, $props_arr);
37
            }
38
        }
39
40
        return $props_arr;
41
    }
42
43
    /**
44
     * @param \ReflectionProperty $property
45
     *
46
     * @return array
47
     */
48
    public static function getPropertyTypes(\ReflectionProperty $property)
49
    {
50
        $doc = $property->getDocComment();
51
        preg_match('/@var\s([\w\\\|\[\]]+)/', $doc, $matches);
52
        $types = [];
53
        if (isset($matches[1])) {
54
            $types = explode('|', $matches[1]);
55
        }
56
57
        return $types;
58
    }
59
}