ListChecks   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 33.77 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 26
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 2
A unhealthyChecks() 8 8 1
A healthyChecks() 8 8 1
A tableWithTitle() 0 11 2
A getTableRows() 10 27 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Spatie\ServerMonitor\Commands;
4
5
use Illuminate\Support\Collection;
6
use Spatie\ServerMonitor\Models\Check;
7
8
class ListChecks extends BaseCommand
9
{
10
    protected $signature = 'server-monitor:list-checks
11
                            {--host= : Only show checks for certain host}
12
                            {--check= : Only show certain check type}';
13
14
    protected $description = 'List all checks for host(s)';
15
16
    public function handle()
17
    {
18
        if ($this->determineHostModelClass()::count() === 0) {
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->determineHostModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
19
            return $this->info('There are no hosts configured');
20
        }
21
22
        $this->unhealthyChecks();
23
24
        $this->healthyChecks();
25
    }
26
27 View Code Duplication
    protected function unhealthyChecks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $this->tableWithTitle(
30
            'Unhealthy checks',
31
            ['Host', 'Check', 'Status', 'Message', 'Last checked', 'Next check'],
32
            $this->getTableRows($this->determineCheckModelClass()::unhealthy()->get())
0 ignored issues
show
Bug introduced by
The method unhealthy cannot be called on $this->determineCheckModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
33
        );
34
    }
35
36 View Code Duplication
    protected function healthyChecks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $this->tableWithTitle(
39
            'Healthy checks',
40
            ['Host', 'Check', 'Message', 'Status', 'Last checked', 'Next check'],
41
            $this->getTableRows(self::determineCheckModelClass()::healthy()->get())
0 ignored issues
show
Bug introduced by
The method healthy cannot be called on self::determineCheckModelClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
42
        );
43
    }
44
45
    protected function tableWithTitle(string $title, array $header, array $rows)
46
    {
47
        if (count($rows) === 0) {
48
            return;
49
        }
50
51
        $this->info($title);
52
        $this->info('================');
53
        $this->table($header, $rows);
54
        $this->comment('');
55
    }
56
57
    protected function getTableRows(Collection $checks): array
58
    {
59 View Code Duplication
        if ($hostName = $this->option('host')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $checks = $checks->filter(function (Check $check) use ($hostName) {
61
                return $check->host->name === $hostName;
62
            });
63
        }
64
65 View Code Duplication
        if ($checkType = $this->option('check')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $checks = $checks->filter(function (Check $check) use ($checkType) {
67
                return $check->type === $checkType;
68
            });
69
        }
70
71
        return $checks
72
            ->map(function (Check $check) {
73
                return [
74
                    'name' => $check->host->name,
75
                    'check' => $check->type,
76
                    'last_run_message' => $check->last_run_message,
77
                    'status' => $check->status_as_emoji,
78
                    'last_checked' => $check->getLatestRunDiffAttribute(),
79
                    'next_check' => $check->getNextRunDiffAttribute(),
80
                ];
81
            })
82
            ->toArray();
83
    }
84
}
85