Skip to content

Commit

Permalink
[Php80] Do not remove true pseudo type on UnionTypesRector (#1715)
Browse files Browse the repository at this point in the history
* Do not remove `true|T` from phpdoc as it cannot be narrowed to native `bool|T` type hint

* [Php80] Do not remove true pseudo type on UnionTypesRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* rectify

Co-authored-by: Simon Podlipsky <simon@podlipsky.net>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people authored Jan 24, 2022
1 parent 386c97b commit a53b3d3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

final class TruePseudoType
{
/**
* @return true|int
*/
public function go($value)
{
return (int) $value ?? true;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

final class TruePseudoType
{
/**
* @return true|int
*/
public function go($value): int|bool
{
return (int) $value ?? true;
}
}

?>
27 changes: 25 additions & 2 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Trait_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
Expand Down Expand Up @@ -56,8 +57,30 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, FunctionLike $fun
return $returnTagValueNode->description === '';
}

if (! $this->genericTypeNodeAnalyzer->hasGenericType($returnTagValueNode->type)) {
return $returnTagValueNode->description === '';
if ($this->genericTypeNodeAnalyzer->hasGenericType($returnTagValueNode->type)) {
return false;
}

if ($this->hasTruePseudoType($returnTagValueNode->type)) {
return false;
}

return $returnTagValueNode->description === '';
}

private function hasTruePseudoType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
{
$unionTypes = $bracketsAwareUnionTypeNode->types;

foreach ($unionTypes as $unionType) {
if (! $unionType instanceof IdentifierTypeNode) {
continue;
}

$name = strtolower((string) $unionType);
if ($name === 'true') {
return true;
}
}

return false;
Expand Down

0 comments on commit a53b3d3

Please sign in to comment.