-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeIteratorCountToArrayRector.php
More file actions
130 lines (111 loc) · 3.74 KB
/
DowngradeIteratorCountToArrayRector.php
File metadata and controls
130 lines (111 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp82\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\NodeVisitor;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
/**
* @changelog https://www.php.net/manual/en/migration82.other-changes.php#migration82.other-changes.functions.spl
*
* @see \Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\DowngradeIteratorCountToArrayRectorTest
* @see https://3v4l.org/TPW7a#v8.1.31
*/
final class DowngradeIteratorCountToArrayRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Ternary::class, FuncCall::class];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Ensure pass Traversable instance before use in iterator_count() and iterator_to_array()',
[
new CodeSample(
<<<'CODE_SAMPLE'
function test(array|Traversable $data) {
$c = iterator_count($data);
$v = iterator_to_array($data);
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function test(array|Traversable $data) {
$c = iterator_count(is_array($data) ? new ArrayIterator($data) : $data);
$v = iterator_to_array(is_array($data) ? new ArrayIterator($data) : $data);
}
CODE_SAMPLE
),
]
);
}
/**
* @param Ternary|FuncCall $node
* @return null|FuncCall|NodeVisitor::DONT_TRAVERSE_CHILDREN
*/
public function refactor(Node $node): null|FuncCall|int
{
if ($node instanceof Ternary) {
$hasIsArrayCheck = (bool) $this->betterNodeFinder->findFirst(
$node,
fn (Node $subNode): bool => $subNode instanceof FuncCall && $this->isName($subNode, 'is_array')
);
if ($hasIsArrayCheck) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return null;
}
if (! $this->isNames($node, ['iterator_count', 'iterator_to_array'])) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$arg = $node->getArg('iterator', 0);
if (! $arg instanceof Arg) {
return null;
}
$type = $this->nodeTypeResolver->getType($arg->value);
if ($this->shouldSkip($type)) {
return null;
}
$position = $this->argsAnalyzer->resolveArgPosition($node->getArgs(), 'iterator', 0);
Assert::isInstanceOf($node->args[$position], Arg::class);
$firstValue = $node->args[$position]->value;
$node->args[$position]->value = new Ternary(
$this->nodeFactory->createFuncCall('is_array', [new Arg($firstValue)]),
new New_(new FullyQualified('ArrayIterator'), [new Arg($firstValue)]),
$firstValue
);
return $node;
}
private function shouldSkip(Type $type): bool
{
if ($type->isArray()->yes()) {
return false;
}
$type = TypeCombinator::removeNull($type);
return $type->isObject()
->yes();
}
}