Skip to content

ctype_digit() narrows to decimal-int-string#5311

Merged
ondrejmirtes merged 8 commits into
phpstan:2.2.xfrom
staabm:decimal-int
Jun 2, 2026
Merged

ctype_digit() narrows to decimal-int-string#5311
ondrejmirtes merged 8 commits into
phpstan:2.2.xfrom
staabm:decimal-int

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Mar 28, 2026

No description provided.

@phpstan-bot
Copy link
Copy Markdown
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

if (ctype_digit((string) $numericString)) {
assertType('numeric-string', $numericString);
} else {
assertType('*NEVER*', $numericString);
Copy link
Copy Markdown
Contributor Author

@staabm staabm Mar 28, 2026

Choose a reason for hiding this comment

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

bug before PR: numeric strings exist which ctype_digit does not return true, for see https://3v4l.org/1Qrlg#veol

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should be fix on 2.1 then ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure its worth fixing on 2.1.x because noone reported it yet and I think for a good fix we need decimal-int-string type which only exists on 2.2.x

@ondrejmirtes
Copy link
Copy Markdown
Member

ondrejmirtes commented Mar 28, 2026

I’d put brakes on this a bit. It’s an experimental type, it’s too soon to propagate it around the codebase.

@ondrejmirtes
Copy link
Copy Markdown
Member

My current idea is that there is going to be 3-way setting about “unsafe array string keys”. The default would be current behaviour (unsafe), to not break users’ baselines with more specific types.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Mar 28, 2026

I see. lets park it for now and come back after the array-key topic is more mature.

@VincentLanglet
Copy link
Copy Markdown
Contributor

VincentLanglet commented Mar 28, 2026

My current idea is that there is going to be 3-way setting about “unsafe array string keys”. The default would be current behaviour (unsafe), to not break users’ baselines with more specific types.

I also tried #5312 on my side @ondrejmirtes.
I won't spend more time on this and let you do it like you want but here's my thought:

  • I have some trouble to see how to have this new type efficient without propagating it to the whole codebase
  • decimal-int-string&lowercase-string&uppercase-string seems redundant, a decimal-int-string is ALWAYS lowercase and uppercase ; we should:
    • Simplify the description in IntersectionType
    • Avoid adding both when possible
  • Breaking the user baseline with more specific seems ok to me in minor, especially when this certainly already occured without people complaining a lot about it when we introduced lowercase-string or uppercase-string ; but if you really want to avoid this, maybe we could just describe the decimal-int-string as numeric-string&lowercase-string&uppercase-string for people with the toggle off.

@ondrejmirtes
Copy link
Copy Markdown
Member

Yes, decimal-int-string&lowercase-string&uppercase-string is redundant, this should be reduced in TypeCombinator, you can write a test and modify some code in isSuperTypeOf/isSubTypeOf.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Jun 2, 2026

@ondrejmirtes can we go ahead here?

the newly reported issue made me remember this PR

@staabm staabm marked this pull request as ready for review June 2, 2026 07:00
@phpstan-bot
Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes
Copy link
Copy Markdown
Member

What about strings with negative integers? AFAIK they are still cast to integers in array keys.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Jun 2, 2026

they are cast to integers but do not return true on ctype_digit

https://3v4l.org/vo6gg#veol

@staabm staabm requested a review from VincentLanglet June 2, 2026 14:18
@ondrejmirtes ondrejmirtes merged commit dcc3960 into phpstan:2.2.x Jun 2, 2026
661 of 665 checks passed
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you!

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.

4 participants