diff --git a/lib/byte_safe_strings.php b/lib/byte_safe_strings.php index 3feaeda..ef24488 100644 --- a/lib/byte_safe_strings.php +++ b/lib/byte_safe_strings.php @@ -94,7 +94,7 @@ function RandomCompat_strlen($binary_string) * * @param string $binary_string * @param int $start - * @param int $length (optional) + * @param int|null $length (optional) * * @throws TypeError * @@ -119,6 +119,7 @@ function RandomCompat_substr($binary_string, $start, $length = null) * mb_substr($str, 0, NULL, '8bit') returns an empty string on * PHP 5.3, so we have to find the length ourselves. */ + /** @var int $length */ $length = RandomCompat_strlen($binary_string) - $start; } elseif (!is_int($length)) { throw new TypeError( @@ -134,7 +135,12 @@ function RandomCompat_substr($binary_string, $start, $length = null) return ''; } - return (string) mb_substr($binary_string, $start, $length, '8bit'); + return (string) mb_substr( + (string) $binary_string, + (int) $start, + (int) $length, + '8bit' + ); } } else { @@ -146,7 +152,7 @@ function RandomCompat_substr($binary_string, $start, $length = null) * * @param string $binary_string * @param int $start - * @param int $length (optional) + * @param int|null $length (optional) * * @throws TypeError * @@ -173,10 +179,17 @@ function RandomCompat_substr($binary_string, $start, $length = null) ); } - return (string) substr($binary_string, $start, $length); + return (string) substr( + (string )$binary_string, + (int) $start, + (int) $length + ); } - return (string) substr($binary_string, $start); + return (string) substr( + (string) $binary_string, + (int) $start + ); } } } diff --git a/lib/cast_to_int.php b/lib/cast_to_int.php index ad6e9c2..f97e434 100644 --- a/lib/cast_to_int.php +++ b/lib/cast_to_int.php @@ -53,12 +53,13 @@ function RandomCompat_intval($number, $fail_open = false) /** @psalm-suppress InvalidOperand */ $number += 0; } + /** @var int|float $number */ if ( is_float($number) - && + && $number > ~PHP_INT_MAX - && + && $number < PHP_INT_MAX ) { $number = (int) $number;