Skip to content
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

ucfirst behavior differs from PHP default #6834

Closed
grommasdietz opened this issue Dec 5, 2024 · 3 comments · Fixed by #6860
Closed

ucfirst behavior differs from PHP default #6834

grommasdietz opened this issue Dec 5, 2024 · 3 comments · Fixed by #6860
Assignees
Labels
type: bug 🐛 Is a bug; fixes a bug
Milestone

Comments

@grommasdietz
Copy link

kirby/src/Toolkit/Str.php

Lines 1387 to 1392 in 94cc37e

public static function ucfirst(string $string = null): string
{
$first = static::substr($string, 0, 1);
$rest = static::substr($string, 1);
return static::upper($first) . static::lower($rest);
}

Description

The Str::ucfirst method in Kirby differs from PHP's native ucfirst() function.

PHP native

Converts only the first letter of the string to uppercase, leaving the rest of the string unchanged.

Kirby

Converts the first letter to uppercase and the rest of the string to lowercase.

Steps to Reproduce

use Kirby\Toolkit\Str;

echo ucfirst('hello WORLD'); 
// Output: Hello WORLD (PHP native behavior)

echo Str::ucfirst('hello WORLD');
// Output: Hello world (Kirby behavior)

Expected Behavior

Str::ucfirst should either align with PHP's ucfirst() or clarify its differing behavior in the documentation to avoid confusion.

Suggested Solutions

change static::lower($rest) to $rest

public static function ucfirst(string $string = null): string
{
	$first = static::substr($string, 0, 1);
	$rest  = static::substr($string, 1);
	return static::upper($first) . $rest;
}
@afbora
Copy link
Member

afbora commented Dec 5, 2024

I think you're right. That's really overlooked bug. But I'm not sure if it will make a breaking change. We can also add a bool lowercase argument if we want, for convenience.

@afbora afbora added the type: bug 🐛 Is a bug; fixes a bug label Dec 5, 2024
@afbora
Copy link
Member

afbora commented Dec 6, 2024

@distantnative What do you think about that?

@distantnative
Copy link
Member

I agree that this is a bug as the method description really claims to work the same as PHP's ucfirst(). We should try to include it in v5, as it is a bugfix but could also seen as breaking change if anyone relied on this behavior.

@afbora afbora added this to the 5.0.0-beta.2 milestone Dec 9, 2024
@afbora afbora self-assigned this Dec 10, 2024
afbora added a commit that referenced this issue Dec 10, 2024
@afbora afbora mentioned this issue Dec 10, 2024
5 tasks
@afbora afbora linked a pull request Dec 10, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Is a bug; fixes a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants