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 ( 150d64...174ec2 )
by Freek
07:55
created

SupportsUptimeCheck::uptimeRequestSucceeded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
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 ($this->shouldLookForStringOnResponse() && ! str_contains((string)$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
            return;
56
        }
57
58
        $this->uptimeCheckSucceeded();
59
    }
60
61
    protected function shouldLookForStringOnResponse(): bool
62
    {
63
        return ! empty($this->look_for_string);
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
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...
75
76
        $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...
77
        $lastStatusChangeDate = $this->uptime_status_last_change_date ? clone $this->uptime_status_last_change_date : null;
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...
78
79
        $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...
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
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...
83
84
        if ($wasFailing) {
85
            event(new UptimeCheckRecovered($this, $lastStatusChangeDate));
0 ignored issues
show
Bug introduced by
It seems like $lastStatusChangeDate defined by $this->uptime_status_las...last_change_date : null on line 77 can also be of type object; however, Spatie\UptimeMonitor\Eve...ecovered::__construct() does only seem to accept null|object<Carbon\Carbon>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
87
            return;
88
        }
89
90
        event(new UptimeCheckSucceeded($this));
91
    }
92
93
    public function uptimeCheckFailed(string $reason)
94
    {
95
        $this->uptime_status = UptimeStatus::DOWN;
96
        $this->uptime_check_times_failed_in_a_row++;
97
        $this->uptime_last_check_date = Carbon::now();
98
        $this->uptime_check_failure_reason = $reason;
99
        $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...
100
101
        if ($this->shouldFireUptimeCheckFailedEvent()) {
102
            $this->uptime_check_failed_event_fired_on_date = Carbon::now();
103
            $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...
104
105
            event(new UptimeCheckFailed($this));
106
        }
107
    }
108
109
    protected function shouldFireUptimeCheckFailedEvent(): bool
110
    {
111
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_monitor_failed_event_after_consecutive_failures')) {
112
            return true;
113
        }
114
115
        if (is_null($this->uptime_check_failed_event_fired_on_date)) {
116
            return false;
117
        }
118
119
        if (config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes') === 0) {
120
            return false;
121
        }
122
123
        if ($this->uptime_check_failed_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_uptime_check_failed_notification_every_minutes')) {
124
            return true;
125
        }
126
127
        return false;
128
    }
129
}
130