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\CodingStyle\Rector\Class_;
|
2019-08-15 11:27:39 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Array_;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\Stmt\PropertyProperty;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\CodingStyle\TypeAnalyzer\IterableTypeAnalyzer;
|
|
|
|
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
|
|
|
|
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-08-15 11:27:39 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector\AddArrayDefaultToArrayPropertyRectorTest
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/dPlUg
|
2019-08-15 11:27:39 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class AddArrayDefaultToArrayPropertyRector extends AbstractRector
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2019-08-18 12:15:39 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
|
2019-08-18 12:15:39 +00:00
|
|
|
*/
|
2021-02-18 20:34:07 +00:00
|
|
|
private $propertyFetchAnalyzer;
|
2020-12-23 21:11:37 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\TypeAnalyzer\IterableTypeAnalyzer
|
2020-12-23 21:11:37 +00:00
|
|
|
*/
|
|
|
|
private $iterableTypeAnalyzer;
|
2021-09-27 15:43:15 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-09-27 15:43:15 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
|
|
|
|
*/
|
|
|
|
private $argsAnalyzer;
|
2022-04-29 13:52:53 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
|
|
|
*/
|
|
|
|
private $visibilityManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, IterableTypeAnalyzer $iterableTypeAnalyzer, ArgsAnalyzer $argsAnalyzer, VisibilityManipulator $visibilityManipulator)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-02-18 20:34:07 +00:00
|
|
|
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
2020-12-23 21:11:37 +00:00
|
|
|
$this->iterableTypeAnalyzer = $iterableTypeAnalyzer;
|
2021-09-27 15:43:15 +00:00
|
|
|
$this->argsAnalyzer = $argsAnalyzer;
|
2022-04-29 13:52:53 +00:00
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Adds array default value to property to prevent foreach over null error', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-08-15 11:27:39 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $values;
|
|
|
|
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return $this->values === null;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-08-15 11:27:39 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $values = [];
|
|
|
|
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return $this->values === [];
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-08-15 11:27:39 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2019-08-15 11:27:39 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
|
|
|
$changedProperties = $this->collectPropertyNamesWithMissingDefaultArray($node);
|
2020-02-02 18:15:36 +00:00
|
|
|
if ($changedProperties === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-15 11:27:39 +00:00
|
|
|
$this->completeDefaultArrayToPropertyNames($node, $changedProperties);
|
|
|
|
// $this->variable !== null && count($this->variable) > 0 → count($this->variable) > 0
|
|
|
|
$this->clearNotNullBeforeCount($node, $changedProperties);
|
|
|
|
// $this->variable === null → $this->variable === []
|
|
|
|
$this->replaceNullComparisonOfArrayPropertiesWithArrayComparison($node, $changedProperties);
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectPropertyNamesWithMissingDefaultArray(Class_ $class) : array
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
|
|
|
$propertyNames = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($class, function (Node $node) use(&$propertyNames) {
|
|
|
|
if (!$node instanceof PropertyProperty) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-10-30 14:38:30 +00:00
|
|
|
if ($node->default !== null) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-12-23 21:11:37 +00:00
|
|
|
$varType = $this->resolveVarType($node);
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->iterableTypeAnalyzer->detect($varType)) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$property = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if (!$property instanceof Property) {
|
2022-04-29 13:52:53 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->visibilityManipulator->isReadonly($property)) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-15 11:27:39 +00:00
|
|
|
$propertyNames[] = $this->getName($node);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
return $propertyNames;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string[] $propertyNames
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function completeDefaultArrayToPropertyNames(Class_ $class, array $propertyNames) : void
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?PropertyProperty {
|
|
|
|
if (!$node instanceof PropertyProperty) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-10 08:30:09 +00:00
|
|
|
if (!$this->isNames($node, $propertyNames)) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->default = new Array_();
|
2022-04-10 08:30:09 +00:00
|
|
|
return $node;
|
2019-08-15 11:27:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string[] $propertyNames
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function clearNotNullBeforeCount(Class_ $class, array $propertyNames) : void
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?Expr {
|
|
|
|
if (!$node instanceof BooleanAnd) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isLocalPropertyOfNamesNotIdenticalToNull($node->left, $propertyNames)) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$isNextNodeCountingProperty = (bool) $this->betterNodeFinder->findFirst($node->right, function (Node $node) use($propertyNames) : bool {
|
|
|
|
if (!$node instanceof FuncCall) {
|
2022-01-07 19:48:45 +00:00
|
|
|
return \false;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'count')) {
|
2022-01-07 19:48:45 +00:00
|
|
|
return \false;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
if (!$this->argsAnalyzer->isArgInstanceInArgsPosition($node->args, 0)) {
|
2022-01-07 19:48:45 +00:00
|
|
|
return \false;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
/** @var Arg $firstArg */
|
|
|
|
$firstArg = $node->args[0];
|
|
|
|
$countedArgument = $firstArg->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$countedArgument instanceof PropertyFetch) {
|
2022-01-07 19:48:45 +00:00
|
|
|
return \false;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
|
|
|
return $this->isNames($countedArgument, $propertyNames);
|
|
|
|
});
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$isNextNodeCountingProperty) {
|
2019-08-15 11:27:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $node->right;
|
|
|
|
});
|
|
|
|
}
|
2019-10-30 09:49:07 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $propertyNames
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function replaceNullComparisonOfArrayPropertiesWithArrayComparison(Class_ $class, array $propertyNames) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-10-30 09:49:07 +00:00
|
|
|
// replace comparison to "null" with "[]"
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?BinaryOp {
|
|
|
|
if (!$node instanceof BinaryOp) {
|
2019-10-30 09:49:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($node->left, $propertyNames) && $this->valueResolver->isNull($node->right)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->right = new Array_();
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($node->right, $propertyNames) && $this->valueResolver->isNull($node->left)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->left = new Array_();
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
});
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveVarType(PropertyProperty $propertyProperty) : Type
|
2020-12-23 21:11:37 +00:00
|
|
|
{
|
2022-08-09 13:39:17 +00:00
|
|
|
/** @var Property $propertyNode */
|
|
|
|
$propertyNode = $propertyProperty->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($propertyNode);
|
2021-01-18 21:45:13 +00:00
|
|
|
return $phpDocInfo->getVarType();
|
2020-12-23 21:11:37 +00:00
|
|
|
}
|
2019-08-15 11:27:39 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $propertyNames
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isLocalPropertyOfNamesNotIdenticalToNull(Expr $expr, array $propertyNames) : bool
|
2019-08-15 11:27:39 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof NotIdentical) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($expr->left, $propertyNames) && $this->valueResolver->isNull($expr->right)) {
|
|
|
|
return \true;
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->propertyFetchAnalyzer->isLocalPropertyOfNames($expr->right, $propertyNames)) {
|
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
2021-01-30 23:20:05 +00:00
|
|
|
return $this->valueResolver->isNull($expr->left);
|
2019-08-15 11:27:39 +00:00
|
|
|
}
|
|
|
|
}
|