Completed
Push — wip-subtree ( 04b705...405079 )
by
unknown
12:15
created

ClassAnalyzer::hasTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\Tools\Reflection;
14
15
class ClassAnalyzer
16
{
17
    /**
18
     * Returns all parents of a class (parent, parent of parent, parent of parent's parent and so on).
19
     *
20
     * @param ReflectionClass|string $class A ReflectionClass object or a class name
21
     *
22
     * @return array
23
     */
24
    public static function getAncestors($class)
25
    {
26
        $rc = $class instanceof \ReflectionClass ? \ReflectionClass($class->getName()) : new \ReflectionClass($class);
27
        $ancestors = [];
28
        while ($parent = $rc->getParentClass()) {
29
            $ancestors[] = $parent->getName();
30
            $rc = $parent;
31
        }
32
33
        return $ancestors;
34
    }
35
36
    /**
37
     * getTraits.
38
     *
39
     * This static method returns back all traits used by a given class
40
     * recursively
41
     *
42
     * @param ReflectionClass|string $class A ReflectionClass object or a class name
43
     *
44
     * @return array
45
     */
46
    public static function getTraits($class)
47
    {
48
        return self::_getTraits($class);
49
    }
50
51
    /**
52
     * hasTraits.
53
     *
54
     * This static method returns back all traits used by a given class
55
     * recursively
56
     *
57
     * @param ReflectionClass|string $class     A ReflectionClass object or a class name
58
     * @param string                 $traitName A string representing an existing trait
59
     *
60
     * @return bool
61
     */
62
    public static function hasTrait($class, $traitName)
63
    {
64
        return in_array($traitName, self::getTraits($class));
65
    }
66
67
    /**
68
     * hasProperty.
69
     *
70
     * This static method says if a class has a property
71
     *
72
     * @param ReflectionClass|string $class        A ReflectionClass object or a class name
73
     * @param string                 $propertyName A string representing an existing property
74
     *
75
     * @return bool
76
     */
77
    public static function hasProperty($class, $propertyName)
78
    {
79
        $rc = $class instanceof \ReflectionClass ? $class : new ReflectionClass($class);
80
81
        if ($rc->hasProperty($propertyName)) {
82
            return true;
83
        }
84
85
        $parentClass = $rc->getParentClass();
86
87
        if (false === $parentClass) {
88
            return false;
89
        }
90
91
        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...
92
    }
93
94
    /**
95
     * hasMethod.
96
     *
97
     * This static method says if a class has a method
98
     *
99
     * @param ReflectionClass|string $class      A ReflectionClass object or a class name
100
     * @param string                 $methodName a method name
101
     *
102
     * @return bool
103
     */
104
    public static function hasMethod($class, $methodName)
105
    {
106
        $rc = $class instanceof \ReflectionClass ? $class : new ReflectionClass($class);
107
108
        return $rc->hasMethod($methodName);
109
    }
110
111
    /**
112
     * @param ReflectionClass|string $class  A ReflectionClass object or a class name
113
     * @param array                  $traits An array of traits (strings)
114
     *
115
     * @return array
116
     */
117
    private static function _getTraits($class, array $traits = null)
118
    {
119
        $rc = $class instanceof \ReflectionClass ? $class : new \ReflectionClass($class);
120
        if (is_null($traits)) {
121
            $traits = array();
122
        }
123
124
        // traits being embedded through the current class or the embedded traits
125
        foreach ($rc->getTraits() as $trait) {
126
            $traits = self::_getTraits($trait, $traits); // first the embedded traits that come first...
127
            if (!in_array($trait->name, $traits)) {
128
                $traits[] = $trait->name;
129
            }                    // then the current trait
130
        }
131
132
        // traits embedded by the parent class
133
        if ($rc->getParentClass()) {
134
            $traits = self::_getTraits($rc->getParentClass(), $traits);
135
        }
136
137
        return $traits;
138
    }
139
}
140