-
-
Notifications
You must be signed in to change notification settings - Fork 365
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] Add return empty string defined support on ReturnTypeFromStrictScalarReturnExprRector #4919
Conversation
…peFromStrictScalarReturnExprRector
Fixed 🎉 /cc @staabm |
final class ReturnEmptyString | ||
{ | ||
private function generateStyle(array $values, string $padding) | ||
{ | ||
$style = ''; | ||
|
||
if ('' != $values['background-color']) { | ||
$style = 'something'; | ||
|
||
if ('' != $padding) { | ||
$style .= $padding; | ||
} | ||
} | ||
|
||
if ('' != $style) { | ||
return ' style="'.$style.'"'; | ||
} | ||
|
||
return $style; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
final class ReturnEmptyString | ||
{ | ||
private function generateStyle(array $values, string $padding): string | ||
{ | ||
$style = ''; | ||
|
||
if ('' != $values['background-color']) { | ||
$style = 'something'; | ||
|
||
if ('' != $padding) { | ||
$style .= $padding; | ||
} | ||
} | ||
|
||
if ('' != $style) { | ||
return ' style="'.$style.'"'; | ||
} | ||
|
||
return $style; | ||
} | ||
} |
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 think we can simplify the test. otherwise its great, thanks!
final class ReturnEmptyString | |
{ | |
private function generateStyle(array $values, string $padding) | |
{ | |
$style = ''; | |
if ('' != $values['background-color']) { | |
$style = 'something'; | |
if ('' != $padding) { | |
$style .= $padding; | |
} | |
} | |
if ('' != $style) { | |
return ' style="'.$style.'"'; | |
} | |
return $style; | |
} | |
} | |
?> | |
----- | |
<?php | |
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | |
final class ReturnEmptyString | |
{ | |
private function generateStyle(array $values, string $padding): string | |
{ | |
$style = ''; | |
if ('' != $values['background-color']) { | |
$style = 'something'; | |
if ('' != $padding) { | |
$style .= $padding; | |
} | |
} | |
if ('' != $style) { | |
return ' style="'.$style.'"'; | |
} | |
return $style; | |
} | |
} | |
final class ReturnEmptyString | |
{ | |
private function generateStyle(array $values, string $padding) | |
{ | |
$style = ''; | |
if ('' != $values['background-color']) { | |
$style = 'something'; | |
} | |
if ('' != $style) { | |
return ' style="'.$style.'"'; | |
} | |
return $style; | |
} | |
} | |
?> | |
----- | |
<?php | |
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | |
final class ReturnEmptyString | |
{ | |
private function generateStyle(array $values, string $padding): string | |
{ | |
$style = ''; | |
if ('' != $values['background-color']) { | |
$style = 'something'; | |
} | |
if ('' != $style) { | |
return ' style="'.$style.'"'; | |
} | |
return $style; | |
} | |
} |
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.
Oh, I merge too fast 😄
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.
@staabm feel free to create PR for updated fixture :)
All checks have passed 🎉 @TomasVotruba I am merging it ;) |
@@ -89,7 +88,7 @@ private function resolveCastType(Cast $cast): ?Type | |||
|
|||
private function isScalarType(Type $type): bool |
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 might try $type->isScalar()->yes()
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 will 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.
It seems got error on StringType calling undefined isScalar()
There was 1 error:
1) Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\ReturnTypeFromStrictScalarReturnExprRectorTest::test with data set #7
Error: Call to undefined method PHPStan\Type\StringType::isScalarType()
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.
oh, it isScalar()
instead of isScalarType()
, I will create new PR.
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.
if ($type->isString()->yes() && ! $type instanceof ConstantStringType) { | ||
if ($type->isString()->yes()) { | ||
return true; | ||
} |
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 was on pourpose to avoid leaky do types.
Would this skip the docblock string 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.
The type definition is native type, see line 81:
rector-src/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php
Lines 81 to 82 in aee4200
$type = $this->nodeTypeResolver->getNativeType($cast); | |
if ($type->isScalar()->yes()) { |
that's will skip docblock read as 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.
also on line 71, use native type as well:
rector-src/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php
Lines 71 to 72 in aee4200
$exprType = $this->nodeTypeResolver->getNativeType($expr); | |
if ($exprType->isScalar()->yes()) { |
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 vaguely recall PHPStan resolved this incorrectly, once the method call was in external file. Making docblock as native types.
If it works, all good :)
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.
for FuncCall, and native Function Reflection, it require ->getReturnType()
instead of ->getNativeReturnType()
, that's expected as PHPStan seems doens't read it based on defined Func Call, see
rector-src/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php
Lines 139 to 140 in aee4200
$returnType = $parametersAcceptor->getReturnType(); | |
if ($returnType->isScalar()->yes()) { |
Fixes rectorphp/rector#8181