From a53b3d37eba05703b13f667065706301adc708e1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 24 Jan 2022 15:27:41 +0700 Subject: [PATCH] [Php80] Do not remove true pseudo type on UnionTypesRector (#1715) * 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 Co-authored-by: GitHub Action --- .../Fixture/true_pseudo_type.php.inc | 33 +++++++++++++++++++ .../PhpDoc/DeadReturnTagValueNodeAnalyzer.php | 27 +++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/true_pseudo_type.php.inc diff --git a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/true_pseudo_type.php.inc b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/true_pseudo_type.php.inc new file mode 100644 index 00000000000..9530888ac05 --- /dev/null +++ b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/true_pseudo_type.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php b/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php index 83252fe582f..047e6069cfe 100644 --- a/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php +++ b/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php @@ -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; @@ -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;