Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Handle crash on valid conditional type on RemoveUselessReturnTagRector #6475

Merged
merged 4 commits into from
Nov 22, 2024

Conversation

samsonasik
Copy link
Member

Fix crash:

There was 1 error:

1) Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\RemoveUselessReturnTagRectorTest::test with data set #34 ('/Users/samsonasik/www/rector-...hp.inc')
Error: Call to undefined method PHPStan\Type\ConditionalTypeForParameter::getItemType()

/Users/samsonasik/www/rector-src/rules/TypeDeclaration/TypeNormalizer.php:61
/Users/samsonasik/www/rector-src/src/NodeTypeResolver/TypeComparator/TypeComparator.php:60

Fixes rectorphp/rector#8907

@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @ruudk

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it ;)

@samsonasik samsonasik merged commit 2f77705 into main Nov 22, 2024
36 checks passed
@samsonasik samsonasik deleted the handle-crash-44 branch November 22, 2024 18:43
@ruudk
Copy link
Contributor

ruudk commented Nov 22, 2024

🔥 🥷 ⚡

@@ -147,7 +147,7 @@ private function processAddArrayReturnType(
ClassMethod|Function_|Closure $node,
Type $returnType
): ClassMethod|Function_|Closure|null {
if (! $returnType->isArray()->yes()) {
if (! $returnType instanceof ArrayType && ! $returnType instanceof ConstantArrayType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this stop working on a union of arrays?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or on non-empty-array, which is a intersection type

Copy link
Member Author

Choose a reason for hiding this comment

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

I talked with @TomasVotruba about this, and more and more we used PHPStan method from Type, it just cause more bugs, so we decide to use instanceof if the variable of call is used next, that's allow for improvement for more types later, rather than cause crash because of method not exists in target PHPStan type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, as soon as you use methods which are not part of the Type interface you hurt PHPStan guarantees.

It would be great if we could get rid of calls to methods which only exist on certain types

Copy link
Contributor

Choose a reason for hiding this comment

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

see #6478

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.

System error: "Call to undefined method PHPStan\Type\ConditionalTypeForParameter::getItemType()
3 participants