Skip to content

Impl StrIncrementDecrementFunctionReturnTypeExtension #2776

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

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Nov 25, 2023

Add types to str_increment() and str_decrement() added in PHP 8.3.

However, unlike something like strtolower(), I can't think of many use cases for incrementing/decrementing constants, so it makes sense to reject this PR.

@zonuexe zonuexe changed the title Feature/str increment decrement Impl StrIncrementDecrementFunctionReturnTypeExtension Nov 25, 2023
}
}

return (string)++$s;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zonuexe zonuexe force-pushed the feature/str_increment_decrement branch 2 times, most recently from d228654 to 1bb4a34 Compare November 25, 2023 22:43
return [
'new' => [
'str_decrement' => ['non-empty-string'],
'str_increment' => ['non-falsy-string'],
Copy link
Member

Choose a reason for hiding this comment

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

This signature is wrong. The functions also have parameters.

{
if (function_exists('str_increment')) {
try {
return str_increment($s);
Copy link
Member

Choose a reason for hiding this comment

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

Or maybe we can always call our own implementation and not have a different one here.

@zonuexe zonuexe force-pushed the feature/str_increment_decrement branch from 1bb4a34 to 77dbe4f Compare November 26, 2023 15:21
$args = $functionCall->getArgs();

if (count($args) !== 1) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
Copy link
Member

Choose a reason for hiding this comment

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

You can just return null in these cases

return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$value = $argType->getConstantScalarValues()[0];
Copy link
Member

Choose a reason for hiding this comment

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

You should account for union of values, don't just read the first one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, I misunderstood UnionType::getConstantScalarValues().

@zonuexe zonuexe force-pushed the feature/str_increment_decrement branch from f903595 to a8eb826 Compare December 23, 2023 11:03
@ondrejmirtes ondrejmirtes merged commit 26e4a53 into phpstan:1.10.x Jan 5, 2024
@ondrejmirtes
Copy link
Member

Thank you!

@zonuexe zonuexe deleted the feature/str_increment_decrement branch January 5, 2024 15:06
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.

2 participants