Skip to content

Commit bf4d894

Browse files
authored
Add PHPstan (#59)
* Add phpstan and fix findings Co-authored-by: LKaemmerling <[email protected]>
1 parent eb7041f commit bf4d894

File tree

24 files changed

+294
-133
lines changed

24 files changed

+294
-133
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ jobs:
2626
composer install
2727
- name: Run phpunit
2828
run: php vendor/bin/phpunit
29+
- name: Run phpstan
30+
run: php vendor/bin/phpstan analyse src --level 1

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "^7.0|^8.5.5",
19+
"phpstan/phpstan": "^0.12.43",
1920
"brianium/paratest": "3.* || 4.*"
2021
},
2122
"license": "MIT",

src/Models/Actions/Action.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ public function __construct(
8484

8585
/**
8686
* @param $actionId
87-
* @return Action
87+
* @return Action|null
8888
* @throws \LKDev\HetznerCloud\APIException
8989
* @deprecated use Actions::getById instead
9090
*/
91-
public function getById($actionId): self
91+
public function getById($actionId): ?self
9292
{
9393
$response = $this->httpClient->get('actions/'.$actionId);
9494
if (! HetznerAPIClient::hasError($response)) {
9595
return self::parse(json_decode((string) $response->getBody())->action);
9696
}
97+
98+
return null;
9799
}
98100

99101
/**

src/Models/Actions/ActionRequestOpts.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 13:51.
77
*/
88

9-
namespace LKDev\HetznerCloud\Models\Datacenters;
9+
namespace LKDev\HetznerCloud\Models\Actions;
1010

1111
use LKDev\HetznerCloud\RequestOpts;
1212

@@ -16,6 +16,10 @@ class ActionRequestOpts extends RequestOpts
1616
* @var string
1717
*/
1818
public $status;
19+
/**
20+
* @var string
21+
*/
22+
public $sort;
1923

2024
/**
2125
* RequestOpts constructor.

src/Models/Actions/Actions.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function all(RequestOpts $requestOpts = null): array
3131

3232
/**
3333
* @param RequestOpts $requestOpts
34-
* @return APIResponse
34+
* @return APIResponse|null
3535
* @throws \LKDev\HetznerCloud\APIException
3636
*/
37-
public function list(RequestOpts $requestOpts = null): APIResponse
37+
public function list(RequestOpts $requestOpts = null): ?APIResponse
3838
{
3939
if ($requestOpts == null) {
4040
$requestOpts = new RequestOpts();
@@ -48,19 +48,23 @@ public function list(RequestOpts $requestOpts = null): APIResponse
4848
'actions' => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
4949
], $response->getHeaders());
5050
}
51+
52+
return null;
5153
}
5254

5355
/**
5456
* @param $actionId
55-
* @return \LKDev\HetznerCloud\Models\Actions\Action
57+
* @return \LKDev\HetznerCloud\Models\Actions\Action|null
5658
* @throws \LKDev\HetznerCloud\APIException
5759
*/
58-
public function getById(int $actionId): Action
60+
public function getById(int $actionId): ?Action
5961
{
6062
$response = $this->httpClient->get('actions/'.$actionId);
6163
if (! HetznerAPIClient::hasError($response)) {
6264
return Action::parse(json_decode((string) $response->getBody())->action);
6365
}
66+
67+
return null;
6468
}
6569

6670
public function getByName(string $name)

src/Models/Contracts/Resources.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Resources
99
{
1010
public function all(RequestOpts $requestOpts = null): array;
1111

12-
public function list(RequestOpts $requestOpts = null): APIResponse;
12+
public function list(RequestOpts $requestOpts = null): ?APIResponse;
1313

1414
public function getById(int $id);
1515

src/Models/Datacenters/Datacenters.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class Datacenters extends Model implements Resources
2323
{
2424
use GetFunctionTrait;
25+
2526
/**
2627
* @var array
2728
*/
@@ -49,10 +50,10 @@ public function all(RequestOpts $requestOpts = null): array
4950
*
5051
* @see https://docs.hetzner.cloud/#resources-datacenters-get
5152
* @param RequestOpts|null $requestOpts
52-
* @return APIResponse
53+
* @return APIResponse|null
5354
* @throws \LKDev\HetznerCloud\APIException
5455
*/
55-
public function list(RequestOpts $requestOpts = null): APIResponse
56+
public function list(RequestOpts $requestOpts = null): ?APIResponse
5657
{
5758
if ($requestOpts == null) {
5859
$requestOpts = new DatacenterRequestOpts();
@@ -67,22 +68,26 @@ public function list(RequestOpts $requestOpts = null): APIResponse
6768
$this->_getKeys()['many'] => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
6869
], $response->getHeaders());
6970
}
71+
72+
return null;
7073
}
7174

7275
/**
7376
* Returns a specific datacenter object.
7477
*
7578
* @see https://docs.hetzner.cloud/#resources-datacenters-get-1
7679
* @param int $datacenterId
77-
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter
80+
* @return \LKDev\HetznerCloud\Models\Datacenters\Datacenter|null
7881
* @throws \LKDev\HetznerCloud\APIException
7982
*/
80-
public function getById(int $datacenterId): Datacenter
83+
public function getById(int $datacenterId): ?Datacenter
8184
{
8285
$response = $this->httpClient->get('datacenters/'.$datacenterId);
8386
if (! HetznerAPIClient::hasError($response)) {
8487
return Datacenter::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']});
8588
}
89+
90+
return null;
8691
}
8792

8893
/**

src/Models/FloatingIps/FloatingIp.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,31 @@ public function __construct(
127127
*
128128
* @see https://docs.hetzner.cloud/#resources-floating-ips-put
129129
* @param string $description
130-
* @return static
130+
* @return static|null
131131
* @throws \LKDev\HetznerCloud\APIException
132132
*/
133-
public function update(array $data): self
133+
public function update(array $data): ?self
134134
{
135135
$response = $this->httpClient->put('floating_ips/'.$this->id, [
136136
'json' => $data,
137137
]);
138138
if (! HetznerAPIClient::hasError($response)) {
139139
return self::parse(json_decode((string) $response->getBody())->floating_ip);
140140
}
141+
142+
return null;
141143
}
142144

143145
/**
144146
* Changes the description of a Floating IP.
145147
*
146148
* @see https://docs.hetzner.cloud/#resources-floating-ips-put
147149
* @param string $description
148-
* @return static
150+
* @return static|null
149151
* @throws \LKDev\HetznerCloud\APIException
150152
* @deprecated 1.2.0
151153
*/
152-
public function changeDescription(string $description): self
154+
public function changeDescription(string $description): ?self
153155
{
154156
return $this->update(['description' => $description]);
155157
}
@@ -167,17 +169,19 @@ public function delete(): bool
167169
if (! HetznerAPIClient::hasError($response)) {
168170
return true;
169171
}
172+
173+
return false;
170174
}
171175

172176
/**
173177
* Changes the protection configuration of the Floating IP.
174178
*
175179
* @see https://docs.hetzner.cloud/#resources-floating-ip-actions-post-3
176180
* @param bool $delete
177-
* @return APIResponse
181+
* @return APIResponse|null
178182
* @throws \LKDev\HetznerCloud\APIException
179183
*/
180-
public function changeProtection(bool $delete = true): APIResponse
184+
public function changeProtection(bool $delete = true): ?APIResponse
181185
{
182186
$response = $this->httpClient->post('floating_ips/'.$this->id.'/actions/change_protection', [
183187
'json' => [
@@ -189,17 +193,19 @@ public function changeProtection(bool $delete = true): APIResponse
189193
'action' => Action::parse(json_decode((string) $response->getBody())->action),
190194
], $response->getHeaders());
191195
}
196+
197+
return null;
192198
}
193199

194200
/**
195201
* Assigns a Floating IP to a server.
196202
*
197203
* @see https://docs.hetzner.cloud/#floating-ip-actions-assign-a-floating-ip-to-a-server
198204
* @param Server $server
199-
* @return APIResponse
205+
* @return APIResponse|null
200206
* @throws \LKDev\HetznerCloud\APIException
201207
*/
202-
public function assignTo(Server $server): APIResponse
208+
public function assignTo(Server $server): ?APIResponse
203209
{
204210
$response = $this->httpClient->post('floating_ips/'.$this->id.'/actions/assign', [
205211
'json' => [
@@ -211,23 +217,27 @@ public function assignTo(Server $server): APIResponse
211217
'action' => Action::parse(json_decode((string) $response->getBody())->action),
212218
], $response->getHeaders());
213219
}
220+
221+
return null;
214222
}
215223

216224
/**
217225
* Unassigns a Floating IP, resulting in it being unreachable. You may assign it to a server again at a later time.
218226
*
219227
* @see https://docs.hetzner.cloud/#floating-ip-actions-unassign-a-floating-ip
220-
* @return APIResponse
228+
* @return APIResponse|null
221229
* @throws \LKDev\HetznerCloud\APIException
222230
*/
223-
public function unassign(): APIResponse
231+
public function unassign(): ?APIResponse
224232
{
225233
$response = $this->httpClient->post('floating_ips/'.$this->id.'/actions/unassign');
226234
if (! HetznerAPIClient::hasError($response)) {
227235
return APIResponse::create([
228236
'action' => Action::parse(json_decode((string) $response->getBody())->action),
229237
], $response->getHeaders());
230238
}
239+
240+
return null;
231241
}
232242

233243
/**
@@ -236,10 +246,10 @@ public function unassign(): APIResponse
236246
* @see https://docs.hetzner.cloud/#floating-ip-actions-change-reverse-dns-entry-for-a-floating-ip
237247
* @param string $ip
238248
* @param string $dnsPtr
239-
* @return APIResponse
249+
* @return APIResponse|null
240250
* @throws \LKDev\HetznerCloud\APIException
241251
*/
242-
public function changeReverseDNS(string $ip, string $dnsPtr): APIResponse
252+
public function changeReverseDNS(string $ip, string $dnsPtr): ?APIResponse
243253
{
244254
$response = $this->httpClient->post('floating_ips/'.$this->id.'/actions/change_dns_ptr', [
245255
'json' => [
@@ -252,13 +262,15 @@ public function changeReverseDNS(string $ip, string $dnsPtr): APIResponse
252262
'action' => Action::parse(json_decode((string) $response->getBody())->action),
253263
], $response->getHeaders());
254264
}
265+
266+
return null;
255267
}
256268

257269
/**
258270
* @param $input
259271
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp|static|null
260272
*/
261-
public static function parse($input): self
273+
public static function parse($input): ?self
262274
{
263275
if ($input == null) {
264276
return null;

src/Models/FloatingIps/FloatingIps.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class FloatingIps extends Model implements Resources
2222
{
2323
use GetFunctionTrait;
24+
2425
/**
2526
* @var array
2627
*/
@@ -48,10 +49,10 @@ public function all(RequestOpts $requestOpts = null): array
4849
*
4950
* @see https://docs.hetzner.cloud/#resources-floating-ips-get
5051
* @param FloatingIPRequestOpts|RequestOpts|null $requestOpts
51-
* @return APIResponse
52+
* @return APIResponse|null
5253
* @throws \LKDev\HetznerCloud\APIException
5354
*/
54-
public function list(RequestOpts $requestOpts = null): APIResponse
55+
public function list(RequestOpts $requestOpts = null): ?APIResponse
5556
{
5657
if ($requestOpts == null) {
5758
$requestOpts = new FloatingIPRequestOpts();
@@ -65,22 +66,26 @@ public function list(RequestOpts $requestOpts = null): APIResponse
6566
$this->_getKeys()['many'] => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']},
6667
], $response->getHeaders());
6768
}
69+
70+
return null;
6871
}
6972

7073
/**
7174
* Returns a specific floating ip object.
7275
*
7376
* @see https://docs.hetzner.cloud/#resources-floating-ips-get-1
7477
* @param int $locationId
75-
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
78+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp|null
7679
* @throws \LKDev\HetznerCloud\APIException
7780
*/
78-
public function getById(int $floatingIpId): FloatingIp
81+
public function getById(int $floatingIpId): ?FloatingIp
7982
{
8083
$response = $this->httpClient->get('floating_ips/'.$floatingIpId);
8184
if (! HetznerAPIClient::hasError($response)) {
8285
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
8386
}
87+
88+
return null;
8489
}
8590

8691
/**
@@ -107,7 +112,7 @@ public function getByName(string $name): ?FloatingIp
107112
* @param \LKDev\HetznerCloud\Models\Locations\Location|null $location
108113
* @param \LKDev\HetznerCloud\Models\Servers\Server|null $server
109114
* @param string|null $name
110-
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp
115+
* @return \LKDev\HetznerCloud\Models\FloatingIps\FloatingIp|null
111116
* @throws \LKDev\HetznerCloud\APIException
112117
*/
113118
public function create(
@@ -116,7 +121,7 @@ public function create(
116121
Location $location = null,
117122
Server $server = null,
118123
string $name = null
119-
): FloatingIp {
124+
): ?FloatingIp {
120125
$response = $this->httpClient->post('floating_ips', [
121126
'type' => $type,
122127
'description' => $description,
@@ -127,6 +132,8 @@ public function create(
127132
if (! HetznerAPIClient::hasError($response)) {
128133
return FloatingIp::parse(json_decode((string) $response->getBody())->floating_ip);
129134
}
135+
136+
return null;
130137
}
131138

132139
/**

src/Models/ISOs/ISO.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function __construct(int $id, string $name = null, string $description =
4949
* @param $input
5050
* @return \LKDev\HetznerCloud\Models\ISOs\ISO|static
5151
*/
52-
public static function parse($input)
52+
public static function parse($input): ?self
5353
{
5454
if ($input == null) {
55-
return;
55+
return null;
5656
}
5757

5858
return new self($input->id, $input->name, $input->description, $input->type);

0 commit comments

Comments
 (0)