Skip to content

Conversation

@TomasVotruba
Copy link
Member

  • add fixture
  • fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector

@TomasVotruba TomasVotruba changed the title tv explode string Fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector Sep 25, 2025
@TomasVotruba TomasVotruba merged commit fe64244 into main Sep 25, 2025
50 checks passed
@TomasVotruba TomasVotruba deleted the tv-explode-string branch September 25, 2025 22:29
}
}

// correction for explode() that always returns array
Copy link
Contributor

@staabm staabm Sep 26, 2025

Choose a reason for hiding this comment

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

This assumption is only true for php8+

https://3v4l.org/N1eUT

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

@staabm staabm Sep 26, 2025

Choose a reason for hiding this comment

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

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)

Copy link
Member

Choose a reason for hiding this comment

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

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

if (! $node instanceof FullyQualified && $node->hasAttribute(AttributeKey::NAMESPACED_NAME)) {
return $this->resolve(new FullyQualified($node->getAttribute(AttributeKey::NAMESPACED_NAME)));
}

Copy link
Member

Choose a reason for hiding this comment

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

I checked locally, without this, it seems got mixed[] on explode() function:

-     * @param string[] $items
+     * @param mixed[] $items

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

Copy link
Member

Choose a reason for hiding this comment

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

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