From c2be436bb47e0ce4ea8400e0a38d2b665df1e51d Mon Sep 17 00:00:00 2001 From: Joey McKenzie Date: Thu, 30 Oct 2025 12:09:56 -0700 Subject: [PATCH 1/2] feat: add nullish methods to Str::class facade chore: style cleanup chore: formatting chore: cleanup, use static chore: cleanup --- src/Illuminate/Support/Str.php | 44 +++++++++++++ tests/Support/SupportStrTest.php | 108 +++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 667c58681ff1..4559f936c789 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -2100,4 +2100,48 @@ public static function flushCache() static::$camelCache = []; static::$studlyCache = []; } + + /** + * Determines if a string is null or contains no characters. + * + * @param string|null $string + * @return bool + */ + public static function nullOrEmpty($string) + { + return $string === null || $string === ''; + } + + /** + * Determines if a string is null or contains only whitespace characters. + * + * @param string|null $string + * @return bool + */ + public static function nullOrWhitespace($string) + { + return static::nullOrEmpty($string) || trim($string) === ''; + } + + /** + * Determines if a string is not null and contains characters. + * + * @param string|null $string + * @return bool + */ + public static function notNullOrEmpty($string) + { + return ! static::nullOrEmpty($string); + } + + /** + * Determines if a string is not null and does not contain only whitespace characters. + * + * @param string|null $string + * @return bool + */ + public static function notNullOrWhitespace($string) + { + return ! static::nullOrWhitespace($string); + } } diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 0e9b00e0cbfe..339f0bc07042 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -1905,6 +1905,114 @@ public function count(): int $this->assertSame('UserGroups', Str::pluralPascal('UserGroup', $countable)); } + + #[DataProvider('nullOrEmptyProvider')] + public function testNullOrEmpty($value, $expected) + { + $this->assertEquals($expected, Str::nullOrEmpty($value)); + } + + #[DataProvider('notNullOrEmptyProvider')] + public function testNotNullOrEmpty($value, $expected) + { + $this->assertEquals($expected, Str::notNullOrEmpty($value)); + } + + #[DataProvider('nullOrWhitespaceProvider')] + public function testNullOrWhitespace($value, $expected) + { + $this->assertEquals($expected, Str::nullOrWhitespace($value)); + } + + #[DataProvider('notNullOrWhitespaceProvider')] + public function testNotNullOrWhitespace($value, $expected) + { + $this->assertEquals($expected, Str::notNullOrWhitespace($value)); + } + + public static function nullOrEmptyProvider() + { + return [ + 'null value' => [null, true], + 'empty string' => ['', true], + 'single space' => [' ', false], + 'multiple spaces' => [' ', false], + 'tab character' => ["\t", false], + 'newline character' => ["\n", false], + 'zero string' => ['0', false], + 'simple text' => ['hello', false], + 'text with spaces' => ['hello world', false], + 'single character' => ['a', false], + 'number string' => ['123', false], + 'special characters' => ['!@#$', false], + ]; + } + + public static function notNullOrEmptyProvider() + { + return [ + 'null value' => [null, false], + 'empty string' => ['', false], + 'single space' => [' ', true], + 'multiple spaces' => [' ', true], + 'tab character' => ["\t", true], + 'newline character' => ["\n", true], + 'zero string' => ['0', true], + 'simple text' => ['hello', true], + 'text with spaces' => ['hello world', true], + 'single character' => ['a', true], + 'number string' => ['123', true], + 'special characters' => ['!@#$', true], + ]; + } + + public static function nullOrWhitespaceProvider() + { + return [ + 'null value' => [null, true], + 'empty string' => ['', true], + 'single space' => [' ', true], + 'multiple spaces' => [' ', true], + 'tab character' => ["\t", true], + 'newline character' => ["\n", true], + 'carriage return' => ["\r", true], + 'mixed whitespace' => [" \t\n\r ", true], + 'zero string' => ['0', false], + 'simple text' => ['hello', false], + 'text with spaces' => ['hello world', false], + 'text with leading space' => [' hello', false], + 'text with trailing space' => ['hello ', false], + 'text with surrounding spaces' => [' hello ', false], + 'single character' => ['a', false], + 'number string' => ['123', false], + 'special characters' => ['!@#$', false], + 'whitespace with period' => [' . ', false], + ]; + } + + public static function notNullOrWhitespaceProvider() + { + return [ + 'null value' => [null, false], + 'empty string' => ['', false], + 'single space' => [' ', false], + 'multiple spaces' => [' ', false], + 'tab character' => ["\t", false], + 'newline character' => ["\n", false], + 'carriage return' => ["\r", false], + 'mixed whitespace' => [" \t\n\r ", false], + 'zero string' => ['0', true], + 'simple text' => ['hello', true], + 'text with spaces' => ['hello world', true], + 'text with leading space' => [' hello', true], + 'text with trailing space' => ['hello ', true], + 'text with surrounding spaces' => [' hello ', true], + 'single character' => ['a', true], + 'number string' => ['123', true], + 'special characters' => ['!@#$', true], + 'whitespace with period' => [' . ', true], + ]; + } } class StringableObjectStub From 6c178ac126d0e09262f104712a6d0ba4dc0c7dac Mon Sep 17 00:00:00 2001 From: Joey McKenzie Date: Fri, 31 Oct 2025 07:57:09 -0700 Subject: [PATCH 2/2] refactor: check for null empty explicitly --- src/Illuminate/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 4559f936c789..592f5c917f51 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -2120,7 +2120,7 @@ public static function nullOrEmpty($string) */ public static function nullOrWhitespace($string) { - return static::nullOrEmpty($string) || trim($string) === ''; + return $string === null || trim($string) === ''; } /**