2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php71\Rector\BinaryOp;
|
2019-05-19 10:37:51 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Scalar\MagicConst\Line;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PHPStan\Type\Constant\ConstantStringType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\ExprAnalyzer;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-05-19 10:37:51 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/XPEEl
|
|
|
|
* @changelog https://3v4l.org/ObNQZ
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\BinaryOpBetweenNumberAndStringRectorTest
|
2019-05-19 10:37:51 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class BinaryOpBetweenNumberAndStringRector extends AbstractRector implements MinPhpVersionInterface
|
2019-05-19 10:37:51 +00:00
|
|
|
{
|
2021-12-04 13:03:42 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
2021-12-04 13:03:42 +00:00
|
|
|
*/
|
|
|
|
private $exprAnalyzer;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ExprAnalyzer $exprAnalyzer)
|
2021-12-04 13:03:42 +00:00
|
|
|
{
|
|
|
|
$this->exprAnalyzer = $exprAnalyzer;
|
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::BINARY_OP_NUMBER_STRING;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-05-19 10:37:51 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change binary operation between some number + string to PHP 7.1 compatible version', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-05-19 10:37:51 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = 5 + '';
|
2019-05-19 10:37:51 +00:00
|
|
|
$value = 5.0 + 'hi';
|
2019-05-19 10:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-05-19 10:37:51 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = 5 + 0;
|
2021-07-28 12:55:48 +00:00
|
|
|
$value = 5.0 + 0;
|
2019-05-19 10:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-05-19 10:37:51 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-05-19 10:37:51 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-05-19 10:37:51 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [BinaryOp::class];
|
2019-05-19 10:37:51 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param BinaryOp $node
|
2019-05-19 10:37:51 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-05-19 10:37:51 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Concat) {
|
2019-05-30 22:32:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Coalesce) {
|
2021-01-24 21:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-12-04 13:03:42 +00:00
|
|
|
if ($this->exprAnalyzer->isNonTypedFromParam($node->left)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->exprAnalyzer->isNonTypedFromParam($node->right)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-25 13:44:54 +00:00
|
|
|
if ($this->isStringOrStaticNonNumericString($node->left) && $this->nodeTypeResolver->isNumberType($node->right)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->left = new LNumber(0);
|
2019-05-19 10:37:51 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2023-10-25 13:44:54 +00:00
|
|
|
if ($this->isStringOrStaticNonNumericString($node->right) && $this->nodeTypeResolver->isNumberType($node->left)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->right = new LNumber(0);
|
2019-05-19 10:37:51 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-25 13:44:54 +00:00
|
|
|
private function isStringOrStaticNonNumericString(Expr $expr) : bool
|
2019-09-25 08:03:19 +00:00
|
|
|
{
|
2019-12-26 11:59:30 +00:00
|
|
|
// replace only scalar values, not variables/constants/etc.
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof Scalar && !$expr instanceof Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-12-26 11:59:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Line) {
|
2021-06-10 23:31:36 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof String_) {
|
2023-05-08 04:38:58 +00:00
|
|
|
return !\is_numeric($expr->value);
|
|
|
|
}
|
|
|
|
$exprStaticType = $this->getType($expr);
|
|
|
|
if ($exprStaticType instanceof ConstantStringType) {
|
|
|
|
return !\is_numeric($exprStaticType->getValue());
|
2019-09-25 08:03:19 +00:00
|
|
|
}
|
2023-05-08 04:38:58 +00:00
|
|
|
return \false;
|
2019-09-25 08:03:19 +00:00
|
|
|
}
|
2019-05-19 10:37:51 +00:00
|
|
|
}
|