- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 378
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
ReturnTypeFromStrictTernaryRector: Support complex ternaries #4515
Conversation
...ypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector/Fixture/short_ternary.php.inc
Outdated
Show resolved
Hide resolved
|
||
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector\Fixture; | ||
|
||
final class SkipPhpdocs |
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 test fails to skip right now. If I understood the "strict" rules correctly, they should not rely on phpdoc, right?
if so, any idea how to achieve that?
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.
You can compare type vs native type, like in ExprAnalyzer:
rector-src/src/NodeAnalyzer/ExprAnalyzer.php
Lines 42 to 52 in f056974
$nativeType = $scope->getNativeType($expr); | |
if ($nativeType instanceof MixedType && ! $nativeType->isExplicitMixed()) { | |
return true; | |
} | |
$type = $scope->getType($expr); | |
if ($nativeType instanceof UnionType) { | |
return ! $nativeType->equals($type); | |
} | |
return ! $nativeType->isSuperTypeOf($type) |
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.
thanks. I added a native type check and now I am wondering why ternary_var_call.php.inc
is failling.
rector type resolving tells me the native type of the ternary is MixedType
but running the same code directly in PHPStan returns a correct native type..
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 dropped the failling test for now, as I don't see it important enough to block the actual useful change
rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php
Show resolved
Hide resolved
continue; | ||
} | ||
|
||
$onlyStmt = $classMethod->stmts[0] ?? null; |
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.
before this PR this rector only supported call-methods with a single line.
I wided the scope to call-likes with a single return
should be ready for review/merge @samsonasik |
rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php
Outdated
Show resolved
Hide resolved
Looks good, thank you 👍 |
idea is to allow ReturnTypeFromStrictTernaryRector to infer the native return type on complex typed expressions