Skip to content

[mbstring][PHP 8.4] Add mb_ucfirst and mb_lcfirst to polyfills #466

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

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

Ayesh
Copy link
Contributor

@Ayesh Ayesh commented Mar 9, 2024

Adds polyfills for mb_ucfirst and mb_lcfirst functions based on the polyfill shown in PHP.Watch. It basically takes the first mb character, calls mb_convert_case with MB_CASE_TITLE or MB_CASE_LOWER, and returns with the concat of the remaining string.

The tests are taken from the php-src PR.

Comment on lines +877 to +891
}

try {
$validEncoding = @self::mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}

// BC for PHP 7.3 and lower
if (!$validEncoding) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

self::mb_internal_encoding() performs validation checks when setting internal values, so adding a try-catch to the else avoids a small amount of overhead. What do you think?

Suggested change
}
try {
$validEncoding = @self::mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}
// BC for PHP 7.3 and lower
if (!$validEncoding) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}
} else {
try {
$validEncoding = @self::mb_check_encoding('', $encoding);
} catch (\ValueError $e) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}
// BC for PHP 7.3 and lower
if (!$validEncoding) {
throw new \ValueError(sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding));
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right I understand the else block would avoid the non-null calls.i tried to follow the rest of the polyfill methods, so I'd prefer to keep this one like it is now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this code can be condensed by combining it into a method like self::assertEncoding(). Let's try another PR.

Adds polyfills for `mb_ucfirst` and `mb_lcfirst` functions based on
the polyfill shown in PHP.Watch. It basically takes the first mb
character, calls `mb_convert_case` with `MB_CASE_TITLE` or
`MB_CASE_LOWER`, and returns with the concat of the remaining string.

The tests are taken from the php-src PR.

 - [RFC](https://wiki.php.net/rfc/mb_ucfirst)
 - [php-src PR: php-src#13161](php/php-src#13161)
 - [PHP.Watch - PHP 8.4: New `mb_ucfirst` and `mb_lcfirst` functions](https://php.watch/versions/8.4/mb_ucfirst-mb_ucfirst)

Co-authored-by: USAMI Kenta <tadsan@zonu.me>
@Ayesh Ayesh force-pushed the php84/mb-ucfirst-lcfirst branch from dc01ec2 to 5bd65b7 Compare March 31, 2024 08:27
@Ayesh
Copy link
Contributor Author

Ayesh commented Mar 31, 2024

Thanks a lot for the review @zonuexe those were great. I addressed all the points except the else block remark, mainly to make sure we follow the other polyfill methods, although there is indeed a merit if that changes. Perhaps it's something we can do for all methods in a separate PR?

@nicolas-grekas
Copy link
Member

Thank you @Ayesh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants