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

[Downgrade PHP 7.4] Contravariant arguments #4164

Closed
leoloso opened this issue Sep 8, 2020 · 4 comments · Fixed by #4770
Closed

[Downgrade PHP 7.4] Contravariant arguments #4164

leoloso opened this issue Sep 8, 2020 · 4 comments · Fixed by #4770
Assignees
Labels

Comments

@leoloso
Copy link
Contributor

leoloso commented Sep 8, 2020

Feature Request

Downgrade new feature from PHP 7.4 to its equivalent PHP 7.3 code

Diff

class ParentType {}
class ChildType extends ParentType {}

class A
{
    public function contraVariantArguments(ChildType $type)
    { /* … */ }
}

class B extends A
{
-    public function contraVariantArguments(ParentType $type)
+    /**
+     * @param ParentType $type
+     */
+    public function contraVariantArguments($type)
    { /* … */ }
}
@leoloso leoloso self-assigned this Dec 2, 2020
@leoloso
Copy link
Contributor Author

leoloso commented Dec 2, 2020

To downgrade it, we only need to replicate the same argument types from the parent, without double-checking if these are contravariant (i.e. if the type in the child class is a parent to the type in the parent class). That's because it must be contravariant, or otherwise it will already fail for PHP 7.4.

The rule will be similar to MakeInheritedMethodVisibilitySameAsParentRector

@leoloso
Copy link
Contributor Author

leoloso commented Dec 2, 2020

Actually, this is more complicated than I mentioned in my previous comment: the change would need to be done on the parent class, not on the child class!

That's because the child class will have a more generic argument type, so it can't be reduced to the more specific one from the parent class, or invocations passing that more generic type will fail.

A possible solution is to completely remove the parameter type from the child class, which is called Parameter type widening. However this works starting from PHP 7.2 only.

To make it work with PHP 7.1 and below, we can directly remove the parameter types on both the parent and child methods, replacing them with phpdocs.

@leoloso
Copy link
Contributor Author

leoloso commented Dec 2, 2020

Should make sure if there is a proper solution for #4754, and implement it, before tackling this issue

@leoloso
Copy link
Contributor Author

leoloso commented Dec 3, 2020

Solution: remove the type completely, switching from covariant argument type, to parameter type widening. Then, this latter feature can be downgraded for PHP 7.2 through new rule DowngradeParameterTypeWidening

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants