Skip to content
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,11 @@
<?php

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

/**
* @return static|null
*/
function skipStaticFunction(): ?SomeStaticInterface
{
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

interface SomeStaticInterface
{
}

trait SkipStaticUnionTrait
{
private $parent;

/**
* @return static|null
*/
public function getParent(): ?SomeStaticInterface
{
return $this->parent;
}

/**
* @return $this|null
*/
public function getThisParent(): ?SomeStaticInterface
{
return $this->parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand Down Expand Up @@ -104,6 +106,11 @@ public function refactor(Node $node): ?Node
return null;
}

// keep static/$this, they denote the runtime subclass the native type cannot express
if ($this->hasStaticOrThisType($returnTagValueNode->type)) {
return null;
}

$nativeReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->returnType);

// keep array-involving native type (array, ?array, union with array), the docblock may carry element types
Expand Down Expand Up @@ -131,4 +138,19 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::UNION_TYPES;
}

private function hasStaticOrThisType(UnionTypeNode $unionTypeNode): bool
{
foreach ($unionTypeNode->types as $typeNode) {
if ($typeNode instanceof ThisTypeNode) {
return true;
}

if ($typeNode instanceof IdentifierTypeNode && $typeNode->name === 'static') {
return true;
}
}

return false;
}
}
Loading