Skip to content

Expand String functions#77

Open
TheSyscall wants to merge 8 commits into
mainfrom
str-isempty
Open

Expand String functions#77
TheSyscall wants to merge 8 commits into
mainfrom
str-isempty

Conversation

@TheSyscall
Copy link
Copy Markdown

@TheSyscall TheSyscall commented May 27, 2026

Add some static methods to the Str class.

  • endsWith as the counterpart to the existing startsWith
  • contains to have a case independent version of str_contains
  • containsAny and containsAll to quickly compare against an array of strings
  • isEmpty to reliably check for empty strings

@cla-bot cla-bot Bot added the cla/signed label May 27, 2026
@TheSyscall TheSyscall changed the title Str Expand String functions May 27, 2026
Al2Klimov

This comment was marked as resolved.

Copy link
Copy Markdown
Member

@Al2Klimov Al2Klimov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hand over to the next reviewer ASAP!

@yhabteab
Copy link
Copy Markdown
Member

yhabteab commented Jun 1, 2026

Hi, I don't think, I can give any meaningful reviews on this, thus, it would probably be better to request a review from @nilmerg instead, if Eric is not available.

Copy link
Copy Markdown
Member

@lippserd lippserd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing to consider: This caseSensitive parameter construct is something that I would do differently nowadays. Passing false without using a named argument requires the reader/caller to read the docs. It unnecessarily bloats the contract. And it requires two different implementation blocks in each function. Consider introducing …Insensitive() variants instead.

Comment thread src/Str.php
{
$subject ??= '';
if (! $caseSensitive) {
return strncasecmp($subject, $end, strlen($end)) === 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a prefix check but it should be a suffix check. Tests don’t exercise the contract properly. You always pass true, which is the default.

Comment thread src/Str.php
* Check if the given string contains any of the specified substrings
*
* @param ?string $haystack
* @param string[] $needles
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use iterable to support any string iterator/generator.

Comment thread src/Str.php
* Check if the given string contains all the specified substrings
*
* @param ?string $haystack
* @param string[] $needles
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same iterable consideration as above.

Comment thread src/Str.php
* Null is considered empty and strings are trimmed before checking.
*
* @param ?string $subject
* @param string $characters
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEmpty should not expose implementation details. Its contract is that it considers null or whitespace only strings as empty. That’s it. Calling trim must be replaced anyway if we want this function to also work with UTF-8 inputs.

Comment thread src/Str.php
*/
public static function contains(?string $subject, string $needle, bool $caseSensitive = true): bool
{
$subject ??= '';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit early instead.

Comment thread src/Str.php
*/
public static function endsWith(?string $subject, string $end, bool $caseSensitive = true): bool
{
$subject ??= '';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit early instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants