This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace yii2mod\enum\helpers; |
||
4 | |||
5 | use BadMethodCallException; |
||
6 | use ReflectionClass; |
||
7 | use UnexpectedValueException; |
||
8 | use Yii; |
||
9 | use yii\helpers\ArrayHelper; |
||
10 | |||
11 | /** |
||
12 | * Class BaseEnum |
||
13 | * |
||
14 | * @package yii2mod\enum\helpers |
||
15 | */ |
||
16 | abstract class BaseEnum |
||
17 | { |
||
18 | /** |
||
19 | * @var string message category |
||
20 | */ |
||
21 | public static $messageCategory = 'app'; |
||
22 | |||
23 | /** |
||
24 | * The cached list of constants by name. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected static $byName = []; |
||
29 | |||
30 | /** |
||
31 | * The cached list of constants by value. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $byValue = []; |
||
36 | |||
37 | /** |
||
38 | * @var array list of properties |
||
39 | */ |
||
40 | protected static $list = []; |
||
41 | |||
42 | /** |
||
43 | * The value managed by this type instance. |
||
44 | * |
||
45 | * @var mixed |
||
46 | */ |
||
47 | protected $value; |
||
48 | |||
49 | /** |
||
50 | * Sets the value that will be managed by this type instance. |
||
51 | * |
||
52 | * @param mixed $value The value to be managed |
||
53 | * |
||
54 | * @throws UnexpectedValueException If the value is not valid |
||
55 | */ |
||
56 | public function __construct($value) |
||
57 | { |
||
58 | if (!static::isValidValue($value)) { |
||
59 | throw new UnexpectedValueException("Value '{$value}' is not part of the enum " . get_called_class()); |
||
60 | } |
||
61 | |||
62 | $this->value = $value; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Creates a new type instance using the name of a value. |
||
67 | * |
||
68 | * @param string $name The name of a value |
||
69 | * |
||
70 | * @throws UnexpectedValueException |
||
71 | * |
||
72 | * @return $this The new type instance |
||
73 | */ |
||
74 | View Code Duplication | public static function createByName($name) |
|
0 ignored issues
–
show
|
|||
75 | { |
||
76 | $constants = static::getConstantsByName(); |
||
77 | |||
78 | if (!array_key_exists($name, $constants)) { |
||
79 | throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class()); |
||
80 | } |
||
81 | |||
82 | return new static($constants[$name]); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * get constant key by value(label) |
||
87 | * |
||
88 | * @param $value |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public static function getValueByName($value) |
||
93 | { |
||
94 | return array_search($value, static::listData()); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Creates a new type instance using the value. |
||
99 | * |
||
100 | * @param mixed $value The value |
||
101 | * |
||
102 | * @throws UnexpectedValueException |
||
103 | * |
||
104 | * @return $this The new type instance |
||
105 | */ |
||
106 | public static function createByValue($value) |
||
107 | { |
||
108 | if (!array_key_exists($value, static::getConstantsByValue())) { |
||
109 | throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class()); |
||
110 | } |
||
111 | |||
112 | return new static($value); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get list data |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public static function listData() |
||
121 | { |
||
122 | return ArrayHelper::getColumn(static::$list, function ($value) { |
||
123 | return Yii::t(static::$messageCategory, $value); |
||
124 | }); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get label by value |
||
129 | * |
||
130 | * @var string value |
||
131 | * |
||
132 | * @return string label |
||
133 | */ |
||
134 | public static function getLabel($value) |
||
135 | { |
||
136 | $list = static::$list; |
||
137 | |||
138 | if (isset($list[$value])) { |
||
139 | return Yii::t(static::$messageCategory, $list[$value]); |
||
140 | } |
||
141 | |||
142 | return null; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns the list of constants (by name) for this type. |
||
147 | * |
||
148 | * @return array The list of constants by name |
||
149 | */ |
||
150 | public static function getConstantsByName() |
||
151 | { |
||
152 | $class = get_called_class(); |
||
153 | |||
154 | if (!array_key_exists($class, static::$byName)) { |
||
155 | $reflection = new ReflectionClass($class); |
||
156 | static::$byName[$class] = $reflection->getConstants(); |
||
157 | } |
||
158 | |||
159 | return static::$byName[$class]; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Returns the list of constants (by value) for this type. |
||
164 | * |
||
165 | * @return array The list of constants by value |
||
166 | */ |
||
167 | public static function getConstantsByValue() |
||
168 | { |
||
169 | $class = get_called_class(); |
||
170 | |||
171 | if (!isset(static::$byValue[$class])) { |
||
172 | static::$byValue[$class] = array_flip(static::getConstantsByName()); |
||
173 | } |
||
174 | |||
175 | return static::$byValue[$class]; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns the name of the value. |
||
180 | * |
||
181 | * @return array|string The name, or names, of the value |
||
182 | */ |
||
183 | public function getName() |
||
184 | { |
||
185 | $constants = static::getConstantsByValue(); |
||
186 | |||
187 | return $constants[$this->value]; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Unwraps the type and returns the raw value. |
||
192 | * |
||
193 | * @return mixed The raw value managed by the type instance |
||
194 | */ |
||
195 | public function getValue() |
||
196 | { |
||
197 | return $this->value; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Checks if a name is valid for this type. |
||
202 | * |
||
203 | * @param string $name The name of the value |
||
204 | * |
||
205 | * @return bool If the name is valid for this type, `true` is returned. |
||
206 | * Otherwise, the name is not valid and `false` is returned |
||
207 | */ |
||
208 | public static function isValidName($name) |
||
209 | { |
||
210 | return array_key_exists($name, static::getConstantsByName()); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Checks if a value is valid for this type. |
||
215 | * |
||
216 | * @param string $value The value |
||
217 | * |
||
218 | * @return bool If the value is valid for this type, `true` is returned. |
||
219 | * Otherwise, the value is not valid and `false` is returned |
||
220 | */ |
||
221 | public static function isValidValue($value) |
||
222 | { |
||
223 | return array_key_exists($value, static::getConstantsByValue()); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant |
||
228 | * |
||
229 | * @param string $name |
||
230 | * @param array $arguments |
||
231 | * |
||
232 | * @return static |
||
233 | * |
||
234 | * @throws BadMethodCallException |
||
235 | */ |
||
236 | View Code Duplication | public static function __callStatic($name, $arguments) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. ![]() |
|||
237 | { |
||
238 | $constants = static::getConstantsByName(); |
||
239 | |||
240 | if (isset($constants[$name])) { |
||
241 | return new static($constants[$name]); |
||
242 | } |
||
243 | |||
244 | throw new BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class()); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function __toString() |
||
251 | { |
||
252 | return (string) $this->value; |
||
253 | } |
||
254 | } |
||
255 |
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.