mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DowngradePhp74\Rector\LNumber;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Scalar\DNumber;
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @changelog https://wiki.php.net/rfc/numeric_literal_separator
|
|
*
|
|
* @see \Rector\Tests\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector\DowngradeNumericLiteralSeparatorRectorTest
|
|
*/
|
|
final class DowngradeNumericLiteralSeparatorRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove "_" as thousands separator in numbers', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$int = 1_000;
|
|
$float = 1_000_500.001;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$int = 1000;
|
|
$float = 1000500.001;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Scalar\LNumber::class, \PhpParser\Node\Scalar\DNumber::class];
|
|
}
|
|
/**
|
|
* @param LNumber|DNumber $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->shouldRefactor($node)) {
|
|
return null;
|
|
}
|
|
$numberNode = clone $node;
|
|
$numberNodeValue = (string) $numberNode->value;
|
|
if (\strpos($numberNodeValue, '+') !== \false) {
|
|
return null;
|
|
}
|
|
$node->value = (string) $node->value;
|
|
/**
|
|
* This code follows a guess, to avoid modifying floats needlessly.
|
|
* If the node is a float, but it doesn't contain ".",
|
|
* then it's likely that the number was forced to be a float
|
|
* by adding ".0" at the end (eg: 0.0).
|
|
* Then, add it again.
|
|
*/
|
|
if ($node instanceof \PhpParser\Node\Scalar\DNumber && \strpos($node->value, '.') === \false) {
|
|
$node->value .= '.0';
|
|
}
|
|
return $node;
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Scalar\LNumber|\PhpParser\Node\Scalar\DNumber $node
|
|
*/
|
|
public function shouldRefactor($node) : bool
|
|
{
|
|
// "_" notation can be applied to decimal numbers only
|
|
if ($node instanceof \PhpParser\Node\Scalar\LNumber) {
|
|
return $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::KIND) === \PhpParser\Node\Scalar\LNumber::KIND_DEC;
|
|
}
|
|
return \true;
|
|
}
|
|
}
|