Skip to content

Commit

Permalink
[DeadCode] Remove unused @return mixed on RemoveUselessReturnTagRector (
Browse files Browse the repository at this point in the history
#5332)

* [DeadCode] Remove unused @return mixed on RemoveUselessReturnTagRector

* implemented 🎉

* more fixture
  • Loading branch information
samsonasik committed Dec 6, 2023
1 parent 1daab3c commit 043d43c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
@@ -0,0 +1,34 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

use stdClass;

class RemoveReturnMixed
{
/**
* @return mixed
*/
function foo(): stdClass
{
return new stdClass();
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

use stdClass;

class RemoveReturnMixed
{
function foo(): stdClass
{
return new stdClass();
}
}

?>
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

class RemoveReturnMixed2
{
/**
* @return mixed
*/
function foo(): mixed
{
return null;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

class RemoveReturnMixed2
{
function foo(): mixed
{
return null;
}
}

?>
14 changes: 9 additions & 5 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Expand Up @@ -51,10 +51,6 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod|Funct
return false;
}

if (! $this->hasUsefullPhpdocType($returnTagValueNode, $returnType)) {
return true;
}

if (! $this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual(
$returnType,
$returnTagValueNode->type,
Expand Down Expand Up @@ -98,6 +94,10 @@ private function isDeadNotEqual(ReturnTagValueNode $returnTagValueNode, Node $no
return true;
}

if (! $this->hasUsefullPhpdocType($returnTagValueNode, $node)) {
return true;
}

$nodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node);
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
$returnTagValueNode->type,
Expand Down Expand Up @@ -129,10 +129,14 @@ private function hasTrueFalsePseudoType(BracketsAwareUnionTypeNode $bracketsAwar
}

/**
* in case of void,never there is no added value in "@return tag"
* exact different between @return and node return type
*/
private function hasUsefullPhpdocType(ReturnTagValueNode $returnTagValueNode, mixed $returnType): bool
{
if ($returnTagValueNode->type instanceof IdentifierTypeNode && $returnTagValueNode->type->name === 'mixed') {
return false;
}

if (! $this->isVoidReturnType($returnType)) {
return ! $this->isNeverReturnType($returnType);
}
Expand Down

0 comments on commit 043d43c

Please sign in to comment.