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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
Comment on lines +5 to +53
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 :)


?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.

{
if ($type->isString()->yes() && ! $type instanceof ConstantStringType) {
if ($type->isString()->yes()) {
return true;
}
Comment on lines -92 to 93
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()) {


Expand Down
1 change: 1 addition & 0 deletions src/Reflection/ReflectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ private function resolveFunctionReflectionFromFuncCall(
if (! $funcCall->name instanceof Name) {
return null;
}

if ($this->reflectionProvider->hasFunction($funcCall->name, $scope)) {
return $this->reflectionProvider->getFunction($funcCall->name, $scope);
}
Expand Down