ReflectionFunctionAbstract::getParameter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 5
cts 6
cp 0.8333
cc 3
eloc 5
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace Benoth\StaticReflection\Reflection;
4
5
abstract class ReflectionFunctionAbstract extends Reflection
6
{
7
    use Parts\DocCommentTrait;
8
9
    protected $parameters      = [];
10
    protected $staticVariables = [];
11
    protected $returnsByRef    = false;
12
    protected $isGenerator     = false;
13
14
    /**
15
     * Get the parameters.
16
     *
17
     * @return Benoth\StaticReflection\Reflection\ReflectionParameter[]
18
     */
19 45
    public function getParameters()
20
    {
21 45
        return $this->parameters;
22
    }
23
24
    /**
25
     * Get a parameter by its name.
26
     *
27
     * @param string $parameterSearchedName The parameter name to reflect
28
     *
29
     * @throws \ReflectionException If the parameter does not exist
30
     *
31
     * @return Benoth\StaticReflection\Reflection\ReflectionParameter
32
     */
33 36
    public function getParameter($parameterSearchedName)
34
    {
35 36
        foreach ($this->getParameters() as $parameterName => $parameter) {
36 36
            if ($parameterName === $parameterSearchedName) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $parameterName (integer) and $parameterSearchedName (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
37 36
                return $parameter;
38
            }
39 36
        }
40
41
        throw new \ReflectionException('Method '.$parameterSearchedName.' does not exist');
42
    }
43
44
    /**
45
     * Checks whether the function returns a reference.
46
     *
47
     * @return bool
48
     */
49 6
    public function returnsByRef()
50
    {
51 6
        return $this->returnsByRef === true;
52
    }
53
54
    /**
55
     * Checks whether the function returns a reference.
56
     *
57
     * @return bool
58
     */
59
    public function returnsReference()
60
    {
61
        return $this->returnsByRef();
62
    }
63
64
    /**
65
     * Get the number of parameters that a function defines, both optional and required.
66
     *
67
     * @return int
68
     */
69
    public function getNumberOfParameters()
70
    {
71
        return count($this->getParameters());
72
    }
73
74
    /**
75
     * Get the number of required parameters that a function defines.
76
     *
77
     * @return int
78
     */
79
    public function getNumberOfRequiredParameters()
80
    {
81
        $counter = 0;
82
83
        foreach ($this->getParameters() as $parameter) {
84
            if ($parameter->isRequired()) {
85
                ++$counter;
86
            }
87
        }
88
89
        return $counter;
90
    }
91
92
    /**
93
     * Get the static variables.
94
     *
95
     * @return array An array of static variables, variable name as key, value as value
96
     */
97
    public function getStaticVariables()
98
    {
99
        return $this->staticVariables;
100
    }
101
102
    /**
103
     * Checks whether the reflected function has a return type specified.
104
     *
105
     * @return bool
106
     */
107
    public function hasReturnType()
108
    {
109
        // @todo Implement hasReturnType()
110
        throw new \ReflectionException('Not implemented yet');
111
    }
112
113
    /**
114
     * Checks whether the reflected function is a Closure.
115
     *
116
     * @return bool
117
     */
118
    public function isClosure()
119
    {
120
        return false; // We do not reflect closures at the moment
121
    }
122
123
    /**
124
     * Checks whether the function is deprecated.
125
     *
126
     * @return bool
127
     */
128 3
    public function isDeprecated()
129
    {
130 3
        return false; // Always false for user defined functions/methods
131
    }
132
133
    /**
134
     * Returns whether this function is a generator.
135
     *
136
     * @return bool
137
     */
138 3
    public function isGenerator()
139
    {
140 3
        return $this->isGenerator === true;
141
    }
142
143
    /**
144
     * Checks if class is defined internally by an extension, or the core.
145
     *
146
     * @return bool
147
     */
148
    public function isInternal()
149
    {
150
        return false; // Always false for user-defined classes
151
    }
152
153
    /**
154
     * Checks if class is user defined.
155
     *
156
     * @return bool
157
     */
158
    public function isUserDefined()
159
    {
160
        return true; // Always true for user-defined classes
161
    }
162
163
    /**
164
     * Checks if the function is variadic.
165
     *
166
     * @return bool
167
     */
168
    public function isVariadic()
169
    {
170
        foreach ($this->getParameters() as $parameter) {
171
            if ($parameter->isVariadic()) {
172
                return true;
173
            }
174
        }
175
176
        return false;
177
    }
178
179
    /**
180
     * Returns the scope associated to the closure.
181
     *
182
     * @throws \ReflectionException Always... Can't be implemented.
183
     *
184
     * @return bool
185
     */
186
    public function getClosureScopeClass()
187
    {
188
        throw new \ReflectionException('StaticReflection can\'t get closure');
189
    }
190
191
    /**
192
     * Returns this pointer bound to closure.
193
     *
194
     * @throws \ReflectionException Always... Can't be implemented.
195
     *
196
     * @return bool
197
     */
198
    public function getClosureThis()
199
    {
200
        throw new \ReflectionException('StaticReflection can\'t get closure');
201
    }
202
203
    /**
204
     * Returns a dynamically created closure for the method.
205
     *
206
     * @throws \ReflectionException Always... Can't be implemented.
207
     *
208
     * @return \Closure|null Returns NULL in case of an error.
209
     */
210
    public function getClosure()
211
    {
212
        throw new \ReflectionException('StaticReflection can\'t get closure');
213
    }
214
215
    /**
216
     * Invokes a reflected method.
217
     *
218
     *
219
     * @param object $object    The object to invoke the method on. For static methods, pass null to this parameter.
220
     * @param mixed  $parameter Zero or more parameters to be passed to the method. It accepts a variable number of parameters which are passed to the method.
0 ignored issues
show
Bug introduced by
There is no parameter named $parameter. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
221
     *
222
     * @throws \ReflectionException Always... Can't be implemented.
223
     *
224
     * @return mixed The method result.
225
     */
226
    public function invoke($object)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
227
    {
228
        throw new \ReflectionException('StaticReflection can\'t invoke a method');
229
    }
230
231
    /**
232
     * Invokes the reflected method and pass its arguments as array.
233
     *
234
     * @param object $object The object to invoke the method on. For static methods, pass null to this parameter.
235
     * @param array  $args   The parameters to be passed to the function, as an array.
236
     *
237
     * @throws \ReflectionException Always... Can't be implemented.
238
     *
239
     * @return mixed The method result.
240
     */
241
    public function invokeArgs($object, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
242
    {
243
        throw new \ReflectionException('StaticReflection can\'t invoke a method');
244
    }
245
246 963
    public function addParameter(ReflectionParameter $parameter)
247
    {
248 963
        $this->parameters[$parameter->getShortName()] = $parameter;
249 963
        $parameter->setDeclaringFunction($this);
250
251 963
        return $this;
252
    }
253
254 450
    public function addStaticVariable($name, $value)
255
    {
256 450
        $this->staticVariables[(string) $name] = $value;
257
258 450
        return $this;
259
    }
260
261 1062
    public function setReturnsByRef($returnsByRef)
262
    {
263 1062
        $this->returnsByRef = (bool) $returnsByRef;
264
265 1062
        return $this;
266
    }
267
268 450
    public function setGenerator($isGenerator)
269
    {
270 450
        $this->isGenerator = (bool) $isGenerator;
271
272 450
        return $this;
273
    }
274
}
275