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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,18 @@
namespace Rector\DeadCode\Rector\ClassLike;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\Enum\ClassName;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector\RemoveTypedPropertyNonMockDocblockRectorTest
* @deprecated This rule is deprecated as it has no real value, removing a single narrow @var docblock combination on a typed property.
*/
final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector implements MinPhpVersionInterface
final class RemoveTypedPropertyNonMockDocblockRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly VarTagRemover $varTagRemover,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -79,73 +60,6 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node, new ObjectType(ClassName::TEST_CASE_CLASS))) {
return null;
}

$hasChanged = false;

foreach ($node->getProperties() as $property) {
// not yet typed
if (! $property->type instanceof Node) {
continue;
}

if (count($property->props) !== 1) {
continue;
}

if (! $property->type instanceof FullyQualified) {
continue;
}

if ($this->isObjectType($property->type, new ObjectType(ClassName::MOCK_OBJECT))) {
continue;
}

$propertyDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

if (! $this->isVarTagUnionTypeMockObject($propertyDocInfo, $property)) {
continue;
}

// clear var docblock
if ($this->varTagRemover->removeVarTag($property)) {
$hasChanged = true;
}
}

if (! $hasChanged) {
return null;
}

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
}

private function isVarTagUnionTypeMockObject(PhpDocInfo $phpDocInfo, Property $property): bool
{
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return false;
}

if (! $varTagValueNode->type instanceof UnionTypeNode) {
return false;
}

$varTagType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
if (! $varTagType instanceof UnionType) {
return false;
}

return array_any(
$varTagType->getTypes(),
fn (Type $unionedType): bool => $unionedType->isSuperTypeOf(new ObjectType(ClassName::MOCK_OBJECT))->yes()
);
throw new ShouldNotHappenException(sprintf('"%s" is deprecated as it has no real value', self::class));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector;
use Rector\DeadCode\Rector\ClassLike\RemoveTypedPropertyNonMockDocblockRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveArgumentFromDefaultParentCallRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveDuplicatedReturnSelfDocblockRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
Expand Down Expand Up @@ -126,7 +125,6 @@ final class DeadCodeLevel
// prioritize safe belt on RemoveUseless*TagRector that registered previously first
RemoveNullTagValueNodeRector::class,
RemovePhpVersionIdCheckRector::class,
RemoveTypedPropertyNonMockDocblockRector::class,

RemoveAlwaysTrueIfConditionRector::class,
ReduceAlwaysFalseIfOrRector::class,
Expand Down
Loading