rector/rules/Php74/Rector/Double/RealToFloatTypeCastRector.php

71 lines
1.8 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php74\Rector\Double;
2019-01-08 10:37:34 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Double;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2019-01-08 10:37:34 +00:00
/**
2021-04-10 18:47:17 +00:00
* @changelog https://wiki.php.net/rfc/deprecations_php_7_4
* @see \Rector\Tests\Php74\Rector\Double\RealToFloatTypeCastRector\RealToFloatTypeCastRectorTest
2019-01-08 10:37:34 +00:00
*/
final class RealToFloatTypeCastRector extends AbstractRector implements MinPhpVersionInterface
2019-01-08 10:37:34 +00:00
{
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::DEPRECATE_REAL;
}
public function getRuleDefinition() : RuleDefinition
2019-01-08 10:37:34 +00:00
{
return new RuleDefinition('Change deprecated (real) to (float)', [new CodeSample(<<<'CODE_SAMPLE'
2019-01-08 10:37:34 +00:00
class SomeClass
{
public function run()
{
$number = (real) 5;
$number = (float) 5;
$number = (double) 5;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2019-01-08 10:37:34 +00:00
class SomeClass
{
public function run()
{
$number = (float) 5;
$number = (float) 5;
$number = (double) 5;
}
}
CODE_SAMPLE
)]);
2019-01-08 10:37:34 +00:00
}
/**
* @return array<class-string<Node>>
2019-01-08 10:37:34 +00:00
*/
public function getNodeTypes() : array
2019-01-08 10:37:34 +00:00
{
return [Double::class];
2019-01-08 10:37:34 +00:00
}
/**
* @param Double $node
2019-01-08 10:37:34 +00:00
*/
public function refactor(Node $node) : ?Node
2019-01-08 10:37:34 +00:00
{
$kind = $node->getAttribute(AttributeKey::KIND);
if ($kind !== Double::KIND_REAL) {
2019-01-08 10:37:34 +00:00
return null;
}
$node->setAttribute(AttributeKey::KIND, Double::KIND_FLOAT);
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
2019-01-08 10:37:34 +00:00
return $node;
}
}