Skip to content

Commit c3e8a20

Browse files
committed
Fix PHPStan errors for GitHub Actions CI
- Add parameter validation in random_bytes() calls (src/Str.php) - Replace @throws Random\RandomException with Exception for PHP 8.0/8.1 compatibility - Remove unmatched ignored error from phpstan.neon
1 parent e3fa525 commit c3e8a20

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

phpstan.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,3 @@ parameters:
2626
excludePaths:
2727
- vendor
2828
- tests
29-
30-
# Custom rules
31-
ignoreErrors:
32-
# Allow mixed types where necessary
33-
- '#Parameter \#1 \$array of static method Farzai\\Support\\Arr::get\(\) expects array|ArrayAccess, mixed given#'

src/Str.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public static function isStudlyCase(string $value): bool
244244
* @param int $length The desired length of the random string
245245
* @return string A random string of the specified length
246246
*
247-
* @throws \Random\RandomException If random_bytes() fails
247+
* @throws \Exception If random_bytes() fails
248248
*
249249
* @example
250250
* Str::random(16); // Returns: 'a3K7mN9pQ1xY2zB5'
@@ -255,7 +255,7 @@ public static function random(int $length = 16): string
255255

256256
while (static::length($string) < $length) {
257257
$remaining = $length - static::length($string);
258-
$bytes = random_bytes($remaining);
258+
$bytes = random_bytes(max(1, $remaining));
259259
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $remaining);
260260
}
261261

@@ -268,14 +268,14 @@ public static function random(int $length = 16): string
268268
* @param int $length The desired length of the random string
269269
* @return string A random ASCII string of the specified length
270270
*
271-
* @throws \Random\RandomException If random_bytes() fails
271+
* @throws \Exception If random_bytes() fails
272272
*
273273
* @example
274274
* Str::randomAscii(16); // Returns: 'a3K7mN9pQ1xY2zB5'
275275
*/
276276
public static function randomAscii(int $length = 16): string
277277
{
278-
return static::substr(str_replace(['/', '+', '='], '', base64_encode(random_bytes($length))), 0, $length);
278+
return static::substr(str_replace(['/', '+', '='], '', base64_encode(random_bytes(max(1, $length)))), 0, $length);
279279
}
280280

281281
/**
@@ -284,7 +284,7 @@ public static function randomAscii(int $length = 16): string
284284
* @param int $length The desired length of the random string
285285
* @return string A random numeric string of the specified length
286286
*
287-
* @throws \Random\RandomException If random_int() fails
287+
* @throws \Exception If random_int() fails
288288
*
289289
* @example
290290
* Str::randomNumeric(6); // Returns: '472891'
@@ -308,7 +308,7 @@ public static function randomNumeric(int $length = 16): string
308308
* @param int $length The desired length of the random string
309309
* @return string A random alphanumeric string of the specified length
310310
*
311-
* @throws \Random\RandomException If random_int() fails
311+
* @throws \Exception If random_int() fails
312312
*
313313
* @example
314314
* Str::randomAlphanumeric(12); // Returns: 'aB3xY9mK2nP7'
@@ -325,7 +325,7 @@ public static function randomAlphanumeric(int $length = 16): string
325325
* @param string|null $characters The character set to use (defaults to alphanumeric)
326326
* @return string A random string of the specified length from the character set
327327
*
328-
* @throws \Random\RandomException If random_int() fails
328+
* @throws \Exception If random_int() fails
329329
*
330330
* @example
331331
* Str::randomString(8, 'ABCD123'); // Returns: 'A2B1C3D2'
@@ -350,7 +350,7 @@ public static function randomString(int $length = 16, ?string $characters = null
350350
* @param int $length The desired length of the random string
351351
* @return string A random alphanumeric string of the specified length
352352
*
353-
* @throws \Random\RandomException If random_int() fails
353+
* @throws \Exception If random_int() fails
354354
*
355355
* @example
356356
* Str::randomStringWithNumeric(10); // Returns: 'aB3xY9mK2n'
@@ -369,7 +369,7 @@ public static function randomStringWithNumeric(int $length = 16): string
369369
* @param int $length The desired length of the random string
370370
* @return string A random string with special characters
371371
*
372-
* @throws \Random\RandomException If random_int() fails
372+
* @throws \Exception If random_int() fails
373373
*
374374
* @example
375375
* Str::randomStringWithSpecialCharacter(12); // Returns: 'aB3!xY@9#mK2'

0 commit comments

Comments
 (0)