helpers.php ➔ model_event_revert()   B
last analyzed

Complexity

Conditions 7
Paths 31

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 12.5143

Importance

Changes 0
Metric Value
cc 7
nc 31
nop 1
dl 0
loc 40
ccs 15
cts 29
cp 0.5172
crap 12.5143
rs 8.3466
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 10/6/2019
6
 * Time: 1:41 AM.
7
 */
8
if (! function_exists('model_event_revert')) {
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');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
18
                        $response['status'] = false;
19
                    }
20 4
                    $model->update($data);
21 4
                    $response['status'] = true;
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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