Completed
Branch Gutenberg/master (b3a823)
by
unknown
73:52 queued 60:28
created

Mirror::getParameterClassName()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 9
nop 3
dl 16
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\container;
4
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
use ReflectionClass;
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionParameter;
10
use ReflectionProperty;
11
12
/**
13
 * Class Mirror
14
 * Utility class for obtaining details about classes using PHP Reflection.
15
 * To prevent extra processing, all generated reflectors are cached for reuse.
16
 *
17
 * @package EventEspresso\core\services\container
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
class Mirror
22
{
23
24
    /**
25
     * @var ReflectionClass[] $classes
26
     */
27
    private $classes = array();
28
29
    /**
30
     * @var ReflectionMethod[] $constructors
31
     */
32
    private $constructors = array();
33
34
    /**
35
     * @var ReflectionParameter[][] $parameters
36
     */
37
    private $parameters = array();
38
39
    /**
40
     * @var ReflectionParameter[][] $parameters
41
     */
42
    private $parameter_classes = array();
43
44
    /**
45
     * @var ReflectionProperty[][] $properties
46
     */
47
    private $properties = array();
48
49
    /**
50
     * @var ReflectionMethod[][] $methods
51
     */
52
    private $methods = array();
53
54
55
    /**
56
     * @param string $class_name
57
     * @return ReflectionClass
58
     * @throws ReflectionException
59
     * @throws InvalidDataTypeException
60
     */
61
    public function getReflectionClass($class_name)
62
    {
63
        if (! is_string($class_name)) {
64
            throw new InvalidDataTypeException($class_name, '$class_name', 'string (fully qualified class name)');
65
        }
66
        if (! isset($this->classes[ $class_name ])) {
67
            $this->classes[ $class_name ] = new ReflectionClass($class_name);
68
        }
69
        return $this->classes[ $class_name ];
70
    }
71
72
73
    /**
74
     * @param string $class_name
75
     * @return ReflectionMethod
76
     * @throws InvalidDataTypeException
77
     * @throws ReflectionException
78
     */
79
    public function getConstructor($class_name)
80
    {
81
        if (! is_string($class_name)) {
82
            throw new InvalidDataTypeException($class_name, '$class_name', 'string (fully qualified class name)');
83
        }
84
        if (! isset($this->constructors[ $class_name ])) {
85
            $reflection_class                  = $this->getReflectionClass($class_name);
86
            $this->constructors[ $class_name ] = $reflection_class->getConstructor();
87
        }
88
        return $this->constructors[ $class_name ];
89
    }
90
91
92
    /**
93
     * @param ReflectionClass $reflection_class
94
     * @return ReflectionMethod
95
     * @throws InvalidDataTypeException
96
     * @throws ReflectionException
97
     */
98
    public function getConstructorFromReflection(ReflectionClass $reflection_class)
99
    {
100
        return $this->getConstructor($reflection_class->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection_class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
101
    }
102
103
104
    /**
105
     * @param string $class_name
106
     * @return ReflectionParameter[]
107
     * @throws InvalidDataTypeException
108
     * @throws ReflectionException
109
     */
110
    public function getParameters($class_name)
111
    {
112
        if (! isset($this->parameters[ $class_name ])) {
113
            $constructor                     = $this->getConstructor($class_name);
114
            $this->parameters[ $class_name ] = $constructor->getParameters();
115
        }
116
        return $this->parameters[ $class_name ];
117
    }
118
119
120
    /**
121
     * @param ReflectionClass $reflection_class
122
     * @return ReflectionParameter[]
123
     * @throws InvalidDataTypeException
124
     * @throws ReflectionException
125
     */
126
    public function getParametersFromReflection(ReflectionClass $reflection_class)
127
    {
128
        return $this->getParameters($reflection_class->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection_class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
129
    }
130
131
132
    /**
133
     * @param ReflectionMethod $constructor
134
     * @return ReflectionParameter[]
135
     * @throws InvalidDataTypeException
136
     * @throws ReflectionException
137
     */
138
    public function getParametersFromReflectionConstructor(ReflectionMethod $constructor)
139
    {
140
        return $this->getParameters($constructor->getDeclaringClass());
141
    }
142
143
144
    /**
145
     * @param ReflectionParameter $param
146
     * @param string              $class_name
147
     * @param string              $index
148
     * @return string|null
149
     */
150 View Code Duplication
    public function getParameterClassName(ReflectionParameter $param, $class_name, $index)
151
    {
152
        if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_name'])) {
153
            return $this->parameter_classes[ $class_name ][ $index ]['param_class_name'];
154
        }
155
        if (! isset($this->parameter_classes[ $class_name ])) {
156
            $this->parameter_classes[ $class_name ] = array();
157
        }
158
        if (! isset($this->parameter_classes[ $class_name ][ $index ])) {
159
            $this->parameter_classes[ $class_name ][ $index ] = array();
160
        }
161
        $this->parameter_classes[ $class_name ][ $index ]['param_class_name'] = $param->getClass()
162
            ? $param->getClass()->name
163
            : null;
164
        return $this->parameter_classes[ $class_name ][ $index ]['param_class_name'];
165
    }
166
167
168
    /**
169
     * @param ReflectionParameter $param
170
     * @param string              $class_name
171
     * @param string              $index
172
     * @return string|null
173
     */
174 View Code Duplication
    public function getParameterDefaultValue(ReflectionParameter $param, $class_name, $index)
175
    {
176
        if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_default'])) {
177
            return $this->parameter_classes[ $class_name ][ $index ]['param_class_default'];
178
        }
179
        if (! isset($this->parameter_classes[ $class_name ])) {
180
            $this->parameter_classes[ $class_name ] = array();
181
        }
182
        if (! isset($this->parameter_classes[ $class_name ][ $index ])) {
183
            $this->parameter_classes[ $class_name ][ $index ] = array();
184
        }
185
        $this->parameter_classes[ $class_name ][ $index ]['param_class_default'] = $param->isDefaultValueAvailable()
186
            ? $param->getDefaultValue()
187
            : null;
188
        return $this->parameter_classes[ $class_name ][ $index ]['param_class_default'];
189
    }
190
191
192
    /**
193
     * @param string $class_name
194
     * @return ReflectionProperty[]
195
     * @throws InvalidDataTypeException
196
     * @throws ReflectionException
197
     */
198 View Code Duplication
    public function getProperties($class_name)
199
    {
200
        if (! isset($this->properties[ $class_name ])) {
201
            $reflection_class                = $this->getReflectionClass($class_name);
202
            $this->properties[ $class_name ] = $reflection_class->getProperties();
203
        }
204
        return $this->properties[ $class_name ];
205
    }
206
207
208
    /**
209
     * @param ReflectionClass $reflection_class
210
     * @return ReflectionProperty[]
211
     * @throws InvalidDataTypeException
212
     * @throws ReflectionException
213
     */
214
    public function getPropertiesFromReflection(ReflectionClass $reflection_class)
215
    {
216
        return $this->getProperties($reflection_class->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection_class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
217
    }
218
219
220
    /**
221
     * @param string $class_name
222
     * @return ReflectionMethod[]
223
     * @throws InvalidDataTypeException
224
     * @throws ReflectionException
225
     */
226 View Code Duplication
    public function getMethods($class_name)
227
    {
228
        if (! isset($this->methods[ $class_name ])) {
229
            $reflection_class             = $this->getReflectionClass($class_name);
230
            $this->methods[ $class_name ] = $reflection_class->getMethods();
231
        }
232
        return $this->methods[ $class_name ];
233
    }
234
235
236
    /**
237
     * @param ReflectionClass $reflection_class )
238
     * @return ReflectionMethod[]
239
     * @throws InvalidDataTypeException
240
     * @throws ReflectionException
241
     */
242
    public function getMethodsFromReflection(ReflectionClass $reflection_class)
243
    {
244
        return $this->getMethods($reflection_class->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection_class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
245
    }
246
}
247