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
AddReturnTypeDeclarationBasedOnParentClassMethodRector #2666
AddReturnTypeDeclarationBasedOnParentClassMethodRector #2666
Conversation
...ypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationBasedOnParentClassMethodRector.php
Outdated
Show resolved
Hide resolved
...ypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationBasedOnParentClassMethodRector.php
Outdated
Show resolved
Hide resolved
Could you add test for the following use case: abstract class A
{
public abstract function run(): mixed;
}
class B extends A
{
public function run()
{
}
} |
b5281b4
to
28884df
Compare
@samsonasik Updated based on your feedback |
Of course I can. What is exactly poin of this test testing with abstract parent? Or testing of mixed return type? |
MixedType return can mean:
so verify if it supported, add |
that's test mixed typed return, can be both abstract method or not. |
Ok I will do it after lunch |
Also test for skip this case: class A
{
public function run(): mixed
{
return null;
}
}
class B extends A
{
public function run(): string
{
return 'test';
}
}
echo (new B())->run(); |
28884df
to
c2edf10
Compare
Test for explicit mixed return type added |
c2edf10
to
735456b
Compare
Test for narrowed return type added |
Any idea why extended_class_mixed.php.inc fails in CI but work just fine on local? Both runs on same PHP version (8.1.7) and with latest dependencies. |
class load maybe overlapped, you may need to register to composer.json classmap, eg: Line 113 in db7012e
|
735456b
to
b2ca2d4
Compare
What do you mean by overlapped class load? Class name is unique. I tried to add line to composer.json so we will see as soon as CI finishes |
afaik, that's depends of how PHPStan load it, sometime in CI, they are overlapped so it loaded after PHPStan |
@samsonasik Well it helped. Thanks. I would not be able to figure this out myself. |
...dReturnTypeDeclarationBasedOnParentClassMethodRector/Fixture/extended_class_narrowed.php.inc
Outdated
Show resolved
Hide resolved
b2ca2d4
to
4b07b81
Compare
@TomasVotruba Let me know if there is anything I should to to finish this PR. |
composer.json
Outdated
@@ -110,7 +110,8 @@ | |||
"stubs", | |||
"rules-tests/CodingStyle/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source", | |||
"rules-tests/Renaming/Rector/Name/RenameClassRector/Source", | |||
"rules-tests/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source" | |||
"rules-tests/Renaming/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source", | |||
"rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationBasedOnParentClassMethodRector/Source" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed, as the classes must be psr-4 compatible here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See discussion above. Without it PHPStan fails in CI. @samsonasik suggested this as fix and it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why it is needed because it seems more like weird PHPStan bug to me. Any idea how it could be solved differently?
|
||
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnType; | ||
|
||
class MyClass extends SomeClassWithReturnType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. The namespace is missing here, should be completed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So add to all fixtures
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Fixture;
and remove that line from composer.json
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the namespace should be added in Fixture
files, the issue is about resolving Source
directory, like other registration in composer.json.
But, that's worth a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK namespaces added and line drom composer.json removed. So we will see.
I also rebased on current master. But there are still some PHPStan errors not related to this PR - it seems they are caused by new rules added to symplify/phpstan-rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test seems failure https://github.com/rectorphp/rector-src/runs/7468797088?check_suite_focus=true#step:5:84
please add new commit instead of squash commit so we can see the different between before and after registered in composer.json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samsonasik I noticed it too and it is fixed now. But i squashed commits before I read your message. But it seems that Ci pases now even without change in composer.json.
4b07b81
to
bc17377
Compare
bc17377
to
aa3f35d
Compare
Thanks 👍 Is it ready to merge now? |
From my point of view it is ready |
Thank you 😊 |
First draft that adds return type only by first parent/interface found with that method. In future I will try to change it to detect correct intersection type that will satisfy all parents/interfaces.
It is heavily based on AddReturnTypeDeclaration and AddParamBasedOnParentClassMethodRector.
There were some duplicated code with existing rule so I moved it to service but not sure if it is correct place. Structure of services used in rules is quite confusing to me - is there any design guide for that?
I think this roule could also be added to some existing sets but not sure to which. It is quite universal rule that detects issues that would cause Fatal error.
Tests included and after PR #2660 by @samsonasik it works perfectly with interfaces too.
@TomasVotruba Let me know if you want something solved differently.