Skip to content

Commit 1d27597

Browse files
authored
Merge pull request #117 from maxmind/greg/run-new-php-cs-fixer
Update php-cs-fixer
2 parents e3fab49 + 9319f74 commit 1d27597

File tree

9 files changed

+112
-71
lines changed

9 files changed

+112
-71
lines changed

.php_cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ return PhpCsFixer\Config::create()
55
->setRules([
66
'@Symfony' => true,
77
'@Symfony:risky' => true,
8+
'@PhpCsFixer' => true,
9+
'@PSR1' => true,
10+
'@PSR2' => true,
11+
'@PSR12' => true,
12+
'@PSR12:risky' => true,
813
'array_syntax' => ['syntax' => 'short'],
914
'combine_consecutive_unsets' => true,
1015
'concat_space' => [ 'spacing' => 'one'],
16+
'explicit_string_variable' => false,
1117
'fopen_flags' => ['b_mode' => true],
1218
'heredoc_to_nowdoc' => true,
19+
'increment_style' => false,
1320
'list_syntax' => ['syntax' => 'short'],
21+
'multiline_whitespace_before_semicolons' => false,
1422
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
1523
'no_short_echo_tag' => true,
1624
'no_unreachable_default_argument_value' => true,
@@ -23,7 +31,7 @@ return PhpCsFixer\Config::create()
2331
'phpdoc_add_missing_param_annotation' => true,
2432
'phpdoc_no_alias_tag' => false,
2533
'phpdoc_order' => true,
26-
'phpdoc_to_comment' => false,
34+
'phpdoc_types_order' => ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
2735
'semicolon_after_instruction' => true,
2836
'single_line_throw' => false,
2937
'strict_comparison' => true,

autoload.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ function mmdb_autoload($class): void
2828
$namespace_map = ['MaxMind\\Db\\' => __DIR__ . '/src/MaxMind/Db/'];
2929

3030
foreach ($namespace_map as $prefix => $dir) {
31-
/* First swap out the namespace prefix with a directory... */
31+
// First swap out the namespace prefix with a directory...
3232
$path = str_replace($prefix, $dir, $class);
3333

34-
/* replace the namespace separator with a directory separator... */
34+
// replace the namespace separator with a directory separator...
3535
$path = str_replace('\\', '/', $path);
3636

37-
/* and finally, add the PHP file extension to the result. */
37+
// and finally, add the PHP file extension to the result.
3838
$path = $path . '.php';
3939

40-
/* $path should now contain the path to a PHP file defining $class */
40+
// $path should now contain the path to a PHP file defining $class
4141
if (file_exists($path)) {
4242
include $path;
4343
}

src/MaxMind/Db/Reader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,12 @@ private function findAddressInTree(string $ipAddress): array
211211
if ($node === $nodeCount) {
212212
// Record is empty
213213
return [0, $i];
214-
} elseif ($node > $nodeCount) {
214+
}
215+
if ($node > $nodeCount) {
215216
// Record is a data pointer
216217
return [$node, $i];
217218
}
219+
218220
throw new InvalidDatabaseException(
219221
'Invalid or corrupt database. Maximum search depth reached without finding a leaf node'
220222
);
@@ -246,6 +248,7 @@ private function readNode(int $nodeNumber, int $index): int
246248
[, $node] = unpack('N', "\x00" . $bytes);
247249

248250
return $node;
251+
249252
case 28:
250253
$bytes = Util::read($this->fileHandle, $baseOffset + 3 * $index, 4);
251254
if ($index === 0) {
@@ -256,11 +259,13 @@ private function readNode(int $nodeNumber, int $index): int
256259
[, $node] = unpack('N', \chr($middle) . substr($bytes, $index, 3));
257260

258261
return $node;
262+
259263
case 32:
260264
$bytes = Util::read($this->fileHandle, $baseOffset + $index * 4, 4);
261265
[, $node] = unpack('N', $bytes);
262266

263267
return $node;
268+
264269
default:
265270
throw new InvalidDatabaseException(
266271
'Unknown record size: '
@@ -312,6 +317,7 @@ private function findMetadataStart(string $filename): int
312317
return $offset + $markerLength;
313318
}
314319
}
320+
315321
throw new InvalidDatabaseException(
316322
"Error opening database file ($filename). " .
317323
'Is this a valid MaxMind DB file?'

src/MaxMind/Db/Reader/Decoder.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// @codingStandardsIgnoreLine
88
use RuntimeException;
99

10-
/**
10+
/*
1111
* @ignore
1212
*
1313
* We subtract 1 from the log to protect against precision loss.
1414
*/
15-
\define(__NAMESPACE__ . '\_MM_MAX_INT_BYTES', (log(PHP_INT_MAX, 2) - 1) / 8);
15+
\define(__NAMESPACE__ . '\_MM_MAX_INT_BYTES', (log(\PHP_INT_MAX, 2) - 1) / 8);
1616

1717
class Decoder
1818
{
@@ -123,33 +123,41 @@ private function decodeByType(int $type, int $offset, int $size): array
123123
switch ($type) {
124124
case self::_MAP:
125125
return $this->decodeMap($size, $offset);
126+
126127
case self::_ARRAY:
127128
return $this->decodeArray($size, $offset);
129+
128130
case self::_BOOLEAN:
129131
return [$this->decodeBoolean($size), $offset];
130132
}
131133

132134
$newOffset = $offset + $size;
133135
$bytes = Util::read($this->fileStream, $offset, $size);
136+
134137
switch ($type) {
135138
case self::_BYTES:
136139
case self::_UTF8_STRING:
137140
return [$bytes, $newOffset];
141+
138142
case self::_DOUBLE:
139143
$this->verifySize(8, $size);
140144

141145
return [$this->decodeDouble($bytes), $newOffset];
146+
142147
case self::_FLOAT:
143148
$this->verifySize(4, $size);
144149

145150
return [$this->decodeFloat($bytes), $newOffset];
151+
146152
case self::_INT32:
147153
return [$this->decodeInt32($bytes, $size), $newOffset];
154+
148155
case self::_UINT16:
149156
case self::_UINT32:
150157
case self::_UINT64:
151158
case self::_UINT128:
152159
return [$this->decodeUint($bytes, $size), $newOffset];
160+
153161
default:
154162
throw new InvalidDatabaseException(
155163
'Unknown or unexpected type: ' . $type
@@ -206,13 +214,17 @@ private function decodeInt32(string $bytes, int $size): int
206214
switch ($size) {
207215
case 0:
208216
return 0;
217+
209218
case 1:
210219
case 2:
211220
case 3:
212-
$bytes = str_pad($bytes, 4, "\x00", STR_PAD_LEFT);
221+
$bytes = str_pad($bytes, 4, "\x00", \STR_PAD_LEFT);
222+
213223
break;
224+
214225
case 4:
215226
break;
227+
216228
default:
217229
throw new InvalidDatabaseException(
218230
"The MaxMind DB file's data section contains bad data (unknown data type or corrupt data)"
@@ -249,20 +261,26 @@ private function decodePointer(int $ctrlByte, int $offset): array
249261
$packed = \chr($ctrlByte & 0x7) . $buffer;
250262
[, $pointer] = unpack('n', $packed);
251263
$pointer += $this->pointerBase;
264+
252265
break;
266+
253267
case 2:
254268
$packed = "\x00" . \chr($ctrlByte & 0x7) . $buffer;
255269
[, $pointer] = unpack('N', $packed);
256270
$pointer += $this->pointerBase + 2048;
271+
257272
break;
273+
258274
case 3:
259275
$packed = \chr($ctrlByte & 0x7) . $buffer;
260276

261277
// It is safe to use 'N' here, even on 32 bit machines as the
262278
// first bit is 0.
263279
[, $pointer] = unpack('N', $packed);
264280
$pointer += $this->pointerBase + 526336;
281+
265282
break;
283+
266284
case 4:
267285
// We cannot use unpack here as we might overflow on 32 bit
268286
// machines
@@ -281,7 +299,9 @@ private function decodePointer(int $ctrlByte, int $offset): array
281299
'The gmp or bcmath extension must be installed to read this database.'
282300
);
283301
}
302+
284303
break;
304+
285305
default:
286306
throw new InvalidDatabaseException(
287307
'Unexpected pointer size ' . $pointerSize

src/MaxMind/Db/Reader/Util.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static function read($stream, int $offset, int $numberOfBytes): string
2424
return $value;
2525
}
2626
}
27+
2728
throw new InvalidDatabaseException(
2829
'The MaxMind DB file contains bad data'
2930
);

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* @coversNothing
12+
*
13+
* @internal
1214
*/
1315
class DecoderTest extends TestCase
1416
{
@@ -23,17 +25,17 @@ class DecoderTest extends TestCase
2325
],
2426
[
2527
'expected' => ['Foo'],
26-
'input' => [0x1, 0x4, /* Foo */
28+
'input' => [0x1, 0x4, // Foo
2729
0x43, 0x46, 0x6f, 0x6f, ],
2830
'name' => 'one element',
2931
],
3032
[
3133
'expected' => ['Foo', ''],
3234
'input' => [
3335
0x2, 0x4,
34-
/* Foo */
36+
// Foo
3537
0x43, 0x46, 0x6f, 0x6f,
36-
/* 人 */
38+
// 人
3739
0x43, 0xe4, 0xba, 0xba,
3840
],
3941
'name' => 'two elements',
@@ -96,23 +98,23 @@ class DecoderTest extends TestCase
9698
],
9799
[
98100
'expected' => ['en' => 'Foo'],
99-
'input' => [0xe1, /* en */
101+
'input' => [0xe1, // en
100102
0x42, 0x65, 0x6e,
101-
/* Foo */
103+
// Foo
102104
0x43, 0x46, 0x6f, 0x6f, ],
103105
'name' => 'one key',
104106
],
105107
[
106108
'expected' => ['en' => 'Foo', 'zh' => ''],
107109
'input' => [
108110
0xe2,
109-
/* en */
111+
// en
110112
0x42, 0x65, 0x6e,
111-
/* Foo */
113+
// Foo
112114
0x43, 0x46, 0x6f, 0x6f,
113-
/* zh */
115+
// zh
114116
0x42, 0x7a, 0x68,
115-
/* 人 */
117+
// 人
116118
0x43, 0xe4, 0xba, 0xba,
117119
],
118120
'name' => 'two keys',
@@ -121,15 +123,15 @@ class DecoderTest extends TestCase
121123
'expected' => ['name' => ['en' => 'Foo', 'zh' => '']],
122124
'input' => [
123125
0xe1,
124-
/* name */
126+
// name
125127
0x44, 0x6e, 0x61, 0x6d, 0x65, 0xe2,
126-
/* en */
128+
// en
127129
0x42, 0x65, 0x6e,
128-
/* Foo */
130+
// Foo
129131
0x43, 0x46, 0x6f, 0x6f,
130-
/* zh */
132+
// zh
131133
0x42, 0x7a, 0x68,
132-
/* 人 */
134+
// 人
133135
0x43, 0xe4, 0xba, 0xba,
134136
],
135137
'name' => 'nested',
@@ -138,14 +140,14 @@ class DecoderTest extends TestCase
138140
'expected' => ['languages' => ['en', 'zh']],
139141
'input' => [
140142
0xe1,
141-
/* languages */
143+
// languages
142144
0x49, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61,
143145
0x67, 0x65, 0x73,
144-
/* array */
146+
// array
145147
0x2, 0x4,
146-
/* en */
148+
// en
147149
0x42, 0x65, 0x6e,
148-
/* zh */
150+
// zh
149151
0x42, 0x7a, 0x68,
150152
],
151153
'name' => 'map with array in it',
@@ -166,8 +168,8 @@ private function pointers(): array
166168
['expected' => 524283, 'input' => [0x2f, 0xf7, 0xfb]],
167169
['expected' => 526335, 'input' => [0x2f, 0xff, 0xff]],
168170
['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+
['expected' => \PHP_INT_MAX < 4294967295 ? '2147483647' : 2147483647, 'input' => [0x38, 0x7f, 0xff, 0xff, 0xff]],
172+
['expected' => \PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0x38, 0xff, 0xff, 0xff, 0xff]],
171173
];
172174
}
173175

@@ -205,7 +207,7 @@ private function pointers(): array
205207
*/
206208
private function strings(): array
207209
{
208-
$strings = [
210+
return [
209211
['expected' => '', 'input' => [0x40]],
210212
['expected' => '1', 'input' => [0x41, 0x31]],
211213
['expected' => '', 'input' => [0x43, 0xE4, 0xBA, 0xBA]],
@@ -228,15 +230,15 @@ private function strings(): array
228230
0x36, 0x37, 0x38, 0x39, 0x30, ]],
229231

230232
['expected' => str_repeat('x', 500),
231-
'input' => array_pad(
233+
'input' => array_pad(
232234
[0x5e, 0x0, 0xd7],
233235
503,
234236
0x78
235237
),
236238
],
237239
[
238-
'expected' => str_repeat('x', 2000),
239-
'input' => array_pad(
240+
'expected' => str_repeat('x', 2000),
241+
'input' => array_pad(
240242
[0x5e, 0x6, 0xb3],
241243
2003,
242244
0x78
@@ -251,8 +253,6 @@ private function strings(): array
251253
),
252254
],
253255
];
254-
255-
return $strings;
256256
}
257257

258258
/**
@@ -267,7 +267,7 @@ private function uint32(): array
267267
['expected' => 10872, 'input' => [0xc2, 0x2a, 0x78]],
268268
['expected' => 65535, 'input' => [0xc2, 0xff, 0xff]],
269269
['expected' => 16777215, 'input' => [0xc3, 0xff, 0xff, 0xff]],
270-
['expected' => PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0xc4, 0xff, 0xff, 0xff, 0xff]],
270+
['expected' => \PHP_INT_MAX < 4294967295 ? '4294967295' : 4294967295, 'input' => [0xc4, 0xff, 0xff, 0xff, 0xff]],
271271
];
272272
}
273273

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* @coversNothing
13+
*
14+
* @internal
1315
*/
1416
class MetadataTest extends TestCase
1517
{
@@ -54,7 +56,7 @@ public function testTooManyConstructorArgs(): void
5456
public function testNoConstructorArgs(): void
5557
{
5658
$this->expectException(ArgumentCountError::class);
57-
/** @phpstan-ignore-next-line */
59+
// @phpstan-ignore-next-line
5860
new Metadata();
5961
}
6062
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* @coversNothing
12+
*
13+
* @internal
1214
*/
1315
class PointerTest extends TestCase
1416
{

0 commit comments

Comments
 (0)