Skip to content

Commit

Permalink
[TypeDeclaration] Add return empty string defined support on ReturnTy…
Browse files Browse the repository at this point in the history
…peFromStrictScalarReturnExprRector (#4919)

* [TypeDeclaration] Add return empty string defined support on ReturnTypeFromStrictScalarReturnExprRector

* [ci-review] Rector Rectify

* Fixed 🎉

* remove unused use

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 6, 2023
1 parent 0abb641 commit c4ea636
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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
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
{
if ($type->isString()->yes() && ! $type instanceof ConstantStringType) {
if ($type->isString()->yes()) {
return true;
}

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

0 comments on commit c4ea636

Please sign in to comment.