StatusCheck   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 22 5
1
<?php
2
3
namespace Darkgoldblade01\StatusCheck\Commands;
4
5
use Darkgoldblade01\StatusCheck\Models\StatusCheck as StatusCheckModel;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Storage;
8
9
class StatusCheck extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'status-check:run';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Run the status check, and cache the results.';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return int
39
     */
40
    public function handle()
41
    {
42
        $last_check = StatusCheckModel::latest()->first();
43
        if(!$last_check) $last_check_id = 0;
44
        if($last_check) $last_check_id = $last_check->group_id + 1;
45
46
        $status_check = [];
47
48
        foreach(config('status-check.checks') AS $class) {
49
            $check = new $class;
50
            $status_check[$check->name] = $check->handle();
51
        }
52
53
        foreach($status_check AS $name => $value) {
54
            StatusCheckModel::create([
55
                'group_id' => $last_check_id,
0 ignored issues
show
Bug introduced by
The variable $last_check_id 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...
56
                'name' => $name,
57
                'results' => $value['results'],
58
                'status' => $value['status'],
59
            ]);
60
        }
61
    }
62
}
63