[DeadCode] Skip non-typed property for RecastingRemovalRector (#608)

This commit is contained in:
Abdul Malik Ikhsan 2021-08-07 18:24:57 +07:00 committed by GitHub
parent 6f0814042f
commit 7e0859efe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 0 deletions

View File

@ -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;
}
}
?>

View File

@ -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;
}
}
?>

View File

@ -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;
}
}
?>

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
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\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;
@ -20,7 +24,9 @@ use PHPStan\Type\MixedType;
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 @@ CODE_SAMPLE
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;
}
}