-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Add nullish check methods to Str facade
#57599
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
[12.x] Add nullish check methods to Str facade
#57599
Conversation
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.
Honestly, I'm not sure if it brings that much benefit to spare "lazy" (no offense) devs from having to write two(!) conditions. The trim() part, though, might be helpful in some cases. The problem I see here, might be that it motivates people to add all kinds of other checks and permutations of it.
I see your concern, but you could apply this same logic to a lot of |
chore: style cleanup chore: formatting chore: cleanup, use static chore: cleanup
6bcac2e to
c2be436
Compare
| $this->assertSame('UserGroups', Str::pluralPascal('UserGroup', $countable)); | ||
| } | ||
|
|
||
| #[DataProvider('nullOrEmptyProvider')] |
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.
Thanks for using data providers—that makes the test output so much better to read and debug 🙏
I get your point—to a certain extend. But the decision to include case change methods into the I'm not saying that I particularly like having all these very distinct sets of functions in one single facade (#52979). |
I agree that casing methods justify themselves due to multibyte considerations, but I’d argue that correctness alone shouldn’t be the only metric for inclusion. Consistency and reduced cognitive load matter just as much, especially in larger teams. These additions aren’t about avoiding two lines of code; they’re about expressing intent clearly across a codebase of many developers. When I see |
shaedrich
left a comment
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.
When I see
Str::nullOrWhitespace(), I instantly understand what it checks without having to interpret logic
When I see the actual code, I can be sure what it does; when I see a method call, I can only speculate about the implementation when I don't look it up. And Laravel already has some places where methods break the principle of least astonishment by not actually doing what you would expect them to by their name.
| */ | ||
| public static function nullOrWhitespace($string) | ||
| { | ||
| return $string === null || trim($string) === ''; |
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.
Speaking about multi-byte support, maybe, using mb_trim() would be safer here
| return $string === null || trim($string) === ''; | |
| return $string === null || mb_trim($string) === ''; |
| */ | ||
| public static function nullOrWhitespace($string) | ||
| { | ||
| return $string === null || trim($string) === ''; |
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.
Has it been a conscious decision to not support modifying the second parameter to trim(), $characters?
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions! |
In an ode to .NET, the
string.IsNullOrEmpty()andstring.IsNullOrWhitespace()functions I've always found helpful and thought they could be useful for the masses using Laravel. I usually add these to my own Laravel projects in some for of helper method, but thought it was be nice to have these officially as part of theStrfacade.I find it fairly useful for high PHPStan levels that enforce strict null checks/empty checks for strings rather than truthy/falsy assertions. For those of us weirdos using it near max level, we could centralize those checks to the
Strfacade rather than the various flavors ofis_null(),empty(),=== null,=== '', etc.