Skip to content

Commit e3fab49

Browse files
authored
Merge pull request #116 from maxmind/greg/ci-static-checker
Lint with phpstan
2 parents 9ee9ba9 + a4c0124 commit e3fab49

7 files changed

Lines changed: 87 additions & 37 deletions

File tree

.github/workflows/lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ jobs:
2323

2424
- name: Lint with phpcs
2525
run: vendor/bin/phpcs --standard=.phpcs-ruleset.xml src/
26+
27+
- name: Lint with phpstan
28+
run: vendor/bin/phpstan analyze

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"phpunit/phpunit": ">=8.0.0,<10.0.0",
2929
"php-coveralls/php-coveralls": "^2.1",
3030
"phpunit/phpcov": ">=6.0.0",
31-
"squizlabs/php_codesniffer": "3.*"
31+
"squizlabs/php_codesniffer": "3.*",
32+
"phpstan/phpstan": "*"
3233
},
3334
"autoload": {
3435
"psr-4": {

phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
- tests
6+
checkMissingIterableValueType: false
7+

src/MaxMind/Db/Reader/Decoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ private function decodePointer(int $ctrlByte, int $offset): array
291291
return [$pointer, $offset];
292292
}
293293

294+
// @phpstan-ignore-next-line
294295
private function decodeUint(string $bytes, int $byteLength)
295296
{
296297
if ($byteLength === 0) {

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

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
class DecoderTest extends TestCase
1414
{
15+
/**
16+
* @var array<array<string, mixed>>
17+
*/
1518
private $arrays = [
1619
[
1720
'expected' => [],
@@ -37,6 +40,9 @@ class DecoderTest extends TestCase
3740
],
3841
];
3942

43+
/**
44+
* @var array<array<string, mixed>>
45+
*/
4046
private $booleans = [
4147
[
4248
'expected' => false,
@@ -48,6 +54,9 @@ class DecoderTest extends TestCase
4854
],
4955
];
5056

57+
/**
58+
* @var array<array<string, mixed>>
59+
*/
5160
private $doubles = [
5261
['expected' => 0.0, 'input' => [0x68, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]],
5362
['expected' => 0.5, 'input' => [0x68, 0x3F, 0xE0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]],
@@ -59,6 +68,9 @@ class DecoderTest extends TestCase
5968
['expected' => -1073741824.12457, 'input' => [0x68, 0xC1, 0xD0, 0x0, 0x0, 0x0, 0x7, 0xF8, 0xF4]],
6069
];
6170

71+
/**
72+
* @var array<array<string, mixed>>
73+
*/
6274
private $floats = [
6375
['expected' => 0.0, 'input' => [0x4, 0x8, 0x0, 0x0, 0x0, 0x0]],
6476
['expected' => 1.0, 'input' => [0x4, 0x8, 0x3F, 0x80, 0x0, 0x0]],
@@ -73,6 +85,9 @@ class DecoderTest extends TestCase
7385

7486
// PHP can't have arrays/objects as keys. Maybe redo all of the tests
7587
// this way so that we can use one test runner
88+
/**
89+
* @var array<array<string, mixed>>
90+
*/
7691
private $maps = [
7792
[
7893
'expected' => [],
@@ -137,22 +152,28 @@ class DecoderTest extends TestCase
137152
],
138153
];
139154

140-
private function pointers()
155+
/**
156+
* @return array<array<string, mixed>>
157+
*/
158+
private function pointers(): array
141159
{
142160
return [
143-
['expected' => 0, 'input' => [0x20, 0x0]],
144-
['expected' => 5, 'input' => [0x20, 0x5]],
145-
['expected' => 10, 'input' => [0x20, 0xa]],
146-
['expected' => 1023, 'input' => [0x23, 0xff]],
147-
['expected' => 3017, 'input' => [0x28, 0x3, 0xc9]],
148-
['expected' => 524283, 'input' => [0x2f, 0xf7, 0xfb]],
149-
['expected' => 526335, 'input' => [0x2f, 0xff, 0xff]],
150-
['expected' => 134217726, 'input' => [0x37, 0xf7, 0xf7, 0xfe]],
151-
['expected' => PHP_INT_MAX < 4294967295 ? '2147483647' : 2147483647, 'input' => [0x38, 0x7f, 0xff, 0xff, 0xff]],
152-
['expected' => PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0x38, 0xff, 0xff, 0xff, 0xff]],
153-
];
161+
['expected' => 0, 'input' => [0x20, 0x0]],
162+
['expected' => 5, 'input' => [0x20, 0x5]],
163+
['expected' => 10, 'input' => [0x20, 0xa]],
164+
['expected' => 1023, 'input' => [0x23, 0xff]],
165+
['expected' => 3017, 'input' => [0x28, 0x3, 0xc9]],
166+
['expected' => 524283, 'input' => [0x2f, 0xf7, 0xfb]],
167+
['expected' => 526335, 'input' => [0x2f, 0xff, 0xff]],
168+
['expected' => 134217726, 'input' => [0x37, 0xf7, 0xf7, 0xfe]],
169+
['expected' => PHP_INT_MAX < 4294967295 ? '2147483647' : 2147483647, 'input' => [0x38, 0x7f, 0xff, 0xff, 0xff]],
170+
['expected' => PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0x38, 0xff, 0xff, 0xff, 0xff]],
171+
];
154172
}
155173

174+
/**
175+
* @var array<array<string, mixed>>
176+
*/
156177
private $uint16 = [
157178
['expected' => 0, 'input' => [0xa0]],
158179
['expected' => 255, 'input' => [0xa1, 0xff]],
@@ -161,6 +182,9 @@ private function pointers()
161182
['expected' => 65535, 'input' => [0xa2, 0xff, 0xff]],
162183
];
163184

185+
/**
186+
* @var array<array<string, mixed>>
187+
*/
164188
private $int32 = [
165189
['expected' => 0, 'input' => [0x0, 0x1]],
166190
['expected' => -1, 'input' => [0x4, 0x1, 0xff, 0xff, 0xff, 0xff]],
@@ -176,7 +200,10 @@ private function pointers()
176200
['expected' => -2147483647, 'input' => [0x4, 0x1, 0x80, 0x0, 0x0, 0x1]],
177201
];
178202

179-
private function strings()
203+
/**
204+
* @return array<array<string, mixed>>
205+
*/
206+
private function strings(): array
180207
{
181208
$strings = [
182209
['expected' => '', 'input' => [0x40]],
@@ -228,20 +255,26 @@ private function strings()
228255
return $strings;
229256
}
230257

231-
private function uint32()
258+
/**
259+
* @return array<array<string, mixed>>
260+
*/
261+
private function uint32(): array
232262
{
233263
return [
234-
['expected' => 0, 'input' => [0xc0]],
235-
['expected' => 255, 'input' => [0xc1, 0xff]],
236-
['expected' => 500, 'input' => [0xc2, 0x1, 0xf4]],
237-
['expected' => 10872, 'input' => [0xc2, 0x2a, 0x78]],
238-
['expected' => 65535, 'input' => [0xc2, 0xff, 0xff]],
239-
['expected' => 16777215, 'input' => [0xc3, 0xff, 0xff, 0xff]],
240-
['expected' => PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0xc4, 0xff, 0xff, 0xff, 0xff]],
241-
];
264+
['expected' => 0, 'input' => [0xc0]],
265+
['expected' => 255, 'input' => [0xc1, 0xff]],
266+
['expected' => 500, 'input' => [0xc2, 0x1, 0xf4]],
267+
['expected' => 10872, 'input' => [0xc2, 0x2a, 0x78]],
268+
['expected' => 65535, 'input' => [0xc2, 0xff, 0xff]],
269+
['expected' => 16777215, 'input' => [0xc3, 0xff, 0xff, 0xff]],
270+
['expected' => PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0xc4, 0xff, 0xff, 0xff, 0xff]],
271+
];
242272
}
243273

244-
private function bytes()
274+
/**
275+
* @return array<array<string, mixed>>
276+
*/
277+
private function bytes(): array
245278
{
246279
// ugly deep clone
247280
$bytes = unserialize(serialize($this->strings()));
@@ -253,7 +286,7 @@ private function bytes()
253286
return $bytes;
254287
}
255288

256-
public function generateLargeUint($bits)
289+
public function generateLargeUint(int $bits): array
257290
{
258291
$ctrlByte = $bits === 64 ? 0x2 : 0x3;
259292

@@ -270,8 +303,6 @@ public function generateLargeUint($bits)
270303
$expected = bcsub(bcpow('2', (string) (8 * $power)), '1');
271304
} else {
272305
$this->markTestSkipped('This test requires gmp or bcmath.');
273-
274-
return;
275306
}
276307
$input = [$power, $ctrlByte];
277308
for ($i = 2; $i < 2 + $power; ++$i) {
@@ -348,14 +379,14 @@ public function testUint128(): void
348379
$this->validateTypeDecoding('uint128', $this->generateLargeUint(128));
349380
}
350381

351-
private function validateTypeDecoding($type, $tests): void
382+
private function validateTypeDecoding(string $type, array $tests): void
352383
{
353384
foreach ($tests as $expected => $input) {
354385
$this->checkDecoding($type, $input, $expected);
355386
}
356387
}
357388

358-
private function validateTypeDecodingList($type, $tests): void
389+
private function validateTypeDecodingList(string $type, array $tests): void
359390
{
360391
foreach ($tests as $test) {
361392
$this->checkDecoding(
@@ -367,7 +398,8 @@ private function validateTypeDecodingList($type, $tests): void
367398
}
368399
}
369400

370-
private function checkDecoding($type, $input, $expected, $name = null): void
401+
// @phpstan-ignore-next-line
402+
private function checkDecoding(string $type, array $input, $expected, $name = null): void
371403
{
372404
$name = $name || $expected;
373405
$description = "decoded $type - $name";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testTooManyConstructorArgs(): void
5454
public function testNoConstructorArgs(): void
5555
{
5656
$this->expectException(ArgumentCountError::class);
57+
/** @phpstan-ignore-next-line */
5758
new Metadata();
5859
}
5960
}

tests/MaxMind/Db/Test/ReaderTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function testDecoder(): void
6363
$this->assertSame(-268435456, $record['int32']);
6464
$this->assertSame(100, $record['uint16']);
6565
$this->assertSame(PHP_INT_MAX < 4294967295 && !\extension_loaded('maxminddb') ? '268435456' : 268435456, $record['uint32']);
66+
// @phpstan-ignore-next-line
6667
$this->assertSame(PHP_INT_MAX > 1152921504606846976 && \extension_loaded('maxminddb') ? 1152921504606846976 : '1152921504606846976', $record['uint64']);
6768

6869
$uint128 = $record['uint128'];
@@ -155,10 +156,11 @@ public function testGetWithPrefixLen(): void
155156
],
156157
'uint128' => \extension_loaded('maxminddb') ? '0x01000000000000000000000000000000' : '1329227995784915872903807060280344576',
157158
'uint16' => 0x64,
158-
'uint32' => PHP_INT_MAX < 4294967295 && !\extension_loaded('maxminddb') ? '268435456' : 268435456,
159-
'uint64' => PHP_INT_MAX > 1152921504606846976 && \extension_loaded('maxminddb') ? 1152921504606846976 : '1152921504606846976',
159+
'uint32' => PHP_INT_MAX < 4294967295 && !\extension_loaded('maxminddb') ? '268435456' : 268435456,
160+
// @phpstan-ignore-next-line
161+
'uint64' => PHP_INT_MAX > 1152921504606846976 && \extension_loaded('maxminddb') ? 1152921504606846976 : '1152921504606846976',
160162
'utf8_string' => 'unicode! ☯ - ♫',
161-
];
163+
];
162164
$tests = [
163165
[
164166
'ip' => '1.1.1.1',
@@ -246,6 +248,7 @@ public function testV6AddressV4Database(): void
246248
{
247249
$this->expectException(InvalidArgumentException::class);
248250
$this->expectExceptionMessage('Error looking up 2001::. You attempted to look up an IPv6 address in an IPv4-only database');
251+
// @phpstan-ignore-next-line
249252
if (\defined('MaxMind\\Db\\Reader::MMDB_LIB_VERSION') && version_compare(Reader::MMDB_LIB_VERSION, '1.2.0', '<')) {
250253
$this->markTestSkipped('MMDB_LIB_VERSION < 1.2.0');
251254
}
@@ -312,6 +315,7 @@ public function testTooManyConstructorArgs(): void
312315
public function testNoConstructorArgs(): void
313316
{
314317
$this->expectException(ArgumentCountError::class);
318+
// @phpstan-ignore-next-line
315319
new Reader();
316320
}
317321

@@ -334,6 +338,7 @@ public function testNoGetArgs(): void
334338
$reader = new Reader(
335339
'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
336340
);
341+
// @phpstan-ignore-next-line
337342
$reader->get();
338343
}
339344

@@ -402,11 +407,11 @@ public function testClosedMetadata(): void
402407

403408
public function testReaderIsNotFinal(): void
404409
{
405-
$reflectionClass = new ReflectionClass('MaxMind\Db\Reader');
410+
$reflectionClass = new ReflectionClass(Reader::class);
406411
$this->assertFalse($reflectionClass->isFinal());
407412
}
408413

409-
private function checkMetadata($reader, $ipVersion, $recordSize): void
414+
private function checkMetadata(Reader $reader, int $ipVersion, int $recordSize): void
410415
{
411416
$metadata = $reader->metadata();
412417

@@ -433,7 +438,7 @@ private function checkMetadata($reader, $ipVersion, $recordSize): void
433438
$this->assertGreaterThan(200, $metadata->searchTreeSize);
434439
}
435440

436-
private function checkIpV4(Reader $reader, $fileName): void
441+
private function checkIpV4(Reader $reader, string $fileName): void
437442
{
438443
for ($i = 0; $i <= 5; ++$i) {
439444
$address = '1.1.1.' . 2 ** $i;
@@ -471,7 +476,7 @@ private function checkIpV4(Reader $reader, $fileName): void
471476
}
472477

473478
// XXX - logic could be combined with above
474-
private function checkIpV6(Reader $reader, $fileName): void
479+
private function checkIpV6(Reader $reader, string $fileName): void
475480
{
476481
$subnets = ['::1:ffff:ffff', '::2:0:0',
477482
'::2:0:40', '::2:0:50', '::2:0:58', ];

0 commit comments

Comments
 (0)