Skip to content

Commit c499eef

Browse files
authored
Improve code quality and increase PHPStan level (#3634)
* improve code quality and increase phpstan level * update exception messages and fixes check for bz2 * review, round one * fix bug in AvroStringIO.php * fix check on primitive type in AvroIODatumReader.php
1 parent d6ecea3 commit c499eef

34 files changed

Lines changed: 908 additions & 620 deletions

lang/php/lib/Avro.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,42 @@ class Avro
4949
* self::GMP_BIGINTEGER_MODE on 32-bit platforms that have GMP available,
5050
* and to self::PHP_BIGINTEGER_MODE otherwise.
5151
*/
52-
private static int $biginteger_mode;
52+
private static int $bigIntegerMode;
5353

5454
/**
5555
* Wrapper method to call each required check.
5656
*/
57-
public static function checkPlatform()
57+
public static function checkPlatform(): void
5858
{
5959
self::check64Bit();
6060
}
6161

6262
/**
63-
* @returns bool true if the PHP GMP extension is used and false otherwise.
63+
* @return bool true if the PHP GMP extension is used and false otherwise.
6464
* @internal Requires Avro::check64Bit() (exposed via Avro::checkPlatform())
65-
* to have been called to set Avro::$biginteger_mode.
65+
* to have been called to set Avro::$bigIntegerMode.
6666
*/
6767
public static function usesGmp(): bool
6868
{
69-
return self::GMP_BIGINTEGER_MODE === self::$biginteger_mode;
69+
return self::GMP_BIGINTEGER_MODE === self::$bigIntegerMode;
7070
}
7171

7272
/**
7373
* Determines if the host platform can encode and decode long integer data.
7474
*
7575
* @throws AvroException if the platform cannot handle long integers.
7676
*/
77-
private static function check64Bit()
77+
private static function check64Bit(): void
7878
{
7979
if (8 !== PHP_INT_SIZE) {
8080
if (extension_loaded('gmp')) {
81-
self::$biginteger_mode = self::GMP_BIGINTEGER_MODE;
81+
self::$bigIntegerMode = self::GMP_BIGINTEGER_MODE;
8282
} else {
8383
throw new AvroException('This platform cannot handle a 64-bit operations. '
8484
.'Please install the GMP PHP extension.');
8585
}
8686
} else {
87-
self::$biginteger_mode = self::PHP_BIGINTEGER_MODE;
87+
self::$bigIntegerMode = self::PHP_BIGINTEGER_MODE;
8888
}
8989
}
9090
}

lang/php/lib/AvroDebug.php

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class AvroDebug
4141
/**
4242
* @param string $format format string for the given arguments. Passed as is
4343
* to <code>vprintf</code>.
44-
* @param array $args array of arguments to pass to vsprinf.
45-
* @param int $debug_level debug level at which to print this statement
46-
* @returns boolean true
44+
* @param list<string> $args array of arguments to pass to vsprinf.
45+
* @param int $debugLevel debug level at which to print this statement
46+
* @return bool true
4747
*/
48-
public static function debug($format, $args, $debug_level = self::DEBUG1)
48+
public static function debug(string $format, array $args, int $debugLevel = self::DEBUG1): bool
4949
{
50-
if (self::isDebug($debug_level)) {
50+
if (self::isDebug($debugLevel)) {
5151
vprintf($format."\n", $args);
5252
}
5353

@@ -59,37 +59,34 @@ public static function debug($format, $args, $debug_level = self::DEBUG1)
5959
* or more verbose than than the current debug level
6060
* and false otherwise.
6161
*/
62-
public static function isDebug(int $debug_level = self::DEBUG1): bool
62+
public static function isDebug(int $debugLevel = self::DEBUG1): bool
6363
{
64-
return self::DEBUG_LEVEL >= $debug_level;
64+
return self::DEBUG_LEVEL >= $debugLevel;
6565
}
6666

6767
/**
68-
* @param string $str
6968
* @param string $joiner string used to join
70-
* @returns string hex-represented bytes of each byte of $str
69+
* @return string hex-represented bytes of each byte of $str
7170
* joined by $joiner
7271
*/
73-
public static function hexString($str, $joiner = ' ')
72+
public static function hexString(string $str, string $joiner = ' '): string
7473
{
7574
return implode($joiner, self::hexArray($str));
7675
}
7776

7877
/**
79-
* @param string $str
80-
* @returns string[] array of hex representation of each byte of $str
78+
* @return string[] array of hex representation of each byte of $str
8179
*/
82-
public static function hexArray($str)
80+
public static function hexArray(string $str): array
8381
{
8482
return self::bytesArray($str);
8583
}
8684

8785
/**
88-
* @param string $str
8986
* @param string $format format to represent bytes
90-
* @returns string[] array of each byte of $str formatted using $format
87+
* @return string[] array of each byte of $str formatted using $format
9188
*/
92-
public static function bytesArray($str, $format = 'x%02x')
89+
public static function bytesArray(string $str, string $format = 'x%02x'): array
9390
{
9491
$x = [];
9592
foreach (str_split($str) as $b) {
@@ -100,40 +97,36 @@ public static function bytesArray($str, $format = 'x%02x')
10097
}
10198

10299
/**
103-
* @param string $str
104100
* @param string $joiner string to join bytes of $str
105-
* @returns string of bytes of $str represented in decimal format
101+
* @return string of bytes of $str represented in decimal format
106102
* @uses decArray()
107103
*/
108-
public static function decString($str, $joiner = ' ')
104+
public static function decString(string $str, string $joiner = ' '): string
109105
{
110106
return implode($joiner, self::decArray($str));
111107
}
112108

113109
/**
114-
* @param string $str
115-
* @returns string[] array of bytes of $str represented in decimal format ('%3d')
110+
* @return string[] array of bytes of $str represented in decimal format ('%3d')
116111
*/
117-
public static function decArray($str)
112+
public static function decArray(string $str): array
118113
{
119114
return self::bytesArray($str, '%3d');
120115
}
121116

122117
/**
123-
* @param string $str
124118
* @param string $format one of 'ctrl', 'hex', or 'dec'.
125119
* See {@link self::asciiArray()} for more description
126-
* @param string $joiner
127-
* @returns string of bytes joined by $joiner
120+
* @throws AvroException
121+
* @return string of bytes joined by $joiner
128122
* @uses asciiArray()
129123
*/
130-
public static function asciiString($str, $format = 'ctrl', $joiner = ' ')
124+
public static function asciiString(string $str, string $format = 'ctrl', string $joiner = ' '): string
131125
{
132126
return implode($joiner, self::asciiArray($str, $format));
133127
}
134128

135129
/**
136-
* @param string $str
137130
* @param string $format one of 'ctrl', 'hex', or 'dec' for control,
138131
* hexadecimal, or decimal format for bytes.
139132
* - ctrl: ASCII control characters represented as text.
@@ -142,16 +135,16 @@ public static function asciiString($str, $format = 'ctrl', $joiner = ' ')
142135
* others are represented as a decimal ('%03d')
143136
* - hex: bytes represented in hexadecimal ('%02X')
144137
* - dec: bytes represented in decimal ('%03d')
145-
* @returns string[] array of bytes represented in the given format.
146138
* @throws AvroException
139+
* @return string[] array of bytes represented in the given format.
147140
*/
148-
public static function asciiArray($str, $format = 'ctrl')
141+
public static function asciiArray(string $str, string $format = 'ctrl'): array
149142
{
150143
if (!in_array($format, ['ctrl', 'hex', 'dec'])) {
151144
throw new AvroException('Unrecognized format specifier');
152145
}
153146

154-
$ctrl_chars = [
147+
$ctrlChars = [
155148
'NUL',
156149
'SOH',
157150
'STX',
@@ -191,15 +184,15 @@ public static function asciiArray($str, $format = 'ctrl')
191184
if ($db < 32) {
192185
switch ($format) {
193186
case 'ctrl':
194-
$x[] = str_pad($ctrl_chars[$db], 3, ' ', STR_PAD_LEFT);
187+
$x[] = str_pad($ctrlChars[$db], 3, ' ', STR_PAD_LEFT);
195188

196189
break;
197190
case 'hex':
198191
$x[] = sprintf("x%02X", $db);
199192

200193
break;
201194
case 'dec':
202-
$x[] = str_pad($db, 3, '0', STR_PAD_LEFT);
195+
$x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT);
203196

204197
break;
205198
}
@@ -218,15 +211,15 @@ public static function asciiArray($str, $format = 'ctrl')
218211

219212
break;
220213
case 'dec':
221-
$x[] = str_pad($db, 3, '0', STR_PAD_LEFT);
214+
$x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT);
222215

223216
break;
224217
}
225218
} else {
226219
if ('hex' === $format) {
227220
$x[] = sprintf("x%02X", $db);
228221
} else {
229-
$x[] = str_pad($db, 3, '0', STR_PAD_LEFT);
222+
$x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT);
230223
}
231224
}
232225
}

lang/php/lib/AvroGMP.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,33 @@ class AvroGMP
3232
/**
3333
* @var \GMP memoized GMP resource for zero
3434
*/
35-
private static $gmp_0;
35+
private static \GMP $gmp_0;
3636
/**
3737
* @var \GMP memoized GMP resource for one (1)
3838
*/
39-
private static $gmp_1;
39+
private static \GMP $gmp_1;
4040
/**
4141
* @var \GMP memoized GMP resource for two (2)
4242
*/
43-
private static $gmp_2;
43+
private static \GMP $gmp_2;
4444
/**
4545
* @var \GMP memoized GMP resource for 0x7f
4646
*/
47-
private static $gmp_0x7f;
47+
private static \GMP $gmp_0x7f;
4848
/**
4949
* @var \GMP memoized GMP resource for 64-bit ~0x7f
5050
*/
51-
private static $gmp_n0x7f;
51+
private static \GMP $gmp_n0x7f;
5252
/**
5353
* @var \GMP memoized GMP resource for 64-bits of 1
5454
*/
55-
private static $gmp_0xfs;
55+
private static \GMP $gmp_0xfs;
5656

5757
/**
5858
* @param int|string $n integer (or string representation of integer) to encode
5959
* @return string $bytes of the long $n encoded per the Avro spec
6060
*/
61-
public static function encodeLong($n)
61+
public static function encodeLong(int|string $n): string
6262
{
6363
$g = gmp_init($n);
6464
$g = gmp_xor(
@@ -77,11 +77,10 @@ public static function encodeLong($n)
7777

7878
/**
7979
* @interal Only works up to shift 63 (doesn't wrap bits around).
80-
* @param int|resource|string $g
8180
* @param int $shift number of bits to shift left
82-
* @returns resource $g shifted left
81+
* @return \GMP $g shifted left
8382
*/
84-
public static function shiftLeft($g, $shift)
83+
public static function shiftLeft(int|string|\GMP $g, int $shift)
8584
{
8685
if (0 == $shift) {
8786
return $g;
@@ -104,21 +103,19 @@ public static function shiftLeft($g, $shift)
104103
}
105104

106105
/**
107-
* @param \GMP $g resource
108106
* @return \GMP resource 64-bit two's complement of input.
109107
*/
110-
public static function gmpTwosComplement($g)
108+
public static function gmpTwosComplement(int|string|\GMP $g)
111109
{
112110
return gmp_neg(gmp_sub(gmp_pow(self::gmp_2(), 64), $g));
113111
}
114112

115113
/**
116114
* Arithmetic right shift
117-
* @param int|resource|string $g
118115
* @param int $shift number of bits to shift right
119-
* @returns resource $g shifted right $shift bits
116+
* @return \GMP $g shifted right $shift bits
120117
*/
121-
public static function shiftRight($g, $shift)
118+
public static function shiftRight(int|string|\GMP $g, int $shift)
122119
{
123120
if (0 == $shift) {
124121
return $g;
@@ -143,13 +140,11 @@ public static function shiftRight($g, $shift)
143140
return $m;
144141
}
145142

146-
// phpcs:enable
147-
148143
/**
149144
* @param int[] $bytes array of ascii codes of bytes to decode
150145
* @return string represenation of decoded long.
151146
*/
152-
public static function decodeLongFromArray($bytes)
147+
public static function decodeLongFromArray(array $bytes): string
153148
{
154149
$b = array_shift($bytes);
155150
$g = gmp_init($b & 0x7F);
@@ -167,7 +162,7 @@ public static function decodeLongFromArray($bytes)
167162
// phpcs:disable PSR1.Methods.CamelCapsMethodName
168163

169164
/**
170-
* @returns \GMP GMP resource for two (2)
165+
* @return \GMP GMP resource for two (2)
171166
*/
172167
private static function gmp_2()
173168
{
@@ -179,9 +174,9 @@ private static function gmp_2()
179174
}
180175

181176
/**
182-
* @returns resource GMP resource for 64-bits of 1
177+
* @return \GMP GMP resource for 64-bits of 1
183178
*/
184-
private static function gmp_0xfs()
179+
private static function gmp_0xfs(): \GMP
185180
{
186181
if (!isset(self::$gmp_0xfs)) {
187182
self::$gmp_0xfs = gmp_init('0xffffffffffffffff');
@@ -191,9 +186,9 @@ private static function gmp_0xfs()
191186
}
192187

193188
/**
194-
* @returns resource GMP resource for one (1)
189+
* @return \GMP GMP resource for one (1)
195190
*/
196-
private static function gmp_1()
191+
private static function gmp_1(): \GMP
197192
{
198193
if (!isset(self::$gmp_1)) {
199194
self::$gmp_1 = gmp_init('1');
@@ -203,9 +198,9 @@ private static function gmp_1()
203198
}
204199

205200
/**
206-
* @returns resource GMP resource for zero
201+
* @return \GMP GMP resource for zero
207202
*/
208-
private static function gmp_0()
203+
private static function gmp_0(): \GMP
209204
{
210205
if (!isset(self::$gmp_0)) {
211206
self::$gmp_0 = gmp_init('0');
@@ -215,9 +210,9 @@ private static function gmp_0()
215210
}
216211

217212
/**
218-
* @returns resource GMP resource for 64-bit ~0x7f
213+
* @return \GMP GMP resource for 64-bit ~0x7f
219214
*/
220-
private static function gmp_n0x7f()
215+
private static function gmp_n0x7f(): \GMP
221216
{
222217
if (!isset(self::$gmp_n0x7f)) {
223218
self::$gmp_n0x7f = gmp_init('0xffffffffffffff80');
@@ -227,9 +222,9 @@ private static function gmp_n0x7f()
227222
}
228223

229224
/**
230-
* @returns resource GMP resource for 0x7f
225+
* @return \GMP GMP resource for 0x7f
231226
*/
232-
private static function gmp_0x7f()
227+
private static function gmp_0x7f(): \GMP
233228
{
234229
if (!isset(self::$gmp_0x7f)) {
235230
self::$gmp_0x7f = gmp_init('0x7f');

0 commit comments

Comments
 (0)