Repository navigation
Expand file tree
/
Copy pathDowngradeIteratorCountToArrayRector.php
More file actions
137 lines (116 loc) · 3.8 KB
/
Copy pathDowngradeIteratorCountToArrayRector.php
File metadata and controls
137 lines (116 loc) · 3.8 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
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp82\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\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,
) {
}
/**
* @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 FuncCall $node
*/
public function refactor(Node $node): null|FuncCall
{
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;
}
// already wrapped in a previous pass; the wrap type stays not-yes-object (e.g. ArrayIterator|mixed),
// so without this check it would be wrapped again on every pass
if ($this->isArrayIteratorWrapped($arg->value)) {
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 isArrayIteratorWrapped(Expr $expr): bool
{
if (! $expr instanceof Ternary) {
return false;
}
if (! $expr->cond instanceof FuncCall || ! $this->isName($expr->cond, 'is_array')) {
return false;
}
if (! $expr->if instanceof New_) {
return false;
}
return $this->isName($expr->if->class, 'ArrayIterator');
}
private function shouldSkip(Type $type): bool
{
if ($type->isArray()->yes()) {
return false;
}
$type = TypeCombinator::removeNull($type);
return $type->isObject()
->yes();
}
}