From 7e0859efe177331b53a327b60f5d6c48d7087bee Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Aug 2021 18:24:57 +0700 Subject: [PATCH] [DeadCode] Skip non-typed property for RecastingRemovalRector (#608) --- .../Fixture/skip_non_typed_property.php.inc | 16 ++++++++++ .../skip_non_typed_property.php2.php.inc | 19 +++++++++++ .../Fixture/typed_property.php.inc | 31 ++++++++++++++++++ .../Rector/Cast/RecastingRemovalRector.php | 32 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php.inc create mode 100644 rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php2.php.inc create mode 100644 rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/typed_property.php.inc diff --git a/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php.inc b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php.inc new file mode 100644 index 00000000000..079fcc94f7a --- /dev/null +++ b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php.inc @@ -0,0 +1,16 @@ +property; + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php2.php.inc b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php2.php.inc new file mode 100644 index 00000000000..e315215b369 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_non_typed_property.php2.php.inc @@ -0,0 +1,19 @@ +property; + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/typed_property.php.inc b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/typed_property.php.inc new file mode 100644 index 00000000000..b78b1a24f24 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/typed_property.php.inc @@ -0,0 +1,31 @@ +property; + } +} + +?> +----- +property; + } +} + +?> diff --git a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php index 9db7b9231f5..02d0168fc0f 100644 --- a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php +++ b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php @@ -5,6 +5,7 @@ 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_; @@ -12,6 +13,9 @@ 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; @@ -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; @@ -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', [ @@ -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; + } }