Completed
Push — wip-platform ( 4ae41a...a65a23 )
by
unknown
02:30
created

ClassAnalyzer::getInterfaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Tools\Reflection;
13
14
class ClassAnalyzer
15
{
16
    /**
17
     * Returns all parents of a class (parent, parent of parent, parent of parent's parent and so on).
18
     *
19
     * @param ReflectionClass|string $class A ReflectionClass object or a class name
20
     *
21
     * @return array
22
     */
23
    public static function getAncestors($class)
24
    {
25
        $rc = $class instanceof \ReflectionClass ? \ReflectionClass($class->getName()) : new \ReflectionClass($class);
26
        $ancestors = [];
27
        while ($parent = $rc->getParentClass()) {
28
            $ancestors[] = $parent->getName();
29
            $rc = $parent;
30
        }
31
32
        return $ancestors;
33
    }
34
35
    /**
36
     * getTraits.
37
     *
38
     * This static method returns back all traits used by a given class
39
     * recursively
40
     *
41
     * @param ReflectionClass|string $class A ReflectionClass object or a class name
42
     *
43
     * @return array
44
     */
45
    public static function getTraits($class)
46
    {
47
        return self::_getTraits($class);
48
    }
49
50
    /**
51
     * getInterfaces.
52
     *
53
     * @param ReflectionClass|string $class A ReflectionClass object or a class name
54
     *
55
     * @return array
56
     */
57
    public static function getInterfaces($class)
58
    {
59
        $rc = $class instanceof \ReflectionClass ? $class : new \ReflectionClass($class);
60
61
        return array_keys($rc->getInterfaces());
62
    }
63
64
    /**
65
     * hasTraits.
66
     *
67
     * This static method returns back all traits used by a given class
68
     * recursively
69
     *
70
     * @param ReflectionClass|string $class     A ReflectionClass object or a class name
71
     * @param string                 $traitName A string representing an existing trait
72
     *
73
     * @return bool
74
     */
75
    public static function hasTrait($class, $traitName)
76
    {
77
        return in_array($traitName, self::getTraits($class));
78
    }
79
80
    /**
81
     * hasProperty.
82
     *
83
     * This static method says if a class has a property
84
     *
85
     * @param ReflectionClass|string $class        A ReflectionClass object or a class name
86
     * @param string                 $propertyName A string representing an existing property
87
     *
88
     * @return bool
89
     */
90
    public static function hasProperty($class, $propertyName)
91
    {
92
        $rc = $class instanceof \ReflectionClass ? $class : new ReflectionClass($class);
93
94
        if ($rc->hasProperty($propertyName)) {
95
            return true;
96
        }
97
98
        $parentClass = $rc->getParentClass();
99
100
        if (false === $parentClass) {
101
            return false;
102
        }
103
104
        return $this->hasProperty($parentClass, $propertyName);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
105
    }
106
107
    /**
108
     * hasMethod.
109
     *
110
     * This static method says if a class has a method
111
     *
112
     * @param ReflectionClass|string $class      A ReflectionClass object or a class name
113
     * @param string                 $methodName a method name
114
     *
115
     * @return bool
116
     */
117
    public static function hasMethod($class, $methodName)
118
    {
119
        $rc = $class instanceof \ReflectionClass ? $class : new ReflectionClass($class);
120
121
        return $rc->hasMethod($methodName);
122
    }
123
124
    /**
125
     * @param ReflectionClass|string $class  A ReflectionClass object or a class name
126
     * @param array                  $traits An array of traits (strings)
127
     *
128
     * @return array
129
     */
130
    private static function _getTraits($class, array $traits = null)
131
    {
132
        $rc = $class instanceof \ReflectionClass ? $class : new \ReflectionClass($class);
133
        if (is_null($traits)) {
134
            $traits = array();
135
        }
136
137
        // traits being embedded through the current class or the embedded traits
138
        foreach ($rc->getTraits() as $trait) {
139
            $traits = self::_getTraits($trait, $traits); // first the embedded traits that come first...
140
            if (!in_array($trait->name, $traits)) {
141
                $traits[] = $trait->name;
142
            }                    // then the current trait
143
        }
144
145
        // traits embedded by the parent class
146
        if ($rc->getParentClass()) {
147
            $traits = self::_getTraits($rc->getParentClass(), $traits);
148
        }
149
150
        return $traits;
151
    }
152
}
153