Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class FunctionSignature extends MagicResolvable implements IFunctionSignature |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $type; |
||
18 | |||
19 | /** |
||
20 | * @var boolean |
||
21 | */ |
||
22 | protected $returnsReference; |
||
23 | |||
24 | /** |
||
25 | * @var int|null |
||
26 | */ |
||
27 | protected $accessModifier; |
||
28 | |||
29 | /** |
||
30 | * @var int|null |
||
31 | */ |
||
32 | protected $polymorphModifier; |
||
33 | |||
34 | /** |
||
35 | * @var bool|null |
||
36 | */ |
||
37 | protected $isStatic; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $name; |
||
43 | |||
44 | /** |
||
45 | * @var O\ParameterExpression[] |
||
46 | */ |
||
47 | protected $parameterExpressions; |
||
48 | |||
49 | /** |
||
50 | * @var string[]|null |
||
51 | */ |
||
52 | protected $scopedVariableNames; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $hash; |
||
58 | |||
59 | protected function __construct( |
||
94 | |||
95 | protected function withResolvedMagic(array $resolvedExpressions) |
||
107 | |||
108 | /** |
||
109 | * Creates a function signature instance from the supplied reflection. |
||
110 | * |
||
111 | * @param \ReflectionFunctionAbstract $reflection |
||
112 | * |
||
113 | * @return self |
||
114 | */ |
||
115 | public static function fromReflection(\ReflectionFunctionAbstract $reflection) |
||
160 | |||
161 | /** |
||
162 | * Creates a function signature with the supplied parameters. |
||
163 | * |
||
164 | * @param boolean $returnsReference |
||
165 | * @param string $name |
||
166 | * @param O\ParameterExpression[] $parameterExpressions |
||
167 | * |
||
168 | * @return self |
||
169 | */ |
||
170 | View Code Duplication | public static function func( |
|
185 | |||
186 | /** |
||
187 | * Creates a closure signature with the supplied parameters. |
||
188 | * |
||
189 | * @param boolean $returnsReference |
||
190 | * @param O\ParameterExpression[] $parameterExpressions |
||
191 | * @param string[] $scopedVariableNames |
||
192 | * |
||
193 | * @return self |
||
194 | */ |
||
195 | View Code Duplication | public static function closure( |
|
210 | |||
211 | /** |
||
212 | * Creates a method signature with the supplied parameters. |
||
213 | * |
||
214 | * @param boolean $returnsReference |
||
215 | * @param int|null $accessModifier |
||
216 | * @param int|null $polymorphModifier |
||
217 | * @param boolean $isStatic |
||
218 | * @param string $name |
||
219 | * @param O\ParameterExpression[] $parameterExpressions |
||
220 | * |
||
221 | * @return self |
||
222 | */ |
||
223 | public static function method( |
||
241 | |||
242 | protected static function getParameterExpressionsFromReflection(\ReflectionFunctionAbstract $reflection) |
||
252 | |||
253 | private static function getParameterExpression(\ReflectionParameter $parameter) |
||
254 | { |
||
255 | $typeHint = null; |
||
256 | |||
257 | if ($parameter->isArray()) { |
||
258 | $typeHint = 'array'; |
||
259 | } elseif ($parameter->isCallable()) { |
||
260 | $typeHint = 'callable'; |
||
261 | } elseif ($parameter->getClass() !== null) { |
||
262 | $typeHint = $parameter->getClass()->getName(); |
||
|
|||
263 | $typeHint = $typeHint[0] === '\\' ? $typeHint : '\\' . $typeHint; |
||
264 | } |
||
265 | |||
266 | return O\Expression::parameter( |
||
267 | $parameter->getName(), |
||
268 | $typeHint, |
||
269 | $parameter->isDefaultValueAvailable() ? O\Expression::value($parameter->getDefaultValue()) : null, |
||
270 | $parameter->isPassedByReference(), |
||
271 | method_exists($parameter, 'isVariadic') && $parameter->isVariadic() |
||
272 | ); |
||
273 | } |
||
274 | |||
275 | public function getType() |
||
279 | |||
280 | public function returnsReference() |
||
284 | |||
285 | public function getAccessModifier() |
||
289 | |||
290 | public function getPolymorphModifier() |
||
294 | |||
295 | public function isStatic() |
||
299 | |||
300 | public function getName() |
||
304 | |||
305 | public function getParameterExpressions() |
||
309 | |||
310 | public function getScopedVariableNames() |
||
314 | |||
315 | public function getHash() |
||
319 | } |
||
320 |