Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 10, 2025

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
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@samsonasik
Copy link
Contributor

str_increment() call exists in StrIncrementDecrementFunctionReturnTypeExtension as well

probably worth check there as well?

@staabm
Copy link
Contributor Author

staabm commented Sep 10, 2025

The added test covers this line

@@ -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

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.

@ondrejmirtes
Copy link
Member

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

@staabm
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)

@ondrejmirtes
Copy link
Member

What's wrong with calling them and catching ValueError?

@staabm
Copy link
Contributor Author

staabm commented Sep 10, 2025

its "a lot of work" without a gain

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

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Sep 10, 2025

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).

@ondrejmirtes
Copy link
Member

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

@staabm
Copy link
Contributor Author

staabm commented Sep 10, 2025

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)

@ondrejmirtes
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 🤦

@ondrejmirtes
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.

@staabm staabm deleted the crash branch September 10, 2025 19:01
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.

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