| Conditions | 4 |
| Paths | 4 |
| Total Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 42 | public function getAttribute($key) |
||
| 43 | { |
||
| 44 | if (! isset($this->enums[$key])) { |
||
| 45 | return parent::getAttribute($key); |
||
| 46 | } |
||
| 47 | |||
| 48 | $enumClass = $this->enums[$key]; |
||
| 49 | |||
| 50 | $storedEnumValue = $this->attributes[$key] ?? null; |
||
| 51 | |||
| 52 | try { |
||
| 53 | $enumObject = forward_static_call_array( |
||
| 54 | $enumClass . '::make', |
||
| 55 | [$storedEnumValue] |
||
| 56 | ); |
||
| 57 | } catch (InvalidValueException $exception) { |
||
| 58 | $mappedEnumValue = array_search($storedEnumValue, $enumClass::$map ?? []); |
||
| 59 | |||
| 60 | if (! $mappedEnumValue) { |
||
| 61 | throw new InvalidValueException($storedEnumValue, $enumClass); |
||
| 62 | } |
||
| 63 | |||
| 64 | $enumObject = forward_static_call_array( |
||
| 65 | $enumClass . '::make', |
||
| 66 | [$mappedEnumValue] |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | return $enumObject; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: