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\Assign;
|
2018-10-09 10:18:42 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-10-09 10:18:42 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/a/41000866/1348344 https://3v4l.org/ABDNv
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php71\Rector\Assign\AssignArrayToStringRector\AssignArrayToStringRectorTest
|
2018-10-09 10:18:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AssignArrayToStringRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-09 10:18:42 +00:00
|
|
|
{
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_ASSIGN_ARRAY_TO_STRING;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-09 10:18:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('String cannot be turned into array by assignment anymore', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-09 10:18:42 +00:00
|
|
|
$string = '';
|
|
|
|
$string[] = 1;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-09 10:18:42 +00:00
|
|
|
$string = [];
|
|
|
|
$string[] = 1;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-09 10:18:42 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-09 10:18:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Assign::class, Property::class];
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Assign|Property $node
|
2018-10-09 10:18:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-09 10:18:42 +00:00
|
|
|
{
|
2021-07-15 03:40:51 +00:00
|
|
|
$defaultExpr = $this->resolveDefaultValueExpr($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$defaultExpr instanceof Expr) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
if (!$this->isEmptyString($defaultExpr)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
$assignedVar = $this->resolveAssignedVar($node);
|
|
|
|
// 1. variable!
|
|
|
|
$shouldRetype = \false;
|
|
|
|
/** @var array<Variable|PropertyFetch|StaticPropertyFetch> $exprUsages */
|
|
|
|
$exprUsages = $this->betterNodeFinder->findSameNamedExprs($assignedVar);
|
|
|
|
// detect if is part of variable assign?
|
|
|
|
foreach ($exprUsages as $exprUsage) {
|
2022-08-09 13:39:17 +00:00
|
|
|
$parentNode = $exprUsage->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if (!$parentNode instanceof ArrayDimFetch) {
|
2021-07-15 03:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
$firstAssign = $this->betterNodeFinder->findParentType($parentNode, Assign::class);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstAssign instanceof Assign) {
|
2021-07-15 03:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip explicit assigns
|
2022-08-09 13:39:17 +00:00
|
|
|
if ($parentNode->dim !== null) {
|
2021-07-15 03:40:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$shouldRetype = \true;
|
|
|
|
break;
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
if (!$shouldRetype) {
|
2021-03-24 22:36:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Property) {
|
|
|
|
$node->props[0]->default = new Array_();
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node;
|
2018-10-31 15:34:37 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->expr = new Array_();
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node;
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Stmt\Property $node
|
2018-10-09 10:18:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveDefaultValueExpr($node) : ?Expr
|
2018-10-09 10:18:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Property) {
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node->props[0]->default;
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node->expr;
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isEmptyString(Expr $expr) : bool
|
2019-01-18 02:41:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof String_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-03-10 23:47:43 +00:00
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
return $expr->value === '';
|
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Stmt\Property $node
|
2021-07-18 02:11:18 +00:00
|
|
|
* @return \PhpParser\Node\Expr|\PhpParser\Node\Stmt\Property
|
2021-07-15 03:40:51 +00:00
|
|
|
*/
|
|
|
|
private function resolveAssignedVar($node)
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Assign) {
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node->var;
|
2019-01-18 02:41:58 +00:00
|
|
|
}
|
2021-07-15 03:40:51 +00:00
|
|
|
return $node;
|
2019-01-18 02:41:58 +00:00
|
|
|
}
|
2018-10-09 10:18:42 +00:00
|
|
|
}
|