These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace DaveRandom\CallbackValidator; |
||
4 | |||
5 | final class ParameterType extends Type |
||
6 | { |
||
7 | /** |
||
8 | * Contravariant parameters allow implementors to specify a supertype of that which is specified in the prototype |
||
9 | */ |
||
10 | const CONTRAVARIANT = 0x01 << 8; |
||
11 | |||
12 | /** |
||
13 | * Covariant parameters allow implementors to specify a subtype of that which is specified in the prototype |
||
14 | * Usually this isn't a good idea, it's not type-safe, do not use unless you understand what you are doing! |
||
15 | */ |
||
16 | const COVARIANT = 0x02 << 8; |
||
17 | |||
18 | /** |
||
19 | * A variadic parameter accepts zero or more arguments of the specified type |
||
20 | */ |
||
21 | const VARIADIC = 0x04 << 8; |
||
22 | |||
23 | /** |
||
24 | * An optional parameter may be omitted at call time |
||
25 | */ |
||
26 | const OPTIONAL = 0x08 << 8; |
||
27 | |||
28 | /** |
||
29 | * The name of the parameter in the prototype |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $parameterName; |
||
34 | |||
35 | /** |
||
36 | * Whether the parameter accepts multiple values |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | public $isVariadic; |
||
41 | |||
42 | /** |
||
43 | * Whether the parameter value can be omitted at call time# |
||
44 | * |
||
45 | * @var |
||
46 | */ |
||
47 | public $isOptional; |
||
48 | |||
49 | /** |
||
50 | * Create a new ParameterType instance from a \ReflectionParameter instance |
||
51 | * |
||
52 | * @param \ReflectionParameter $reflection |
||
53 | * @param int $flags |
||
54 | * @return ParameterType |
||
55 | */ |
||
56 | public static function createFromReflectionParameter($reflection, $flags = 0) |
||
57 | { |
||
58 | $parameterName = $reflection->getName(); |
||
59 | |||
60 | if ($reflection->isPassedByReference()) { |
||
61 | $flags |= self::REFERENCE; |
||
62 | } |
||
63 | |||
64 | if ($reflection->isVariadic()) { |
||
65 | $flags |= self::VARIADIC; |
||
66 | } |
||
67 | |||
68 | if ($reflection->isOptional()) { |
||
69 | $flags |= self::OPTIONAL; |
||
70 | } |
||
71 | |||
72 | $typeName = null; |
||
73 | $typeReflection = $reflection->getType(); |
||
74 | |||
75 | View Code Duplication | if ($typeReflection !== null) { |
|
1 ignored issue
–
show
|
|||
76 | $typeName = (string)$typeReflection; |
||
77 | |||
78 | if ($typeReflection->allowsNull()) { |
||
79 | $flags |= self::NULLABLE; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return new self($parameterName, $typeName, $flags); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $parameterName |
||
88 | * @param string|null $typeName |
||
89 | * @param int $flags |
||
90 | */ |
||
91 | public function __construct($parameterName, $typeName = null, $flags = self::CONTRAVARIANT) |
||
92 | { |
||
93 | $flags = (int)$flags; |
||
94 | |||
95 | parent::__construct($typeName, $flags, $flags & self::COVARIANT, $flags & self::CONTRAVARIANT); |
||
96 | |||
97 | $this->parameterName = (string)$parameterName; |
||
98 | $this->isOptional = (bool)($flags & self::OPTIONAL); |
||
99 | $this->isVariadic = (bool)($flags & self::VARIADIC); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function __toString() |
||
106 | { |
||
107 | $string = ''; |
||
108 | |||
109 | if ($this->typeName !== null) { |
||
110 | if ($this->isNullable) { |
||
111 | $string .= '?'; |
||
112 | } |
||
113 | |||
114 | $string .= $this->typeName . ' '; |
||
115 | } |
||
116 | |||
117 | if ($this->isByReference) { |
||
118 | $string .= '&'; |
||
119 | } |
||
120 | |||
121 | if ($this->isVariadic) { |
||
122 | $string .= '...'; |
||
123 | } |
||
124 | |||
125 | return $string . '$' . $this->parameterName; |
||
126 | } |
||
127 | } |
||
128 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.