diff --git a/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php b/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php index f342ec2f2d6..59c7418b70c 100644 --- a/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php +++ b/rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php @@ -6,6 +6,7 @@ namespace Rector\DeadCode\PhpDoc; use PhpParser\Node; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; @@ -61,24 +62,27 @@ final class DeadReturnTagValueNodeAnalyzer $this->phpDocTypeChanger = $phpDocTypeChanger; $this->staticTypeMapper = $staticTypeMapper; } - public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod $classMethod) : bool + /** + * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike + */ + public function isDead(ReturnTagValueNode $returnTagValueNode, $functionLike) : bool { - $returnType = $classMethod->getReturnType(); + $returnType = $functionLike->getReturnType(); if ($returnType === null) { return \false; } if ($returnTagValueNode->description !== '') { return \false; } - $scope = $classMethod->getAttribute(AttributeKey::SCOPE); + $scope = $functionLike->getAttribute(AttributeKey::SCOPE); if ($scope instanceof Scope && $scope->isInTrait() && $returnTagValueNode->type instanceof ThisTypeNode) { return \false; } if (!$this->hasUsefullPhpdocType($returnTagValueNode, $returnType)) { return \true; } - if (!$this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual($returnType, $returnTagValueNode->type, $classMethod)) { - return $this->isDeadNotEqual($returnTagValueNode, $returnType, $classMethod); + if (!$this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual($returnType, $returnTagValueNode->type, $functionLike)) { + return $this->isDeadNotEqual($returnTagValueNode, $returnType, $functionLike); } if ($this->phpDocTypeChanger->isAllowed($returnTagValueNode->type)) { return \false; @@ -102,13 +106,16 @@ final class DeadReturnTagValueNodeAnalyzer { return $node instanceof Identifier && $node->toString() === 'never'; } - private function isDeadNotEqual(ReturnTagValueNode $returnTagValueNode, Node $node, ClassMethod $classMethod) : bool + /** + * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike + */ + private function isDeadNotEqual(ReturnTagValueNode $returnTagValueNode, Node $node, $functionLike) : bool { if ($returnTagValueNode->type instanceof IdentifierTypeNode && (string) $returnTagValueNode->type === 'void') { return \true; } $nodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node); - $docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($returnTagValueNode->type, $classMethod); + $docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($returnTagValueNode->type, $functionLike); return $docType instanceof UnionType && $this->typeComparator->areTypesEqual(TypeCombinator::removeNull($docType), $nodeType); } private function hasTrueFalsePseudoType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode) : bool diff --git a/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php b/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php index c700d1f65c3..26266bd1b8e 100644 --- a/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php +++ b/rules/DeadCode/PhpDoc/TagRemover/ReturnTagRemover.php @@ -4,6 +4,7 @@ declare (strict_types=1); namespace Rector\DeadCode\PhpDoc\TagRemover; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Function_; use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\DeadCode\PhpDoc\DeadReturnTagValueNodeAnalyzer; @@ -18,14 +19,17 @@ final class ReturnTagRemover { $this->deadReturnTagValueNodeAnalyzer = $deadReturnTagValueNodeAnalyzer; } - public function removeReturnTagIfUseless(PhpDocInfo $phpDocInfo, ClassMethod $classMethod) : bool + /** + * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike + */ + public function removeReturnTagIfUseless(PhpDocInfo $phpDocInfo, $functionLike) : bool { // remove existing type $returnTagValueNode = $phpDocInfo->getReturnTagValue(); if (!$returnTagValueNode instanceof ReturnTagValueNode) { return \false; } - $isReturnTagValueDead = $this->deadReturnTagValueNodeAnalyzer->isDead($returnTagValueNode, $classMethod); + $isReturnTagValueDead = $this->deadReturnTagValueNodeAnalyzer->isDead($returnTagValueNode, $functionLike); if (!$isReturnTagValueDead) { return \false; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php index ff6e5245230..27f46ef80ba 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\ClassMethod; use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Function_; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Core\Rector\AbstractRector; @@ -69,10 +70,10 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [ClassMethod::class]; + return [ClassMethod::class, Function_::class]; } /** - * @param ClassMethod $node + * @param ClassMethod|Function_ $node */ public function refactor(Node $node) : ?Node { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 238959f5caf..9a553c0ebad 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '005ccc330c3bb37f9faf0f5e2c319036f2d055b8'; + public const PACKAGE_VERSION = '4531aeb0cd7ef47a75afbee72237eef45b4cc266'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-12-04 21:54:02'; + public const RELEASE_DATE = '2023-12-05 16:53:06'; /** * @var int */