Skip to content

Commit

Permalink
[DeadCode] Remove @return void on return self on RemoveUselessReturnT…
Browse files Browse the repository at this point in the history
…agRector (#4894)

* [DeadCode] Remove @return void on return self on RemoveUselessReturnTagRector

* [DeadCode] Remove @return void on return self on RemoveUselessReturnTagRector

* rework: check empty description early

* reduce unused method

* Fix

* fix

* [ci-review] Rector Rectify

* final touch: add remove void on void fixture

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 2, 2023
1 parent 3243e0e commit d5f399e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

final class ReturnVoidOnFluent
{
/**
* @return void
*/
public function resolve(): self
{
return $this;
}
}

?>
-----
<?php

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

final class ReturnVoidOnFluent
{
public function resolve(): self
{
return $this;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

final class ReturnVoidOnVoid
{
/**
* @return void
*/
public function resolve(): void
{
}
}

?>
-----
<?php

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

final class ReturnVoidOnVoid
{
public function resolve(): void
{
}
}

?>
12 changes: 6 additions & 6 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return false;
}

if ($paramTagValueNode->description !== '') {
return false;
}

if ($param->type instanceof Name && $this->nodeNameResolver->isName($param->type, 'object')) {
return $paramTagValueNode->type instanceof IdentifierTypeNode && (string) $paramTagValueNode->type === 'object';
}
Expand All @@ -57,17 +61,13 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
}

if (! $paramTagValueNode->type instanceof BracketsAwareUnionTypeNode) {
return $paramTagValueNode->description === '';
return true;
}

if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($paramTagValueNode->type)) {
return false;
}

if (! $this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type)) {
return $paramTagValueNode->description === '';
}

return false;
return ! $this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type);
}
}
24 changes: 7 additions & 17 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\DeadCode\PhpDoc;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
Expand Down Expand Up @@ -36,6 +35,10 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod $clas
return false;
}

if ($returnTagValueNode->description !== '') {
return false;
}

$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope && $scope->isInTrait() && $returnTagValueNode->type instanceof ThisTypeNode) {
return false;
Expand All @@ -46,15 +49,15 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod $clas
$returnTagValueNode->type,
$classMethod,
)) {
return false;
return $returnTagValueNode->type instanceof IdentifierTypeNode && (string) $returnTagValueNode->type === 'void';
}

if ($this->phpDocTypeChanger->isAllowed($returnTagValueNode->type)) {
return false;
}

if (! $returnTagValueNode->type instanceof BracketsAwareUnionTypeNode) {
return $this->isIdentiferRemovalAllowed($returnTagValueNode, $returnType);
return $this->standaloneTypeRemovalGuard->isLegal($returnTagValueNode->type, $returnType);
}

if ($this->genericTypeNodeAnalyzer->hasGenericType($returnTagValueNode->type)) {
Expand All @@ -65,20 +68,7 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, ClassMethod $clas
return false;
}

if ($this->hasTruePseudoType($returnTagValueNode->type)) {
return false;
}

return $returnTagValueNode->description === '';
}

private function isIdentiferRemovalAllowed(ReturnTagValueNode $returnTagValueNode, Node $node): bool
{
if ($returnTagValueNode->description === '') {
return $this->standaloneTypeRemovalGuard->isLegal($returnTagValueNode->type, $node);
}

return false;
return ! $this->hasTruePseudoType($returnTagValueNode->type);
}

private function hasTruePseudoType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
Expand Down
12 changes: 6 additions & 6 deletions rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property): bo
return false;
}

if ($varTagValueNode->description !== '') {
return false;
}

// is strict type superior to doc type? keep strict type only
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
Expand All @@ -33,14 +37,10 @@ public function isDead(VarTagValueNode $varTagValueNode, Property $property): bo
return ! $docType instanceof IntersectionType;
}

if (! $this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual(
return $this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual(
$property->type,
$varTagValueNode->type,
$property
)) {
return false;
}

return $varTagValueNode->description === '';
);
}
}

0 comments on commit d5f399e

Please sign in to comment.