1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\ServerStatus; |
4
|
|
|
|
5
|
|
|
use JJG\Ping; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
class Host |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $host; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Ping |
17
|
|
|
*/ |
18
|
|
|
protected $pinger; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
protected $requester; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var double |
27
|
|
|
*/ |
28
|
|
|
protected $ping = 0.0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var integer |
32
|
|
|
*/ |
33
|
|
|
protected $statusCode = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $host The host URL or IP to check |
37
|
|
|
*/ |
38
|
18 |
|
public function __construct($host) |
39
|
|
|
{ |
40
|
18 |
|
$this->host = $this->strip($host); |
41
|
18 |
|
$this->pinger = new Ping($host); |
42
|
18 |
|
$this->requester = new Client(); |
43
|
18 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Strips off the https and http |
47
|
|
|
* @param string $host The URL to strip |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
18 |
|
protected function strip($host) |
51
|
|
|
{ |
52
|
18 |
|
$host = str_replace("https://","",$host); |
53
|
18 |
|
$host = str_replace("http://", "",$host); |
54
|
18 |
|
return $host; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks the ping of the host |
59
|
|
|
* |
60
|
|
|
* @return double|false |
61
|
|
|
*/ |
62
|
6 |
|
public function ping() |
63
|
|
|
{ |
64
|
3 |
|
$this->ping = $this->pinger->ping(); |
|
|
|
|
65
|
6 |
|
return $this->getPing(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks the status of the host |
70
|
|
|
* |
71
|
|
|
* @return integer |
72
|
|
|
*/ |
73
|
3 |
|
public function status(){ |
74
|
3 |
|
$response = $this->requester->request('GET', $this->host); |
75
|
3 |
|
$this->statusCode = $response->getStatusCode(); |
76
|
3 |
|
return $this->getStatusCode(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets the value of pinger. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
3 |
|
public function setPinger(Ping $pinger) |
85
|
|
|
{ |
86
|
3 |
|
$this->pinger = $pinger; |
87
|
3 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the value of requester. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
3 |
|
public function setRequester(Client $requester) |
95
|
|
|
{ |
96
|
3 |
|
$this->requester = $requester; |
97
|
3 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the value of host. |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
3 |
|
public function getHost() |
105
|
|
|
{ |
106
|
3 |
|
return $this->host; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets the value of ping. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
6 |
|
public function getPing() |
115
|
|
|
{ |
116
|
6 |
|
return $this->ping; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets the value of statusCode. |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
6 |
|
public function getStatusCode() |
125
|
|
|
{ |
126
|
6 |
|
return $this->statusCode; |
127
|
|
|
} |
128
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.