Skip to content

Improve ReplaceFunctionsDynamicReturnTypeExtension #4023

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

Open
wants to merge 5 commits into
base: 2.1.x
Choose a base branch
from

Conversation

VincentLanglet
Copy link
Contributor

Comment on lines 111 to 114
$stringArgumentType = TypeCombinator::intersect(new StringType(), $subjectArgumentType);
if (!$stringArgumentType instanceof NeverType) {
$result[] = $this->getReplaceType($stringArgumentType, $replaceArgumentType);

if ($subjectArgumentType->isUppercaseString()->yes() && $replaceArgumentType->isUppercaseString()->yes()) {
$accessories[] = new AccessoryUppercaseStringType();
}
$subjectArgumentType = $subjectArgumentType->tryRemove($stringArgumentType);
}

if (count($accessories) > 0) {
$accessories[] = new StringType();
return new IntersectionType($accessories);
if ($subjectArgumentType->isArray()->yes()) {
Copy link
Contributor Author

@VincentLanglet VincentLanglet May 25, 2025

Choose a reason for hiding this comment

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

When we get string|array<string>

I didn't find a better way:

  • First, to work with the string part first (in order to know if it's non-empty, lowercase, etc)
  • Secondly to work only with the array part (in order to know the shape, if it's a list, non empty, the values, etc)

To access the string part, the best I found was

$stringArgumentType = TypeCombinator::intersect(new StringType(), $subjectArgumentType);

Same idea for array because

  • $subjectArgumentType->getArrays() does not return array on union
  • TypeUtiles::toBenevolent($subjectArgumentType)->getArrays() is returning arrays but is loosing the non-empty and the list accessory.

@VincentLanglet VincentLanglet force-pushed the str_replace branch 2 times, most recently from b762936 to f438281 Compare May 25, 2025 17:57
@VincentLanglet VincentLanglet marked this pull request as ready for review May 26, 2025 07:49
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

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.

non-empty-string|array in str_replace
2 participants