mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[DeadCode] Skip non-typed property for RecastingRemovalRector (#608)
This commit is contained in:
parent
6f0814042f
commit
7e0859efe1
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user