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

[TypeDeclaration] Skip nullable self referencing param and return on AddArrayParamDocTypeRector and ReturnTypeDeclarationRector #1183

Merged
merged 6 commits into from
Nov 8, 2021

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik force-pushed the close-1182 branch 2 times, most recently from b7dfc83 to b85d018 Compare November 8, 2021 05:08
@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

Comment on lines 173 to 183
private function hasUnionWithThisType(UnionType $unionType): bool
{
$types = $unionType->getTypes();
foreach ($types as $type) {
if ($type instanceof ThisType) {
return true;
}
}

return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

IMO this would skip any union type with ThisType. Instead, the ThisType should match the existing object type and compare it.

If they are equal, it shold be skipped. If they are no the same, they type should be overriden.

These 2 fixtures should be covered and work too:

class NullableSelf
{
    private ?self $parent = null;

-    public function getParent(): ?int
+   public function getParent(): ?self
    {
        return $this->parent;
    }
}
?>
class NullableSelf
{
    private ?self $parent = null;

-    public function getParent(): ?AnotherObject
+   public function getParent(): ?self
    {
        return $this->parent;
    }
}
?>

Copy link
Member Author

Choose a reason for hiding this comment

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

@TomasVotruba I think those mostly a userbase invalid code which possibly on purpose. I tried that, but that cause invalid changes in other places due to check return expr and the return stmts as well.

Copy link
Member

@TomasVotruba TomasVotruba Nov 8, 2021

Choose a reason for hiding this comment

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

It might be the case, but this asumption might be wrong. It is rather about fixing the self comparison object that now does not work.

I tried that, but that cause invalid changes in other places due to check return expr and the return stmts as well.

It should be fixed there too then. Could you send this proposal? I'll see what fails there.

Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to remove this method to see the change, gimme a sec...

@TomasVotruba TomasVotruba force-pushed the close-1182 branch 2 times, most recently from eeb06a2 to 5371ee1 Compare November 8, 2021 11:16
Comment on lines 223 to 230
return $inferedType->isSubTypeOf($currentType)
return $currentType->isSubTypeOf($inferedType)
->yes();
Copy link
Member

Choose a reason for hiding this comment

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

This was the buggy part. Current type can be stricter then resolved, so if it subtype, it should be kept.

@TomasVotruba
Copy link
Member

@samsonasik I've posted the solution. Could you review & merge?

@@ -476,6 +476,9 @@ public function getNode(): \PhpParser\Node
return $this->node;
}

/**
* @param \PhpDocTagValueNode|null $phpDocTagValueNode
Copy link
Member Author

Choose a reason for hiding this comment

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

this is invalid, there is no \PhpDocTagValueNode class, only PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode, also, typed param already cover it.

@samsonasik
Copy link
Member Author

rectify now got error /~https://github.com/rectorphp/rector-src/runs/4138103624?check_suite_focus=true#step:6:5794

                                                                                
 [ERROR] Could not process                                                      
         "rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php"
         file, due to:                                                          
         "Serialization of 'Closure' is not allowed". On line: 33       

@TomasVotruba
Copy link
Member

I've missed the Rector job, will check 👍

return serialize($type) . $type->isExplicitMixed();
return $type->describe(VerbosityLevel::precise()) . $type->isExplicitMixed();
Copy link
Member

Choose a reason for hiding this comment

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

In some wierd cases, the MixedType contains closures that cannot be serialized.

@TomasVotruba
Copy link
Member

CI is passing. Ready for review 👍

@samsonasik
Copy link
Member Author

Thank you @TomasVotruba

@samsonasik samsonasik merged commit e899eaa into main Nov 8, 2021
@samsonasik samsonasik deleted the close-1182 branch November 8, 2021 12:27
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.

Strange behavior of AddArrayParamDocTypeRector, ReturnTypeDeclarationRector
3 participants