Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix "Crash: str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters" #4316

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

Closed
staabm wants to merge 5 commits into phpstan:2.1.x from staabm:crash

Conversation

Copy link
Contributor

@staabm staabm commented Sep 10, 2025
edited
Loading

I thought about different variants on how this could be solved.
most important point IMO is we don't crash PHPStan on unexpected input.

I think it makes sense to report a ERROR type for PHP 8.3+ (so when function_exists('str_increment'); and PHP started to emit deprecations) when invalid input is provided.

closes phpstan/phpstan#13481

@staabm staabm marked this pull request as ready for review September 10, 2025 15:35
Copy link
Collaborator

This pull request has been marked as ready for review.

Copy link
Contributor

str_increment() call exists in StrIncrementDecrementFunctionReturnTypeExtension as well

probably worth check there as well?

Copy link
Contributor Author

staabm commented Sep 10, 2025

The added test covers this line

samsonasik reacted with thumbs up emoji

@@ -1750,7 +1751,11 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
&& !is_numeric($varValue)
&& function_exists('str_increment')
) {
$varValue = str_increment($varValue);
try {
Copy link
Contributor

@VincentLanglet VincentLanglet Sep 10, 2025

Choose a reason for hiding this comment

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

This feels kinda weird because

  • On PHP 8.3 & 8.4 we return ErrorType while it's still valid code (even if deprecated)
  • On PHP 8.5+ we're still returning '1' for ''++ while it's invalid code

Should we introduce a method inside PHPVersion class ?

Then you could keep @++$varValue for PHPVersion <= 8.5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was one of the variants I considered. the ++$varValue is consistent across all versions (it differs only by emitting a deprecation warning).

if we are fine with swalling the deprecation warning the impl could be simplified.

I think we wouldn't even need per version expectations

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

I wonder: how come we only use str_increment in our code but not str_decrement at all? I feel like places doing -- possibly on strings should also have similar conditions around that code.

Copy link
Member

Forgot to link it - I tested it and -- is deprecated too https://3v4l.org/q5Fp3/rfc#vgit.master

Copy link
Contributor Author

staabm commented Sep 10, 2025

what do you think about swallowing the deprecations as there is currently no need to use the str_increment/derecment functions..?
(and these are no good replacement)

Copy link
Member

What's wrong with calling them and catching ValueError?

Copy link
Contributor Author

staabm commented Sep 10, 2025
edited
Loading

its "a lot of work" without a gain

it will also lead to different results depending on the used php runtime version

Copy link
Contributor

VincentLanglet commented Sep 10, 2025
edited
Loading

What's wrong with calling them and catching ValueError?

The only wrong thing I see is the fact that we get a ValueError on PHP 8.3/8.4 (and considered the result as ErrorType) while ++ and -- are supposed to be still valid (and working, even if deprecated).

Copy link
Member

Do you think this will work or am I missing something? e4c5865

Copy link
Contributor Author

staabm commented Sep 10, 2025
edited
Loading

Do you think this will work or am I missing something?

as is I think it misses some cases, e.g. ++'ab c1' (this PRs test)

Copy link
Member

I'll cherry pick the tests from here and we'll see. Right now I'm fixing the tests which I forgot to run before pushing the commit 🤦

Copy link
Member

See 392e58d - I know it's not perfect and it's a weird quirk that for some strings ++/-- keeps them the same but str_increment throws a ValueError, but I think I can live with this now.

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

@ondrejmirtes ondrejmirtes ondrejmirtes requested changes

+1 more reviewer

@VincentLanglet VincentLanglet VincentLanglet left review comments

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Crash: str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters on PHPStan 2.1.23

AltStyle によって変換されたページ (->オリジナル) /