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

[TypeDeclaration] Add return empty string defined support on ReturnTypeFromStrictScalarReturnExprRector #4919

Merged
merged 4 commits into from Sep 6, 2023

Conversation

samsonasik
Copy link
Member

@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @staabm

Comment on lines +5 to +53
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;
}
}
Copy link
Contributor

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!

Suggested change
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;
}
}

Copy link
Member Author

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 😄

Copy link
Member Author

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 :)

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it ;)

@samsonasik samsonasik merged commit c4ea636 into main Sep 6, 2023
40 checks passed
@samsonasik samsonasik deleted the return-empty-string branch September 6, 2023 11:33
@@ -89,7 +88,7 @@ private function resolveCastType(Cast $cast): ?Type

private function isScalarType(Type $type): bool
Copy link
Contributor

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()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try 👍

Copy link
Member Author

@samsonasik samsonasik Sep 6, 2023

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()

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -92 to 93
if ($type->isString()->yes() && ! $type instanceof ConstantStringType) {
if ($type->isString()->yes()) {
return true;
}
Copy link
Member

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?

Copy link
Member Author

@samsonasik samsonasik Sep 6, 2023

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:

$type = $this->nodeTypeResolver->getNativeType($cast);
if ($type->isScalar()->yes()) {

that's will skip docblock read as type :)

Copy link
Member Author

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:

$exprType = $this->nodeTypeResolver->getNativeType($expr);
if ($exprType->isScalar()->yes()) {

Copy link
Member

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 :)

Copy link
Member Author

@samsonasik samsonasik Sep 6, 2023

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

$returnType = $parametersAcceptor->getReturnType();
if ($returnType->isScalar()->yes()) {

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