-
Notifications
You must be signed in to change notification settings - Fork 0
3634: Improve code quality and increase PHPStan level #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,13 +41,13 @@ class AvroDebug | |
| /** | ||
| * @param string $format format string for the given arguments. Passed as is | ||
| * to <code>vprintf</code>. | ||
| * @param array $args array of arguments to pass to vsprinf. | ||
| * @param int $debug_level debug level at which to print this statement | ||
| * @returns boolean true | ||
| * @param list<string> $args array of arguments to pass to vsprinf. | ||
| * @param int $debugLevel debug level at which to print this statement | ||
| * @return bool true | ||
| */ | ||
| public static function debug($format, $args, $debug_level = self::DEBUG1) | ||
| public static function debug(string $format, array $args, int $debugLevel = self::DEBUG1): bool | ||
| { | ||
| if (self::isDebug($debug_level)) { | ||
| if (self::isDebug($debugLevel)) { | ||
| vprintf($format."\n", $args); | ||
| } | ||
|
|
||
|
|
@@ -59,37 +59,34 @@ public static function debug($format, $args, $debug_level = self::DEBUG1) | |
| * or more verbose than than the current debug level | ||
| * and false otherwise. | ||
| */ | ||
| public static function isDebug(int $debug_level = self::DEBUG1): bool | ||
| public static function isDebug(int $debugLevel = self::DEBUG1): bool | ||
| { | ||
| return self::DEBUG_LEVEL >= $debug_level; | ||
| return self::DEBUG_LEVEL >= $debugLevel; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @param string $joiner string used to join | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * @returns string hex-represented bytes of each byte of $str | ||
| * @return string hex-represented bytes of each byte of $str | ||
| * joined by $joiner | ||
| */ | ||
| public static function hexString($str, $joiner = ' ') | ||
| public static function hexString(string $str, string $joiner = ' '): string | ||
| { | ||
| return implode($joiner, self::hexArray($str)); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @returns string[] array of hex representation of each byte of $str | ||
| * @return string[] array of hex representation of each byte of $str | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function hexArray($str) | ||
| public static function hexArray(string $str): array | ||
| { | ||
| return self::bytesArray($str); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @param string $format format to represent bytes | ||
| * @returns string[] array of each byte of $str formatted using $format | ||
| * @return string[] array of each byte of $str formatted using $format | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function bytesArray($str, $format = 'x%02x') | ||
| public static function bytesArray(string $str, string $format = 'x%02x'): array | ||
| { | ||
| $x = []; | ||
| foreach (str_split($str) as $b) { | ||
|
|
@@ -100,40 +97,35 @@ public static function bytesArray($str, $format = 'x%02x') | |
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @param string $joiner string to join bytes of $str | ||
| * @returns string of bytes of $str represented in decimal format | ||
| * @return string of bytes of $str represented in decimal format | ||
| * @uses decArray() | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function decString($str, $joiner = ' ') | ||
| public static function decString(string $str, string $joiner = ' '): string | ||
| { | ||
| return implode($joiner, self::decArray($str)); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @returns string[] array of bytes of $str represented in decimal format ('%3d') | ||
| * @return string[] array of bytes of $str represented in decimal format ('%3d') | ||
| */ | ||
| public static function decArray($str) | ||
| public static function decArray(string $str): array | ||
| { | ||
| return self::bytesArray($str, '%3d'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| /** | ||
| * @param string $str | ||
| * @param string $format one of 'ctrl', 'hex', or 'dec'. | ||
| * See {@link self::asciiArray()} for more description | ||
| * @param string $joiner | ||
| * @returns string of bytes joined by $joiner | ||
| * @return string of bytes joined by $joiner | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * @uses asciiArray() | ||
| */ | ||
| public static function asciiString($str, $format = 'ctrl', $joiner = ' ') | ||
| public static function asciiString(string $str, string $format = 'ctrl', string $joiner = ' '): string | ||
| { | ||
| return implode($joiner, self::asciiArray($str, $format)); | ||
| } | ||
|
|
||
| /** | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * @param string $str | ||
| * @param string $format one of 'ctrl', 'hex', or 'dec' for control, | ||
| * hexadecimal, or decimal format for bytes. | ||
| * - ctrl: ASCII control characters represented as text. | ||
|
|
@@ -142,16 +134,16 @@ public static function asciiString($str, $format = 'ctrl', $joiner = ' ') | |
| * others are represented as a decimal ('%03d') | ||
| * - hex: bytes represented in hexadecimal ('%02X') | ||
| * - dec: bytes represented in decimal ('%03d') | ||
| * @returns string[] array of bytes represented in the given format. | ||
| * @throws AvroException | ||
| * @return string[] array of bytes represented in the given format. | ||
| */ | ||
| public static function asciiArray($str, $format = 'ctrl') | ||
| public static function asciiArray(string $str, string $format = 'ctrl'): array | ||
| { | ||
| if (!in_array($format, ['ctrl', 'hex', 'dec'])) { | ||
| throw new AvroException('Unrecognized format specifier'); | ||
| } | ||
|
|
||
| $ctrl_chars = [ | ||
| $ctrlChars = [ | ||
| 'NUL', | ||
| 'SOH', | ||
| 'STX', | ||
|
|
@@ -191,15 +183,15 @@ public static function asciiArray($str, $format = 'ctrl') | |
| if ($db < 32) { | ||
| switch ($format) { | ||
| case 'ctrl': | ||
| $x[] = str_pad($ctrl_chars[$db], 3, ' ', STR_PAD_LEFT); | ||
| $x[] = str_pad($ctrlChars[$db], 3, ' ', STR_PAD_LEFT); | ||
|
|
||
| break; | ||
| case 'hex': | ||
| $x[] = sprintf("x%02X", $db); | ||
|
|
||
| break; | ||
| case 'dec': | ||
| $x[] = str_pad($db, 3, '0', STR_PAD_LEFT); | ||
| $x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT); | ||
|
|
||
| break; | ||
| } | ||
|
|
@@ -218,15 +210,15 @@ public static function asciiArray($str, $format = 'ctrl') | |
|
|
||
| break; | ||
| case 'dec': | ||
| $x[] = str_pad($db, 3, '0', STR_PAD_LEFT); | ||
| $x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT); | ||
|
|
||
| break; | ||
| } | ||
| } else { | ||
| if ('hex' === $format) { | ||
| $x[] = sprintf("x%02X", $db); | ||
| } else { | ||
| $x[] = str_pad($db, 3, '0', STR_PAD_LEFT); | ||
| $x[] = str_pad((string) $db, 3, '0', STR_PAD_LEFT); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,33 +32,33 @@ class AvroGMP | |
| /** | ||
| * @var \GMP memoized GMP resource for zero | ||
| */ | ||
| private static $gmp_0; | ||
| private static \GMP $gmp_0; | ||
| /** | ||
| * @var \GMP memoized GMP resource for one (1) | ||
| */ | ||
| private static $gmp_1; | ||
| private static \GMP $gmp_1; | ||
| /** | ||
| * @var \GMP memoized GMP resource for two (2) | ||
| */ | ||
| private static $gmp_2; | ||
| private static \GMP $gmp_2; | ||
| /** | ||
| * @var \GMP memoized GMP resource for 0x7f | ||
| */ | ||
| private static $gmp_0x7f; | ||
| private static \GMP $gmp_0x7f; | ||
| /** | ||
| * @var \GMP memoized GMP resource for 64-bit ~0x7f | ||
| */ | ||
| private static $gmp_n0x7f; | ||
| private static \GMP $gmp_n0x7f; | ||
| /** | ||
| * @var \GMP memoized GMP resource for 64-bits of 1 | ||
| */ | ||
| private static $gmp_0xfs; | ||
| private static \GMP $gmp_0xfs; | ||
|
|
||
| /** | ||
| * @param int|string $n integer (or string representation of integer) to encode | ||
| * @return string $bytes of the long $n encoded per the Avro spec | ||
| */ | ||
| public static function encodeLong($n) | ||
| public static function encodeLong(int|string $n): string | ||
| { | ||
| $g = gmp_init($n); | ||
| $g = gmp_xor( | ||
|
|
@@ -77,11 +77,10 @@ public static function encodeLong($n) | |
|
|
||
| /** | ||
| * @interal Only works up to shift 63 (doesn't wrap bits around). | ||
| * @param int|resource|string $g | ||
| * @param int $shift number of bits to shift left | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| * @returns resource $g shifted left | ||
| * @return \GMP $g shifted left | ||
| */ | ||
| public static function shiftLeft($g, $shift) | ||
| public static function shiftLeft(int|string|\GMP $g, int $shift) | ||
| { | ||
| if (0 == $shift) { | ||
| return $g; | ||
|
|
@@ -104,21 +103,19 @@ public static function shiftLeft($g, $shift) | |
| } | ||
|
|
||
| /** | ||
| * @param \GMP $g resource | ||
| * @return \GMP resource 64-bit two's complement of input. | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function gmpTwosComplement($g) | ||
| public static function gmpTwosComplement(int|string|\GMP $g) | ||
| { | ||
| return gmp_neg(gmp_sub(gmp_pow(self::gmp_2(), 64), $g)); | ||
| } | ||
|
|
||
| /** | ||
| * Arithmetic right shift | ||
| * @param int|resource|string $g | ||
| * @param int $shift number of bits to shift right | ||
| * @returns resource $g shifted right $shift bits | ||
| * @return \GMP $g shifted right $shift bits | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function shiftRight($g, $shift) | ||
| public static function shiftRight(int|string|\GMP $g, int $shift) | ||
| { | ||
| if (0 == $shift) { | ||
| return $g; | ||
|
|
@@ -143,13 +140,11 @@ public static function shiftRight($g, $shift) | |
| return $m; | ||
| } | ||
|
|
||
| // phpcs:enable | ||
|
|
||
| /** | ||
| * @param int[] $bytes array of ascii codes of bytes to decode | ||
| * @return string represenation of decoded long. | ||
| */ | ||
| public static function decodeLongFromArray($bytes) | ||
| public static function decodeLongFromArray(array $bytes): string | ||
| { | ||
| $b = array_shift($bytes); | ||
| $g = gmp_init($b & 0x7F); | ||
|
|
@@ -167,7 +162,7 @@ public static function decodeLongFromArray($bytes) | |
| // phpcs:disable PSR1.Methods.CamelCapsMethodName | ||
|
|
||
| /** | ||
| * @returns \GMP GMP resource for two (2) | ||
| * @return \GMP GMP resource for two (2) | ||
| */ | ||
| private static function gmp_2() | ||
| { | ||
|
|
@@ -179,9 +174,9 @@ private static function gmp_2() | |
| } | ||
|
|
||
| /** | ||
| * @returns resource GMP resource for 64-bits of 1 | ||
| * @return \GMP GMP resource for 64-bits of 1 | ||
| */ | ||
| private static function gmp_0xfs() | ||
| private static function gmp_0xfs(): \GMP | ||
| { | ||
| if (!isset(self::$gmp_0xfs)) { | ||
| self::$gmp_0xfs = gmp_init('0xffffffffffffffff'); | ||
|
|
@@ -191,9 +186,9 @@ private static function gmp_0xfs() | |
| } | ||
|
|
||
| /** | ||
| * @returns resource GMP resource for one (1) | ||
| * @return \GMP GMP resource for one (1) | ||
| */ | ||
| private static function gmp_1() | ||
| private static function gmp_1(): \GMP | ||
| { | ||
| if (!isset(self::$gmp_1)) { | ||
| self::$gmp_1 = gmp_init('1'); | ||
|
|
@@ -203,9 +198,9 @@ private static function gmp_1() | |
| } | ||
|
|
||
| /** | ||
| * @returns resource GMP resource for zero | ||
| * @return \GMP GMP resource for zero | ||
| */ | ||
| private static function gmp_0() | ||
| private static function gmp_0(): \GMP | ||
| { | ||
| if (!isset(self::$gmp_0)) { | ||
| self::$gmp_0 = gmp_init('0'); | ||
|
|
@@ -215,9 +210,9 @@ private static function gmp_0() | |
| } | ||
|
|
||
| /** | ||
| * @returns resource GMP resource for 64-bit ~0x7f | ||
| * @return \GMP GMP resource for 64-bit ~0x7f | ||
| */ | ||
| private static function gmp_n0x7f() | ||
| private static function gmp_n0x7f(): \GMP | ||
| { | ||
| if (!isset(self::$gmp_n0x7f)) { | ||
| self::$gmp_n0x7f = gmp_init('0xffffffffffffff80'); | ||
|
|
@@ -227,9 +222,9 @@ private static function gmp_n0x7f() | |
| } | ||
|
|
||
| /** | ||
| * @returns resource GMP resource for 0x7f | ||
| * @return \GMP GMP resource for 0x7f | ||
| */ | ||
| private static function gmp_0x7f() | ||
| private static function gmp_0x7f(): \GMP | ||
| { | ||
| if (!isset(self::$gmp_0x7f)) { | ||
| self::$gmp_0x7f = gmp_init('0x7f'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the docblock to reference the renamed property.
Line 65 still references
Avro::$biginteger_modebut the property was renamed to$bigIntegerMode.📝 Suggested fix
/** * `@return` bool true if the PHP GMP extension is used and false otherwise. * `@internal` Requires Avro::check64Bit() (exposed via Avro::checkPlatform()) - * to have been called to set Avro::$biginteger_mode. + * to have been called to set Avro::$bigIntegerMode. */📝 Committable suggestion
🤖 Prompt for AI Agents