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 ( f14d00...b85ce8 )
by Freek
04:11 queued 01:55
created

SupportsUptimeCheck::couldReachSite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models\Traits;
4
5
use Spatie\UptimeMonitor\Events\MonitorFailed;
6
use Carbon\Carbon;
7
use Spatie\UptimeMonitor\Events\MonitorRecovered;
8
use Spatie\UptimeMonitor\Events\MonitorHealthy;
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->enabled) {
0 ignored issues
show
Bug introduced by
The property 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->uptimeTestFailed("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->uptimeTestSucceeded();
57
    }
58
59
    public function uptimeRequestFailed(string $reason)
60
    {
61
        $this->uptimeTestFailed($reason);
62
    }
63
64
    public function uptimeTestSucceeded()
65
    {
66
        $this->uptime_status = UptimeStatus::UP;
67
        $this->uptime_failure_reason = '';
0 ignored issues
show
Bug introduced by
The property uptime_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->down_event_fired_on_date);
0 ignored issues
show
Bug introduced by
The property down_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->down_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
        $eventClass = ($wasFailing ? MonitorRecovered::class : MonitorHealthy::class);
77
78
        event(new $eventClass($this));
79
    }
80
81
    public function uptimeTestFailed(string $reason)
82
    {
83
        $this->uptime_status = UptimeStatus::DOWN;
84
        $this->uptime_check_times_failed_in_a_row++;
85
        $this->uptime_last_check_date = Carbon::now();
86
        $this->uptime_failure_reason = $reason;
87
        $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...
88
89
        if ($this->shouldFireDownEvent()) {
90
            $this->down_event_fired_on_date = Carbon::now();
91
            $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...
92
93
            event(new MonitorFailed($this));
94
        }
95
    }
96
97
    protected function shouldFireDownEvent(): bool
98
    {
99
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_down_event_after_consecutive_failures')) {
100
            return true;
101
        }
102
103
        if (is_null($this->down_event_fired_on_date)) {
104
            return false;
105
        }
106
107
        if (config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes') === 0) {
108
            return false;
109
        }
110
111
        if ($this->down_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes')) {
112
            return true;
113
        }
114
115
        return false;
116
    }
117
}
118