Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Remove unused @return mixed on RemoveUselessReturnTagRector #5332

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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