Skip to content

Commit

Permalink
[DeadCode] Skip non-typed property for RecastingRemovalRector (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Aug 7, 2021
1 parent 6f08140 commit 7e0859e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Rector;

class SkipNonTypedProperty
{
/** @var int */
public $property = 1;

public function run()
{
$value = (int) $this->property;
}
}

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

namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Rector;

class Config
{
/** @var int */
public $property = 1;
}

class SkipNonTypedProperty2
{
public function run(Config $config)
{
$value = (int) $config->property;
}
}

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

namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Rector;

class TypedProperty
{
public int $property = 1;

public function run()
{
$value = (int) $this->property;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Rector;

class TypedProperty
{
public int $property = 1;

public function run()
{
$value = $this->property;
}
}

?>
32 changes: 32 additions & 0 deletions rules/DeadCode/Rector/Cast/RecastingRemovalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
namespace Rector\DeadCode\Rector\Cast;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Cast\Double;
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\Object_;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
Expand All @@ -20,7 +24,9 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -41,6 +47,12 @@ final class RecastingRemovalRector extends AbstractRector
Double::class => FloatType::class,
];

public function __construct(
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private ReflectionResolver $reflectionResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Removes recasting of the same type', [
Expand Down Expand Up @@ -92,6 +104,26 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkip($node->expr)) {
return null;
}

return $node->expr;
}

private function shouldSkip(Expr $expr): bool
{
if (! $this->propertyFetchAnalyzer->isPropertyFetch($expr)) {
return false;
}

/** @var PropertyFetch|StaticPropertyFetch $expr */
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($expr);
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return false;
}

$nativeType = $phpPropertyReflection->getNativeType();
return $nativeType instanceof MixedType;
}
}

0 comments on commit 7e0859e

Please sign in to comment.