Fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector#7331
Fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector#7331TomasVotruba merged 2 commits intomainfrom
Conversation
TomasVotruba
commented
Sep 25, 2025
- add fixture
- fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector
| } | ||
| } | ||
|
|
||
| // correction for explode() that always returns array |
There was a problem hiding this comment.
This assumption is only true for php8+
There was a problem hiding this comment.
This is getType(), not getNativeType(), I guess it ok for most usage that allow based on docblock, but I think we can add check PhpVersionProvider->isAtLeastPhpVersion() for it.
There was a problem hiding this comment.
Any reason why rector re-implements the type inference for such functions instead of using return-type extensions from PHPStan?
(Which would already do php version specific types)
There was a problem hiding this comment.
For this PR, looking for fixture, use for docblock array iterable value type reason.
Other type resolver and corrector are for common resolve type that are needed, eg Intersection string to string, Intersection array to array, also node verify by its attribute, eg, Name with its attribute, eg
rector-src/src/NodeTypeResolver/NodeTypeResolver/NameTypeResolver.php
Lines 41 to 43 in fe64244
There was a problem hiding this comment.
I checked locally, without this, it seems got mixed[] on explode() function:
- * @param string[] $items
+ * @param mixed[] $itemsWhen using scope->getType(), it got Intersection:
PHPStan\Type\IntersectionType #11668
types: array (3)
| 0 => PHPStan\Type\ArrayType #13424
| | itemType: PHPStan\Type\IntersectionType #11020
| | | types: array (3)
| | | | 0 => PHPStan\Type\Accessory\AccessoryLowercaseStringType #11687
| | | | 1 => PHPStan\Type\Accessory\AccessoryUppercaseStringType #11035
| | | | 2 => PHPStan\Type\StringType #10771
| | | sortedTypes: false
| | keyType: PHPStan\Type\IntegerRangeType #11763
| | | min: 0
| | | max: null
| 1 => PHPStan\Type\Accessory\AccessoryArrayListType #12617
| 2 => PHPStan\Type\Accessory\NonEmptyArrayType #10765
sortedTypes: false
It possibly can be handled in new corrector, eg: AccessoryNonEmptyArrayTypeCorrector, I will create new PR if this can be handled there.
There was a problem hiding this comment.