Updated Rector to commit 4531aeb0cd7ef47a75afbee72237eef45b4cc266

4531aeb0cd [DeadCode] Add Function_ support on RemoveUselessReturnTagRector (#5325)
This commit is contained in:
Tomas Votruba 2023-12-05 09:55:15 +00:00
parent 1095b54b14
commit 374ed77ef0
4 changed files with 25 additions and 13 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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
{

View File

@ -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
*/