rector/rules/Php70/Rector/Assign/ListSplitStringRector.php

53 lines
1.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Php70\Rector\Assign;
2018-10-07 09:53:59 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\List_;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2018-10-07 09:53:59 +00:00
/**
2021-04-19 16:15:52 +00:00
* @changelog http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.variable-handling.list
2018-10-07 09:53:59 +00:00
*
2021-04-10 18:47:17 +00:00
* @changelog https://stackoverflow.com/a/47965344/1348344
* @see \Rector\Tests\Php70\Rector\Assign\ListSplitStringRector\ListSplitStringRectorTest
2018-10-07 09:53:59 +00:00
*/
final class ListSplitStringRector extends AbstractRector implements MinPhpVersionInterface
2018-10-07 09:53:59 +00:00
{
public function getRuleDefinition() : RuleDefinition
2018-10-07 09:53:59 +00:00
{
return new RuleDefinition('list() cannot split string directly anymore, use str_split()', [new CodeSample('list($foo) = "string";', 'list($foo) = str_split("string");')]);
2018-10-07 09:53:59 +00:00
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NO_LIST_SPLIT_STRING;
}
2018-10-07 09:53:59 +00:00
/**
* @return array<class-string<Node>>
2018-10-07 09:53:59 +00:00
*/
public function getNodeTypes() : array
2018-10-07 09:53:59 +00:00
{
return [Assign::class];
2018-10-07 09:53:59 +00:00
}
/**
* @param Assign $node
2018-10-07 09:53:59 +00:00
*/
public function refactor(Node $node) : ?Node
2018-10-07 09:53:59 +00:00
{
if (!$node->var instanceof List_) {
return null;
2018-10-07 09:53:59 +00:00
}
$exprType = $this->getType($node->expr);
if (!$exprType->isString()->yes()) {
return null;
2018-10-07 09:53:59 +00:00
}
2021-01-30 21:41:25 +00:00
$node->expr = $this->nodeFactory->createFuncCall('str_split', [$node->expr]);
return $node;
2018-10-07 09:53:59 +00:00
}
}