Skip to content

Commit

Permalink
[DeadCode] Skip RecastingRemovalRector on has __set() and __get() (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Aug 30, 2021
1 parent f257b10 commit 5058437
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class AConfig
{
private $data = [];

public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
}

public function __set($name, $value)
{
$this->data[$name] = (object) $value;
}
}

class SkipWithSetGetMagic
{
public function run(AConfig $config)
{
$config->property = [];
var_dump([] === (array) $config->property);
}
}

?>
10 changes: 9 additions & 1 deletion rules/DeadCode/Rector/Cast/RecastingRemovalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
Expand Down Expand Up @@ -121,7 +122,14 @@ private function shouldSkip(Expr $expr): bool
/** @var PropertyFetch|StaticPropertyFetch $expr */
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($expr);
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return false;
$propertyType = $expr instanceof StaticPropertyFetch
? $this->nodeTypeResolver->resolve($expr->class)
: $this->nodeTypeResolver->resolve($expr->var);

// need to UnionType check due rectify with RecastingRemovalRector + CountOnNullRector
// cause add (array) cast on $node->args
// on union $node types FuncCall|MethodCall|StaticCall
return ! $propertyType instanceof UnionType;
}

$nativeType = $phpPropertyReflection->getNativeType();
Expand Down

0 comments on commit 5058437

Please sign in to comment.