GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 95a376...21b183 )
by Freek
02:07
created

SupportsUptimeCheck::shouldCheckUptime()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 0
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models\Traits;
4
5
use Spatie\UptimeMonitor\Events\UptimeCheckFailed;
6
use Carbon\Carbon;
7
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered;
8
use Spatie\UptimeMonitor\Events\UptimeCheckSucceeded;
9
use Spatie\UptimeMonitor\Models\Monitor;
10
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
11
12
trait SupportsUptimeCheck
13
{
14
    public static function bootSupportsUptimeCheck()
15
    {
16
        static::saving(function (Monitor $monitor) {
17
            if (is_null($monitor->uptime_status_last_change_date)) {
18
                $monitor->uptime_status_last_change_date = Carbon::now();
19
20
                return;
21
            }
22
23
            if ($monitor->getOriginal('uptime_status') != $monitor->uptime_status) {
24
                $monitor->uptime_status_last_change_date = Carbon::now();
25
            }
26
        });
27
    }
28
29
    public function shouldCheckUptime() : bool
30
    {
31
        if (!$this->uptime_check_enabled) {
0 ignored issues
show
Bug introduced by
The property uptime_check_enabled does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
            return false;
33
        }
34
35
        if ($this->uptime_status == UptimeStatus::NOT_YET_CHECKED) {
0 ignored issues
show
Bug introduced by
The property uptime_status does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            return true;
37
        }
38
39
        if ($this->uptime_status == UptimeStatus::DOWN) {
40
            return true;
41
        }
42
43
        if (is_null($this->uptime_last_check_date)) {
0 ignored issues
show
Bug introduced by
The property uptime_last_check_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
            return true;
45
        }
46
47
        return $this->uptime_last_check_date->diffInMinutes() >= $this->uptime_check_interval_in_minutes;
0 ignored issues
show
Bug introduced by
The property uptime_check_interval_in_minutes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
    }
49
50
    public function uptimeRequestSucceeded($responseHtml)
51
    {
52
        if (!str_contains($responseHtml, $this->look_for_string)) {
53
            $this->uptimeCheckFailed("String `{$this->look_for_string}` was not found on the response.");
0 ignored issues
show
Bug introduced by
The property look_for_string does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        }
55
56
        $this->uptimeCheckSucceeded();
57
    }
58
59
    public function uptimeRequestFailed(string $reason)
60
    {
61
        $this->uptimeCheckFailed($reason);
62
    }
63
64
    public function uptimeCheckSucceeded()
65
    {
66
        $this->uptime_status = UptimeStatus::UP;
67
        $this->uptime_check_failure_reason = '';
0 ignored issues
show
Bug introduced by
The property uptime_check_failure_reason does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
69
        $wasFailing = !is_null($this->uptime_check_failed_event_fired_on_date);
0 ignored issues
show
Bug introduced by
The property uptime_check_failed_event_fired_on_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
71
        $this->uptime_check_times_failed_in_a_row = 0;
0 ignored issues
show
Bug introduced by
The property uptime_check_times_failed_in_a_row does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
        $this->uptime_last_check_date = Carbon::now();
73
        $this->uptime_check_failed_event_fired_on_date = null;
74
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
76
        if ($wasFailing) {
77
            $this->fireRecoveredEvent();
78
            return;
79
        }
80
81
        event(new UptimeCheckSucceeded($this));
82
    }
83
84
    public function uptimeCheckFailed(string $reason)
85
    {
86
        $this->uptime_status = UptimeStatus::DOWN;
87
        $this->uptime_check_times_failed_in_a_row++;
88
        $this->uptime_last_check_date = Carbon::now();
89
        $this->uptime_check_failure_reason = $reason;
90
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
91
92
        if ($this->shouldFireUptimeCheckFailedEvent()) {
93
            $this->uptime_check_failed_event_fired_on_date = Carbon::now();
94
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95
96
            event(new UptimeCheckFailed($this));
97
        }
98
    }
99
100
    protected function fireRecoveredEvent()
101
    {
102
        if ($this->uptime_status_last_change_date) {
0 ignored issues
show
Bug introduced by
The property uptime_status_last_change_date does not seem to exist. Did you mean uptime_status?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
103
104
            $uptimeCheckStartedFailingOnDate = clone $this->uptime_status_last_change_date;
0 ignored issues
show
Bug introduced by
The property uptime_status_last_change_date does not seem to exist. Did you mean uptime_status?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
        }
106
107
        event(new UptimeCheckRecovered($this, $uptimeCheckStartedFailingOnDate));
0 ignored issues
show
Bug introduced by
The variable $uptimeCheckStartedFailingOnDate 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...
108
    }
109
110
    protected function shouldFireUptimeCheckFailedEvent(): bool
111
    {
112
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_monitor_failed_event_after_consecutive_failures')) {
113
            return true;
114
        }
115
116
        if (is_null($this->uptime_check_failed_event_fired_on_date)) {
117
            return false;
118
        }
119
120
        if (config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes') === 0) {
121
            return false;
122
        }
123
124
        if ($this->uptime_check_failed_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes')) {
125
            return true;
126
        }
127
128
        return false;
129
    }
130
}
131