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

[Php80] Add MixedTypeRector #2701

Merged
merged 32 commits into from Jul 27, 2022
Merged

[Php80] Add MixedTypeRector #2701

merged 32 commits into from Jul 27, 2022

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik marked this pull request as draft July 22, 2022 06:31
@samsonasik samsonasik marked this pull request as ready for review July 22, 2022 16:24
@samsonasik
Copy link
Member Author

samsonasik commented Jul 22, 2022

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

For summary:

For Return Type, I think that require different PR as that will need various handling, eg: actually return void.

@samsonasik
Copy link
Member Author

it seems when parent method has mixed type already, and current doesn't has type doc, it should be transformed to mixed.

@samsonasik
Copy link
Member Author

samsonasik commented Jul 23, 2022

@TomasVotruba so, I think the use case like this should be skipped:

class VendorParam
{
    /**
     * @param int $param
     */
    public function run($param)
    {}
}

class UseParam extends VendorParam
{
    public function run($param)
    {}
}

Next, when parent method int param doc be typed, has child widen with mixed should not be applied while it is working ok, same type is better.

There are other use case like typed already that should be skipped as well:

class A
{
    public function run(int $a)
    {}
}

class B extends A
{
    public function run($a)
    {}
}

as same type in child should be preferred, so there should be different rule or phpstan that handle that

https://phpstan.org/r/c1ffffc4-e76a-4367-adf7-26608665d9b1

@samsonasik
Copy link
Member Author

implemented with add MakeClassMethodParamMixedTypedGuard

@TomasVotruba
Copy link
Member

TomasVotruba commented Jul 23, 2022

@TomasVotruba so, I think the use case like this should be skipped:

Agreed. I think only the doc-typed mixed should be included, so the intention is already defined by user.
Implicit mixed types should be skipped, as the absence of doc-type does not mean mixed type. Maybe it's just missing.

public function run($value)
{
}

-/**
- * @param mixed $value
- */
-public function run($value)
+public function run(mixed $value)
 {
 }

@samsonasik
Copy link
Member Author

The issue with only check mixed doc type is when we use the following code in code base:

interface SomeInterface 
{ 
    /** @param mixed $data */
    public function save($data);
}

class Memory extends SomeInterface
{
    public function save($data) {}
}

That will only change interface, and that will cause error something like:

 ------------------------------------------------------------------------
  packages/Caching/ValueObject/Storage/MemoryCacheStorage.php:37
 ------------------------------------------------------------------------
  - '#Method Rector\\Caching\\ValueObject\\Storage\\MemoryCacheStorage\:\:save\(\) has parameter \$data with no type specified#'

see https://github.com/rectorphp/rector-src/runs/7473011787?check_suite_focus=true#step:5:16 , that's why I created MakeClassMethodParamMixedTypedGuard to cover that.

@samsonasik
Copy link
Member Author

Ok, let's only make typed with use doc @param mixed

@TomasVotruba
Copy link
Member

I think these cases should be skipped, as imlicit mixed doc can be overriden by explicit type in of childs:

Then it could crash:
https://3v4l.org/DGidN

@samsonasik
Copy link
Member Author

samsonasik commented Jul 23, 2022

@TomasVotruba It seems we need to ensure it only apply change when:

  • no parent method
  • no child method

that will also ensure no manual add mixed type when parent has @param mixed and child not, or child using @inheritDoc, what do you think?

@TomasVotruba
Copy link
Member

That makes sense 👍

@samsonasik
Copy link
Member Author

for ClassMethod, I think the mixed type can be applied for final class.

@samsonasik
Copy link
Member Author

or not, as final class can have parent

@samsonasik
Copy link
Member Author

implemented check has child or parent method 🎉

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

@samsonasik
Copy link
Member Author

rebased.

@TomasVotruba TomasVotruba merged commit 69d3c92 into main Jul 27, 2022
@TomasVotruba TomasVotruba deleted the php8 branch July 27, 2022 08:22
@TomasVotruba
Copy link
Member

Thanks 👍

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