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 ( 2fe938...e8e1b6 )
by Freek
07:44
created

src/Models/Traits/SupportsUptimeCheck.php (13 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\UptimeMonitor\Models\Traits;
4
5
use Carbon\Carbon;
6
use Psr\Http\Message\ResponseInterface;
7
use Spatie\UptimeMonitor\Helpers\Period;
8
use Spatie\UptimeMonitor\Models\Monitor;
9
use Spatie\UptimeMonitor\Events\UptimeCheckFailed;
10
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
11
use Spatie\UptimeMonitor\Events\UptimeCheckRecovered;
12
use Spatie\UptimeMonitor\Events\UptimeCheckSucceeded;
13
use Spatie\UptimeMonitor\Helpers\UptimeResponseCheckers\UptimeResponseChecker;
14
15
trait SupportsUptimeCheck
16
{
17
    public static function bootSupportsUptimeCheck()
18
    {
19
        static::saving(function (Monitor $monitor) {
20
            if (is_null($monitor->uptime_status_last_change_date)) {
21
                $monitor->uptime_status_last_change_date = Carbon::now();
22
23
                return;
24
            }
25
26
            if ($monitor->getOriginal('uptime_status') != $monitor->uptime_status) {
27
                $monitor->uptime_status_last_change_date = Carbon::now();
28
            }
29
        });
30
    }
31
32
    public function shouldCheckUptime() : bool
33
    {
34
        if (! $this->uptime_check_enabled) {
0 ignored issues
show
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...
35
            return false;
36
        }
37
38
        if ($this->uptime_status == UptimeStatus::NOT_YET_CHECKED) {
0 ignored issues
show
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...
39
            return true;
40
        }
41
42
        if ($this->uptime_status == UptimeStatus::DOWN) {
43
            return true;
44
        }
45
46
        if (is_null($this->uptime_last_check_date)) {
0 ignored issues
show
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...
47
            return true;
48
        }
49
50
        return $this->uptime_last_check_date->diffInMinutes() >= $this->uptime_check_interval_in_minutes;
0 ignored issues
show
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...
51
    }
52
53
    public function uptimeRequestSucceeded(ResponseInterface $response)
54
    {
55
        $uptimeResponseChecker = app(UptimeResponseChecker::class);
56
57
        if (! $uptimeResponseChecker->isValidResponse($response, $this)) {
58
            $this->uptimeCheckFailed($uptimeResponseChecker->getFailureReason($response, $this));
59
60
            return;
61
        }
62
63
        $this->uptimeCheckSucceeded();
64
    }
65
66
    public function uptimeRequestFailed(string $reason)
67
    {
68
        $this->uptimeCheckFailed($reason);
69
    }
70
71
    public function uptimeCheckSucceeded()
72
    {
73
        $this->uptime_status = UptimeStatus::UP;
74
        $this->uptime_check_failure_reason = '';
0 ignored issues
show
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...
75
76
        $wasFailing = ! is_null($this->uptime_check_failed_event_fired_on_date);
0 ignored issues
show
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...
77
        $lastStatusChangeDate = $this->uptime_status_last_change_date ? clone $this->uptime_status_last_change_date : null;
0 ignored issues
show
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...
78
79
        $this->uptime_check_times_failed_in_a_row = 0;
0 ignored issues
show
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...
80
        $this->uptime_last_check_date = Carbon::now();
81
        $this->uptime_check_failed_event_fired_on_date = null;
82
        $this->save();
0 ignored issues
show
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...
83
84
        if ($wasFailing) {
85
            $downtimePeriod = new Period($lastStatusChangeDate, $this->uptime_last_check_date);
0 ignored issues
show
$lastStatusChangeDate is of type object|null, but the function expects a object<Carbon\Carbon>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
87
            event(new UptimeCheckRecovered($this, $downtimePeriod));
88
89
            return;
90
        }
91
92
        event(new UptimeCheckSucceeded($this));
93
    }
94
95
    public function uptimeCheckFailed(string $reason)
96
    {
97
        $this->uptime_status = UptimeStatus::DOWN;
98
        $this->uptime_check_times_failed_in_a_row++;
99
        $this->uptime_last_check_date = Carbon::now();
100
        $this->uptime_check_failure_reason = $reason;
101
        $this->save();
0 ignored issues
show
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...
102
103
        if ($this->shouldFireUptimeCheckFailedEvent()) {
104
            $this->uptime_check_failed_event_fired_on_date = Carbon::now();
105
            $this->save();
0 ignored issues
show
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...
106
107
            $updatedMonitor = $this->fresh();
0 ignored issues
show
It seems like fresh() 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...
108
109
            $downtimePeriod = new Period($updatedMonitor->uptime_status_last_change_date, $this->uptime_last_check_date);
110
111
            event(new UptimeCheckFailed($this, $downtimePeriod));
112
        }
113
    }
114
115
    protected function shouldFireUptimeCheckFailedEvent(): bool
116
    {
117
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_monitor_failed_event_after_consecutive_failures')) {
118
            return true;
119
        }
120
121
        if (is_null($this->uptime_check_failed_event_fired_on_date)) {
122
            return false;
123
        }
124
125
        if (config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes') === 0) {
126
            return false;
127
        }
128
129
        if ($this->uptime_check_failed_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes')) {
130
            return true;
131
        }
132
133
        return false;
134
    }
135
}
136