Improve return type of range() function to always yield a non-empty-list
#4688
+50
−51
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.
Description
By definition, the
range()function will never return an empty array. This PR updates theRangeFunctionReturnTypeExtensionto follow this definition for all cases.Note
PHP 8.3 changed how certain “special” or unusual
range()calls are handled, but this does not affect the aforementioned definition.Example:
Up to PHP 8.2, the interpreter attempted to infer a sensible result (e.g. by normalizing the step). Starting with PHP 8.3, such calls either emit warnings or throw errors instead.
Example
list<int>non-empty-list<int>Implementation
Some return types previously did not use
NonEmptyArrayType. This PR appliesNonEmptyArrayTypeto all possible return types ofrange(), ensuring they differ only in their inner element type.To avoid duplication, a small helper method
getNonEmptyListOfType($type)was introduced and reused across the 9 different return statements.