Completed
Push — master ( cdb5aa...909635 )
by Bernhard
10s
created

ReflectionParameter::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * AppserverIo\Lang\Reflection\ReflectionParameter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/lang
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Lang\Reflection;
22
23
use AppserverIo\Lang\Objct;
24
25
/**
26
 * A wrapper instance for a reflection parameter.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/lang
32
 * @link      http://www.appserver.io
33
 */
34
class ReflectionParameter extends Objct implements ParameterInterface, \Serializable
35
{
36
37
    /**
38
     * The name of the class the parameter belongs to.
39
     *
40
     * @var string
41
     */
42
    protected $className;
43
44
    /**
45
     * The name of the method the parameter belongs to.
46
     *
47
     * @var string
48
     */
49
    protected $methodName;
50
51
    /**
52
     * The parameter name.
53
     *
54
     * @var string
55
     */
56
    protected $parameterName;
57
58
    /**
59
     * Initializes the reflection parameter with the passed data.
60
     *
61
     * @param string $className     The name of the class the parameter belongs to
62
     * @param string $methodName    The name of the method the parameter belongs to
63
     * @param string $parameterName The parameter name
64
     */
65 5
    public function __construct($className, $methodName, $parameterName)
66
    {
67
        // initialize property default values here, as declarative default values may break thread safety,
68
        // when utilizing static and non-static access on class methods within same thread context!
69 5
        $this->className = '';
70 5
        $this->methodName = '';
71 5
        $this->parameterName = '';
72
73 5
        $this->className = $className;
74 5
        $this->methodName = $methodName;
75 5
        $this->parameterName = $parameterName;
76 5
    }
77
78
    /**
79
     * This method returns the class name as
80
     * a string.
81
     *
82
     * @return string
83
     */
84
    public static function __getClass()
85
    {
86
        return __CLASS__;
87
    }
88
89
    /**
90
     * Returns name of the class the parameter belongs to.
91
     *
92
     * @return string The class name
93
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::getClassName()
94
     */
95 2
    public function getClassName()
96
    {
97 2
        return $this->className;
98
    }
99
100
    /**
101
     * Returns name of the method the parameter belongs to.
102
     *
103
     * @return string The method name
104
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::getMethodName()
105
     */
106 2
    public function getMethodName()
107
    {
108 2
        return $this->methodName;
109
    }
110
111
    /**
112
     * Returns the parameter position.
113
     *
114
     * @return string The parameter position
115
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::getParameterName()
116
     */
117 3
    public function getParameterName()
118
    {
119 3
        return $this->parameterName;
120
    }
121
122
    /**
123
     * Returns the parameters position.
124
     *
125
     * @return string The parameters position
126
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::getPosition()
127
     */
128 2
    public function getPosition()
129
    {
130 2
        return $this->toPhpReflectionParameter()->getPosition();
0 ignored issues
show
Bug introduced by
The method getPosition() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        return $this->toPhpReflectionParameter()->/** @scrutinizer ignore-call */ getPosition();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
    }
132
133
    /**
134
     * Returns the parameters class name.
135
     *
136
     * @return string The parameters class name
137
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::getType()
138
     */
139
    public function getType()
140
    {
141
        return $this->toPhpReflectionParameter()->getClass()->getName();
0 ignored issues
show
Bug introduced by
The method getClass() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        return $this->toPhpReflectionParameter()->/** @scrutinizer ignore-call */ getClass()->getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
    }
143
144
    /**
145
     * Serializes the timeout method and returns a string representation.
146
     *
147
     * @return string The serialized string representation of the instance
148
     * @see \Serializable::serialize()
149
     */
150 1
    public function serialize()
151
    {
152 1
        return serialize(get_object_vars($this));
153
    }
154
155
    /**
156
     * Restores the instance with the serialized data of the passed string.
157
     *
158
     * @param string $data The serialized property representation
159
     *
160
     * @return void
161
     * @see \Serializable::unserialize()
162
     */
163 1
    public function unserialize($data)
164
    {
165 1
        foreach (unserialize($data) as $propertyName => $propertyValue) {
166 1
            $this->$propertyName = $propertyValue;
167
        }
168 1
    }
169
170
    /**
171
     * Returns a PHP reflection parameter representation of this instance.
172
     *
173
     * @return \ReflectionProperty The PHP reflection parameter instance
174
     * @see \AppserverIo\Lang\Reflection\ParameterInterface::toPhpReflectionParameter()
175
     */
176 2
    public function toPhpReflectionParameter()
177
    {
178 2
        return new \ReflectionParameter(array($this->getClassName(), $this->getMethodName()), $this->getParameterName());
0 ignored issues
show
Bug introduced by
array($this->getClassNam...$this->getMethodName()) of type array<integer,string> is incompatible with the type string expected by parameter $function of ReflectionParameter::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

178
        return new \ReflectionParameter(/** @scrutinizer ignore-type */ array($this->getClassName(), $this->getMethodName()), $this->getParameterName());
Loading history...
Bug Best Practice introduced by
The expression return new ReflectionPar...is->getParameterName()) returns the type ReflectionParameter which is incompatible with the documented return type ReflectionProperty.
Loading history...
179
    }
180
181
    /**
182
     * Returns an array of reflection parameter instances from the passed reflection method.
183
     *
184
     * @param \AppserverIo\Lang\Reflection\ReflectionMethod $reflectionMethod The reflection method to return the parameters for
185
     *
186
     * @return array An array with ReflectionParameter instances
187
     */
188 2
    public static function fromReflectionMethod(ReflectionMethod $reflectionMethod)
189
    {
190
191
        // initialize the array for the reflection parameters
192 2
        $reflectionParameters = array();
193
194
        // load the reflection parameters and initialize the array with the reflection parameters
195 2
        $phpReflectionMethod = $reflectionMethod->toPhpReflectionMethod();
196 2
        foreach ($phpReflectionMethod->getParameters() as $phpReflectionParameter) {
197 2
            $reflectionParameters[$phpReflectionParameter->getName()] = ReflectionParameter::fromPhpReflectionParameter($phpReflectionParameter);
198
        }
199
200
        // return the array with the initialized reflection parameters
201 2
        return $reflectionParameters;
202
    }
203
204
    /**
205
     * Creates a new reflection parameter instance from the passed PHP reflection parameter.
206
     *
207
     * @param \ReflectionParameter $reflectionParameter The reflection parameter to load the data from
208
     *
209
     * @return \AppserverIo\Lang\Reflection\ReflectionParameter The instance
210
     */
211 2
    public static function fromPhpReflectionParameter(\ReflectionParameter $reflectionParameter)
212
    {
213
214
        // load class, method and parameter name from the reflection parameter
215 2
        $className = $reflectionParameter->getDeclaringClass()->getName();
216 2
        $methodName = $reflectionParameter->getDeclaringFunction()->getName();
217 2
        $parameterName = $reflectionParameter->getName();
218
219
        // initialize and return the parameter instance
220 2
        return new ReflectionParameter($className, $methodName, $parameterName);
221
    }
222
}
223