-
-
Notifications
You must be signed in to change notification settings - Fork 333
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture; | ||
|
||
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; | ||
} | ||
} | ||
|
||
?> |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,7 +21,6 @@ | |||||||||||||
use PHPStan\Reflection\Native\NativeFunctionReflection; | ||||||||||||||
use PHPStan\Reflection\ReflectionProvider; | ||||||||||||||
use PHPStan\Type\BooleanType; | ||||||||||||||
use PHPStan\Type\Constant\ConstantStringType; | ||||||||||||||
use PHPStan\Type\FloatType; | ||||||||||||||
use PHPStan\Type\IntegerType; | ||||||||||||||
use PHPStan\Type\NullType; | ||||||||||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. you might try There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. It seems got error on StringType calling undefined
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, it There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||||||
} | ||||||||||||||
Comment on lines
-92
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was on pourpose to avoid leaky do types. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
that's will skip docblock read as type :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. for FuncCall, and native Function Reflection, it require rector-src/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php Lines 139 to 140 in aee4200
|
||||||||||||||
|
||||||||||||||
|
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!
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 :)