Skip to content

Commit 06340ce

Browse files
refactor: remove deprecated types in random_string() helper (#9592)
* refactor: remove deprecated types in random_string() helper * Apply suggestions from code review Co-authored-by: John Paul E. Balandan, CPA <[email protected]> * cs fix --------- Co-authored-by: John Paul E. Balandan, CPA <[email protected]>
1 parent 5ff2df0 commit 06340ce

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

system/Helpers/text_helper.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,8 @@ function reduce_multiples(string $str, string $character = ',', bool $trim = fal
545545
*
546546
* Useful for generating passwords or hashes.
547547
*
548-
* @param string $type Type of random string. basic, alpha, alnum, numeric, nozero, md5, sha1, and crypto
548+
* @param string $type Type of random string: alpha, alnum, numeric, nozero, or crypto
549549
* @param int $len Number of characters
550-
*
551-
* @deprecated The type 'basic', 'md5', and 'sha1' are deprecated. They are not cryptographically secure.
552550
*/
553551
function random_string(string $type = 'alnum', int $len = 8): string
554552
{
@@ -578,12 +576,6 @@ function random_string(string $type = 'alnum', int $len = 8): string
578576

579577
return sprintf('%0' . $len . 'd', $rand);
580578

581-
case 'md5':
582-
return md5(uniqid((string) mt_rand(), true));
583-
584-
case 'sha1':
585-
return sha1(uniqid((string) mt_rand(), true));
586-
587579
case 'crypto':
588580
if ($len % 2 !== 0) {
589581
throw new InvalidArgumentException(
@@ -594,8 +586,12 @@ function random_string(string $type = 'alnum', int $len = 8): string
594586
return bin2hex(random_bytes($len / 2));
595587
}
596588

597-
// 'basic' type treated as default
598-
return (string) mt_rand();
589+
throw new InvalidArgumentException(
590+
sprintf(
591+
'Invalid type "%s". Accepted types: alpha, alnum, numeric, nozero, or crypto.',
592+
$type,
593+
),
594+
);
599595
}
600596
}
601597

tests/system/Helpers/TextHelperTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,8 @@ public function testRandomString(): void
130130
$this->assertSame(16, strlen(random_string('numeric', 16)));
131131
$this->assertSame(8, strlen(random_string('numeric')));
132132

133-
$this->assertIsString(random_string('basic'));
134133
$this->assertSame(16, strlen($random = random_string('crypto', 16)));
135134
$this->assertIsString($random);
136-
137-
$this->assertSame(32, strlen($random = random_string('md5')));
138-
$this->assertSame(40, strlen($random = random_string('sha1')));
139135
}
140136

141137
/**
@@ -151,6 +147,16 @@ public function testRandomStringCryptoOddNumber(): void
151147
random_string('crypto', 9);
152148
}
153149

150+
public function testRandomStringWithUnsupportedType(): void
151+
{
152+
$this->expectException(InvalidArgumentException::class);
153+
$this->expectExceptionMessage(
154+
'Invalid type "basic". Accepted types: alpha, alnum, numeric, nozero, or crypto.',
155+
);
156+
157+
random_string('basic');
158+
}
159+
154160
public function testIncrementString(): void
155161
{
156162
$this->assertSame('my-test_1', increment_string('my-test'));

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Interface Changes
3131
Method Signature Changes
3232
========================
3333

34+
Removed Deprecated Items
35+
========================
36+
37+
- **Text Helper:** The deprecated types in ``random_string()`` function: ``basic``, ``md5``, and ``sha1`` has been removed.
38+
3439
************
3540
Enhancements
3641
************

user_guide_src/source/helpers/text_helper.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,13 @@ The following functions are available:
3030
Generates a random string based on the type and length you specify.
3131
Useful for creating passwords or generating random hashes.
3232

33-
.. warning:: For types: **basic**, **md5**, and **sha1**, generated strings
34-
are not cryptographically secure. Therefore, these types cannot be used
35-
for cryptographic purposes or purposes requiring unguessable return values.
36-
Since v4.3.3, these types are deprecated.
37-
3833
The first parameter specifies the type of string, the second parameter
3934
specifies the length. The following choices are available:
4035

4136
- **alpha**: A string with lower and uppercase letters only.
4237
- **alnum**: Alphanumeric string with lower and uppercase characters.
43-
- **basic**: [deprecated] A random number based on ``mt_rand()`` (length ignored).
4438
- **numeric**: Numeric string.
4539
- **nozero**: Numeric string with no zeros.
46-
- **md5**: [deprecated] An encrypted random number based on ``md5()`` (fixed length of 32).
47-
- **sha1**: [deprecated] An encrypted random number based on ``sha1()`` (fixed length of 40).
4840
- **crypto**: A random string based on ``random_bytes()``.
4941

5042
.. note:: When you use **crypto**, you must set an even number to the second parameter.

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3057 errors
1+
# total 3056 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/method.alreadyNarrowedType.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 24 errors
1+
# total 23 errors
22

33
parameters:
44
ignoreErrors:
@@ -59,7 +59,7 @@ parameters:
5959

6060
-
6161
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertIsString\(\) with string will always evaluate to true\.$#'
62-
count: 2
62+
count: 1
6363
path: ../../tests/system/Helpers/TextHelperTest.php
6464

6565
-

0 commit comments

Comments
 (0)