| Total Complexity | 10 | 
| Total Lines | 66 | 
| Duplicated Lines | 0 % | 
| Coverage | 100% | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
| 1 | <?php | ||
| 7 | trait HelpsLoopFunctions | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * Assign the value to the property or rescue. | ||
| 11 | * | ||
| 12 | * @param int|string $key | ||
| 13 | * @param mixed $value | ||
| 14 | * @param mixed|null $rescue | ||
| 15 | * | ||
| 16 | * @return void | ||
| 17 | * @throws \ReflectionException | ||
| 18 | */ | ||
| 19 | 16 | private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void | |
| 20 |     { | ||
| 21 | 16 |         if ($this->canAssignValue($key)) { | |
| 22 | 16 | rescue( | |
| 23 | 16 |                 fn () => $this->{$key} = $value, | |
| 24 | $rescue, | ||
| 25 | 16 |                 config('loop-functions.log') ?? false | |
| 26 | ); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @return array | ||
| 32 | */ | ||
| 33 | 16 | private function ignoredPropertyNames(): array | |
| 34 |     { | ||
| 35 | 16 |         return config('loop-functions.ignore_keys', ['id', 'password']); | |
| 36 | } | ||
| 37 | |||
| 38 | /** | ||
| 39 | * @param int|string $key | ||
| 40 | * | ||
| 41 | * @return bool | ||
| 42 | * @throws \ReflectionException | ||
| 43 | */ | ||
| 44 | 16 | private function canAssignValue(int|string $key): bool | |
| 45 |     { | ||
| 46 | 16 | return is_string($key) | |
| 47 | 16 | && property_exists($this, $key) | |
| 48 | 16 |             && (empty($this->{$key}) || $this->hasDefaultValue($key)); | |
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * @param int|string $key | ||
| 53 | * | ||
| 54 | * @return bool | ||
| 55 | * @throws \ReflectionException | ||
| 56 | */ | ||
| 57 | 3 | private function hasDefaultValue(int|string $key): bool | |
| 58 |     { | ||
| 59 | 3 | $default = (new \ReflectionProperty($this, $key)) | |
| 60 | 3 | ->getDefaultValue(); | |
| 61 | |||
| 62 | 3 | return ! empty($default); | |
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @param mixed $value | ||
| 67 | * | ||
| 68 | * @return bool | ||
| 69 | */ | ||
| 70 | 7 | private function canWalkRecursively(mixed $value): bool | |
| 73 | } | ||
| 74 | } | ||
| 75 |