Skip to content

Commit 88409b7

Browse files
Fixed hydration of the Network class with IPv6 addresses (#339)
* IPv6 the CIDR field assigned with wrong type and notation. For IPv6 networks the netmask is integer and the CIDR field is assigned with `int` to a field with `string` constraint. The netmask in this case represents the number for allow networks. The IPv6 address `2b03:b0c0:2:d0::102e:d001` should have the CIDR notation `2b03:b0c0:2:d0::102e:d001/64` or `2b03:b0c0:0002:00d0:0000:0000:0000:0000/64`. I'm not 100% sure what the netmask value can be on the api die, is it always int? or can it come as string? that's why the check for int. * Added 'networks' to droplet test fully checking build() logic. * Refactoring the networks fix code to comply with phpstan rules. * Tested multiple solutions, this is the nicest and most minimal one, and probably more correct since we are not manipulating values we iterate on. * This also this resulted some cleanup on the baseline rules file. * Cleanup * Fixed code style * Update CHANGELOG.md * IPv6 netmasks are always integers --------- Co-authored-by: alkavan <[email protected]> Co-authored-by: Graham Campbell <[email protected]>
1 parent dfb125c commit 88409b7

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ CHANGE LOG
22
==========
33

44

5+
## 5.0.3 (UPCOMING)
6+
7+
* Fixed hydration of the Network class with IPv6 addresses
8+
9+
510
## 5.0.2 (04/03/2025)
611

712
* Fixed a couple of incorrect property types

phpstan-baseline.neon

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,10 @@ parameters:
192192
count: 2
193193
path: src/Entity/Droplet.php
194194

195-
-
196-
message: '#^Cannot access property \$cidr on mixed\.$#'
197-
identifier: property.nonObject
198-
count: 1
199-
path: src/Entity/Droplet.php
200-
201-
-
202-
message: '#^Cannot access property \$netmask on mixed\.$#'
203-
identifier: property.nonObject
204-
count: 2
205-
path: src/Entity/Droplet.php
206-
207195
-
208196
message: '#^Cannot access property \$version on mixed\.$#'
209197
identifier: property.nonObject
210-
count: 2
198+
count: 1
211199
path: src/Entity/Droplet.php
212200

213201
-

src/Entity/Droplet.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,15 @@ public function build(array $parameters): void
103103

104104
if (\property_exists($value, 'v6')) {
105105
foreach ($value->v6 as $subValue) {
106-
$subValue->version = 6;
107-
$subValue->cidr = $subValue->netmask;
108-
$subValue->netmask = null;
109-
$this->networks[] = new Network($subValue);
106+
/** @var object{ip_address: string, netmask: int, gateway: string, type: string} $subValue */
107+
$this->networks[] = new Network((object) [
108+
'ip_address' => $subValue->ip_address,
109+
'netmask' => null,
110+
'gateway' => $subValue->gateway,
111+
'type' => $subValue->type,
112+
'cidr' => \sprintf('%s/%d', $subValue->ip_address, $subValue->netmask),
113+
'version' => 6,
114+
]);
110115
}
111116
}
112117
}

tests/DropletEntityTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ public function testConstructor(): void
9595
'description' => 'Basic',
9696
],
9797
'size_slug' => 's-1vcpu-1gb',
98+
'networks' => (object) [
99+
'v4' => [
100+
(object) [
101+
'ip_address' => '10.135.0.3',
102+
'netmask' => '255.255.0.0',
103+
'gateway' => '10.135.0.1',
104+
'type' => 'private',
105+
],
106+
],
107+
'v6' => [
108+
(object) [
109+
'ip_address' => '2001:db8:a::1234:5678',
110+
'netmask' => 64,
111+
'gateway' => '2001:db8:a::1',
112+
'type' => 'public',
113+
],
114+
],
115+
],
98116
'region' => (object) [
99117
'name' => 'New York 3',
100118
'slug' => 'nyc3',

0 commit comments

Comments
 (0)