Narrow types after strlen()#5703
Open
phpstan-bot wants to merge 7 commits into
Open
Conversation
…ArrayDimFetchCheck` "might not exist" report
- In the `reportMaybes` decomposition loop of `NonexistentOffsetInArrayDimFetchCheck`,
when flattened union members are checked individually, skip reporting for string types
with integer offsets where a specific member returns `no` for `hasOffsetValueType`
- The union-level `hasOffsetValueType` already correctly returns `maybe` for these cases;
the decomposition adds no new information for strings and produces false positives
when one union member (e.g. empty string) is shorter than the offset
- Example: `(''|':')[0]` — union-level returns `maybe`, decomposition found `''` returns
`no` for offset 0 and incorrectly escalated to "might not exist" report
- Definitive `no` results (single string, non-integer offset) are unaffected
- Regression test covers the original issue pattern and string union variants
staabm
reviewed
May 18, 2026
…uthy context Revert the rule-level fix that skipped string-with-integer-offset checks in NonexistentOffsetInArrayDimFetchCheck. Instead, teach the TypeSpecifier to narrow strlen()/mb_strlen() arguments to non-empty-string when the result is used in a truthy context. This enables the existing ConditionalExpressionHolder machinery to propagate the narrowing through variable assignments like `$len = strlen($x); if ($len > 0)`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
reviewed
May 19, 2026
…xtension Extract the inline strlen/mb_strlen non-empty-string narrowing from TypeSpecifier::specifyTypesInCondition() into a proper FunctionTypeSpecifyingExtension, following the same pattern as CountFunctionTypeSpecifyingExtension. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
reviewed
May 19, 2026
NonexistentOffsetInArrayDimFetchCheck "might not exist" report…texts Cover all context types: direct truthy/falsey, negation, boolean and/or, ternary, loose comparison with true/false, mb_strlen, and non-string input handling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
approved these changes
May 19, 2026
staabm
reviewed
May 19, 2026
staabm
reviewed
May 19, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes phpstan/phpstan#13688