| Conditions | 7 |
| Paths | 31 |
| Total Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 15 |
| CRAP Score | 12.5143 |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | function model_event_revert($log): array |
||
| 10 | { |
||
| 11 | try { |
||
| 12 | 5 | switch ($log->action) { |
|
| 13 | 5 | case 'update': |
|
| 14 | 5 | $data = json_decode($log->old, true); |
|
| 15 | 5 | $model = $log->subject; |
|
| 16 | 4 | if (! $model) { |
|
| 17 | $response['message'] = trans('ModelEventLogger::model-event-logger.messages.modelNotExist'); |
||
|
|
|||
| 18 | $response['status'] = false; |
||
| 19 | } |
||
| 20 | 4 | $model->update($data); |
|
| 21 | 4 | $response['status'] = true; |
|
| 22 | 4 | break; |
|
| 23 | case 'create': |
||
| 24 | $log->subject()->delete(); |
||
| 25 | $response['status'] = true; |
||
| 26 | break; |
||
| 27 | case 'delete': |
||
| 28 | $log->subject()->insert(json_decode($log->old, true)); |
||
| 29 | $response['status'] = true; |
||
| 30 | break; |
||
| 31 | default: |
||
| 32 | $response['status'] = false; |
||
| 33 | 4 | $response['message'] = trans('ModelEventLogger::model-event-logger.messages.unknownModelEvent'); |
|
| 34 | } |
||
| 35 | 1 | } catch (\Exception $e) { |
|
| 36 | $response['status'] = false; |
||
| 37 | $response['message'] = 'An error occurred. Please check the logs.'; |
||
| 38 | |||
| 39 | \Illuminate\Support\Facades\Log::info($e->getMessage()); |
||
| 40 | 1 | } catch (\Throwable $e) { |
|
| 41 | 1 | $response['status'] = false; |
|
| 42 | 1 | $response['message'] = 'An error occurred. Please check the logs.'; |
|
| 43 | |||
| 44 | 1 | \Illuminate\Support\Facades\Log::info($e->getMessage()); |
|
| 45 | } |
||
| 46 | |||
| 47 | 5 | return $response; |
|
| 48 | } |
||
| 49 | } |
||
| 50 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.