Skip to content

Commit

Permalink
[DeadCode] Skip Union with Interface on RemoveUselessParamTagRector (#…
Browse files Browse the repository at this point in the history
…1127)

* [DeadCode] Skip Union with Interface on RemoveUselessParamTagRector

* Fixed 🎉

* [ci-review] Rector Rectify

* eol

* union type

* fix

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* add failing fixture for add some comment before param

* fix

* clean up

* clean up

* [ci-review] Rector Rectify

* rectify

* clean up

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 2, 2021
1 parent 2b09156 commit 8e9ef87
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types = 1);

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Source\SomeInterface;
use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Source\SomeClass;

class SkipUnionOfInterface {
/**
* @param SomeClass|SomeInterface $someInterface
*/
public function run(SomeInterface $someInterface)
{
if (method_exists($someInterface, 'run')) {
$someInterface->run();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Source\SomeInterface;
use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Source\SomeClass;

class SkipUnionOfInterface {
/**
* Some comment
*
* @param SomeClass|SomeInterface $someInterface
*/
public function run(SomeInterface $someInterface)
{
if (method_exists($someInterface, 'run')) {
$someInterface->run();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

class SomeClass implements SomeInterface
{
public function run()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

interface SomeInterface
{
}
51 changes: 45 additions & 6 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

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\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\VariadicAwareParamTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
Expand Down Expand Up @@ -58,17 +62,17 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
}

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

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

return false;
}

private function isEmptyDescription(ParamTagValueNode $paramTagValueNode): bool
private function isEmptyDescription(ParamTagValueNode $paramTagValueNode, Node $node): bool
{
if ($paramTagValueNode->description !== '') {
return false;
Expand All @@ -86,15 +90,50 @@ private function isEmptyDescription(ParamTagValueNode $paramTagValueNode): bool

$children = $parent->children;

if (! isset($children[1])) {
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 (! $children[1] instanceof PhpDocTextNode) {
if (! $phpDocChildNode instanceof PhpDocTextNode) {
return true;
}

return (string) $children[1] === '';
return (string) $phpDocChildNode === '';
}

private function isUnionIdentifier(PhpDocTagNode $phpDocTagNode): bool
{
if (! $phpDocTagNode->value instanceof VariadicAwareParamTagValueNode) {
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;
}

private function hasGenericType(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
Expand Down

0 comments on commit 8e9ef87

Please sign in to comment.