Skip to content

Commit

Permalink
Skip description on same line, remove on next line as based on phpdoc…
Browse files Browse the repository at this point in the history
…-parser in RemoveUselessParamTagRector (#4551)

Co-authored-by: Travis Carden <travis.carden@gmail.com>
  • Loading branch information
TomasVotruba and TravisCarden committed Jul 20, 2023
1 parent 8c5507f commit 715a047
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

final class MultilineCommentForUserlandClassTypeHint
{
/**
* @param string $primitiveValue
* A primitive is fine.
* @param UserlandClass $userlandClass
* A user land class is not.
*/
public function test(string $primitiveValue, callable $callableValue, UserlandClass $userlandClass)
{
}
}

class UserlandClass
{
}

?>
-----
<?php

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

final class MultilineCommentForUserlandClassTypeHint
{
/** A primitive is fine.
* A user land class is not.
*/
public function test(string $primitiveValue, callable $callableValue, UserlandClass $userlandClass)
{
}
}

class UserlandClass
{
}

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

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

final class MultilineCommentForUserlandClassTypeHint
{
/**
* @param string $primitiveValue start on first line
* A primitive is fine.
* @param callable $callableValue start on first line
* A PHP core class is fine.
* @param AnotherUserlandClass $userlandClass start on first line
* A user land class is not.
*/
public function test(string $primitiveValue, callable $callableValue, AnotherUserlandClass $userlandClass)
{
}
}

class AnotherUserlandClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ private function refactorClassMethod(ClassMethod $classMethod, Scope $scope): ?C
return null;
}

$classMethodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod, $scope);
$classMethodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod(
$classMethod,
$scope
);
if (! $classMethodReflection instanceof MethodReflection) {
return null;
}
Expand Down
75 changes: 2 additions & 73 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

namespace Rector\DeadCode\PhpDoc;

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
Expand Down Expand Up @@ -64,81 +57,17 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
}

if (! $paramTagValueNode->type instanceof BracketsAwareUnionTypeNode) {
return $this->isEmptyDescription($paramTagValueNode, $param->type);
return $paramTagValueNode->description === '';
}

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

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

return false;
}

private function isEmptyDescription(ParamTagValueNode $paramTagValueNode, Node $node): bool
{
if ($paramTagValueNode->description !== '') {
return false;
}

$parentNode = $paramTagValueNode->getAttribute(PhpDocAttributeKey::PARENT);
if (! $parentNode instanceof PhpDocTagNode) {
return true;
}

$parentNode = $parentNode->getAttribute(PhpDocAttributeKey::PARENT);
if (! $parentNode instanceof PhpDocNode) {
return true;
}

$children = $parentNode->children;

foreach ($children as $key => $child) {
if ($child instanceof PhpDocTagNode && $node instanceof FullyQualified) {
return $this->isUnionIdentifier($child);
}

if (! $this->isTextNextline($key, $child)) {
return false;
}
}

return true;
}

private function isTextNextline(int $key, PhpDocChildNode $phpDocChildNode): bool
{
if ($key < 1) {
return true;
}

if (! $phpDocChildNode instanceof PhpDocTextNode) {
return true;
}

return (string) $phpDocChildNode === '';
}

private function isUnionIdentifier(PhpDocTagNode $phpDocTagNode): bool
{
if (! $phpDocTagNode->value instanceof ParamTagValueNode) {
return true;
}

if (! $phpDocTagNode->value->type instanceof BracketsAwareUnionTypeNode) {
return true;
}

$types = $phpDocTagNode->value->type->types;
foreach ($types as $type) {
if ($type instanceof IdentifierTypeNode) {
return false;
}
}

return true;
}
}
6 changes: 6 additions & 0 deletions rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
Expand Down Expand Up @@ -42,6 +43,11 @@ public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $f
return null;
}

// skip union types
if ($docNode->value->type instanceof UnionTypeNode) {
return null;
}

if (! $this->deadParamTagValueNodeAnalyzer->isDead($docNode->value, $functionLike)) {
return null;
}
Expand Down

0 comments on commit 715a047

Please sign in to comment.