Skip to content

Commit 7297c4f

Browse files
committed
Improve the type hints for arrays
1 parent cbf37b8 commit 7297c4f

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Improve the error handling when the user tries to open a directory
88
with the pure PHP reader.
9+
* Improve the typehints on arrays in the PHPDocs.
910

1011
1.11.1 (2023-12-01)
1112
-------------------

src/MaxMind/Db/Reader.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public function get(string $ipAddress)
149149
* if the database is invalid or there is an error reading
150150
* from it
151151
*
152-
* @return array an array where the first element is the record and the
153-
* second the network prefix length for the record
152+
* @return array{0:mixed, 1:int} an array where the first element is the record and the
153+
* second the network prefix length for the record
154154
*/
155155
public function getWithPrefixLen(string $ipAddress): array
156156
{
@@ -174,6 +174,9 @@ public function getWithPrefixLen(string $ipAddress): array
174174
return [$this->resolveDataPointer($pointer), $prefixLen];
175175
}
176176

177+
/**
178+
* @return array{0:int, 1:int}
179+
*/
177180
private function findAddressInTree(string $ipAddress): array
178181
{
179182
$packedAddr = @inet_pton($ipAddress);

src/MaxMind/Db/Reader/Decoder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function __construct(
6363
$this->switchByteOrder = $this->isPlatformLittleEndian();
6464
}
6565

66+
/**
67+
* @return array<mixed>
68+
*/
6669
public function decode(int $offset): array
6770
{
6871
$ctrlByte = \ord(Util::read($this->fileStream, $offset, 1));
@@ -110,6 +113,8 @@ public function decode(int $offset): array
110113

111114
/**
112115
* @param int<0, max> $size
116+
*
117+
* @return array{0:mixed, 1:int}
113118
*/
114119
private function decodeByType(int $type, int $offset, int $size): array
115120
{
@@ -167,6 +172,9 @@ private function verifySize(int $expected, int $actual): void
167172
}
168173
}
169174

175+
/**
176+
* @return array{0:array<mixed>, 1:int}
177+
*/
170178
private function decodeArray(int $size, int $offset): array
171179
{
172180
$array = [];
@@ -247,6 +255,9 @@ private function decodeInt32(string $bytes, int $size): int
247255
return $int;
248256
}
249257

258+
/**
259+
* @return array{0:array<string, mixed>, 1:int}
260+
*/
250261
private function decodeMap(int $size, int $offset): array
251262
{
252263
$map = [];
@@ -260,6 +271,9 @@ private function decodeMap(int $size, int $offset): array
260271
return [$map, $offset];
261272
}
262273

274+
/**
275+
* @return array{0:int, 1:int}
276+
*/
263277
private function decodePointer(int $ctrlByte, int $offset): array
264278
{
265279
$pointerSize = (($ctrlByte >> 3) & 0x3) + 1;
@@ -378,6 +392,9 @@ private function decodeUint(string $bytes, int $byteLength)
378392
return $integerAsString;
379393
}
380394

395+
/**
396+
* @return array{0:int, 1:int}
397+
*/
381398
private function sizeFromCtrlByte(int $ctrlByte, int $offset): array
382399
{
383400
$size = $ctrlByte & 0x1F;

src/MaxMind/Db/Reader/Metadata.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Metadata
4848
* in that language as a UTF-8 string. May be undefined for some
4949
* databases.
5050
*
51-
* @var array
51+
* @var array<string, string>
5252
*/
5353
public $description;
5454

@@ -65,7 +65,7 @@ class Metadata
6565
* may contain data items that have been localized to some or all of
6666
* these languages. This may be undefined.
6767
*
68-
* @var array
68+
* @var array<string>
6969
*/
7070
public $languages;
7171

@@ -95,6 +95,9 @@ class Metadata
9595
*/
9696
public $searchTreeSize;
9797

98+
/**
99+
* @param array<string, mixed> $metadata
100+
*/
98101
public function __construct(array $metadata)
99102
{
100103
if (\func_num_args() !== 1) {

tests/MaxMind/Db/Test/Reader/DecoderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ private function bytes(): array
291291
return $bytes;
292292
}
293293

294+
/**
295+
* @return array<int, array<int>>
296+
*/
294297
public function generateLargeUint(int $bits): array
295298
{
296299
$ctrlByte = $bits === 64 ? 0x2 : 0x3;
@@ -384,13 +387,19 @@ public function testUint128(): void
384387
$this->validateTypeDecoding('uint128', $this->generateLargeUint(128));
385388
}
386389

390+
/**
391+
* @param array<mixed> $tests
392+
*/
387393
private function validateTypeDecoding(string $type, array $tests): void
388394
{
389395
foreach ($tests as $expected => $input) {
390396
$this->checkDecoding($type, $input, $expected);
391397
}
392398
}
393399

400+
/**
401+
* @param array<mixed> $tests
402+
*/
394403
private function validateTypeDecodingList(string $type, array $tests): void
395404
{
396405
foreach ($tests as $test) {

tests/MaxMind/Db/Test/ReaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,12 @@ public function testMetadataArgs(): void
364364

365365
public function testClose(): void
366366
{
367+
$this->expectNotToPerformAssertions();
368+
367369
$reader = new Reader(
368370
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
369371
);
370372
$reader->close();
371-
372-
$this->assertTrue(true);
373373
}
374374

375375
public function testCloseArgs(): void

0 commit comments

Comments
 (0)