Skip to content

Commit 613c740

Browse files
authored
[TypeDeclaration] Skip private method param called with ArrayAccess arg in StrictArrayParamDimFetchRector (#8648)
1 parent 274b758 commit 613c740

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
4+
5+
use ArrayAccess;
6+
7+
class SkipPrivateMethodCalledWithArrayAccessArg
8+
{
9+
public function run(ArrayAccess $arrayAccess, array $data): void
10+
{
11+
$this->isTrue($arrayAccess);
12+
$this->isTrue($data);
13+
}
14+
15+
private function isTrue($value): bool
16+
{
17+
return $value['asdf'] === true;
18+
}
19+
}

‎rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\Node\Expr\Instanceof_;
2020
use PhpParser\Node\Expr\MethodCall;
2121
use PhpParser\Node\Expr\PropertyFetch;
22+
use PhpParser\Node\Expr\StaticCall;
2223
use PhpParser\Node\Expr\StaticPropertyFetch;
2324
use PhpParser\Node\Expr\Variable;
2425
use PhpParser\Node\FunctionLike;
@@ -35,6 +36,7 @@
3536
use PHPStan\Type\UnionType;
3637
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
3738
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
39+
use Rector\PhpParser\Node\BetterNodeFinder;
3840
use Rector\Rector\AbstractRector;
3941
use Rector\RuleDoc\CodeSample\CodeSample;
4042
use Rector\RuleDoc\RuleDefinition;
@@ -48,7 +50,8 @@ final class StrictArrayParamDimFetchRector extends AbstractRector
4850
public function __construct(
4951
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
5052
private readonly TypeComparator $typeComparator,
51-
private readonly TypeFactory $typeFactory
53+
private readonly TypeFactory $typeFactory,
54+
private readonly BetterNodeFinder $betterNodeFinder
5255
) {
5356
}
5457

@@ -111,7 +114,7 @@ public function refactor(Node $node): ?Node
111114
return null;
112115
}
113116

114-
foreach ($node->getParams() as $param) {
117+
foreach ($node->getParams() as $position => $param) {
115118
if ($param->type instanceof Node) {
116119
continue;
117120
}
@@ -128,6 +131,15 @@ public function refactor(Node $node): ?Node
128131
continue;
129132
}
130133

134+
// a private method is only reachable from inside the class, so all its callers are visible here;
135+
// skip if any of them passes an ArrayAccess object, which also supports dim fetch
136+
if ($node instanceof ClassMethod && $node->isPrivate() && $this->isCalledWithArrayAccessArg(
137+
$node,
138+
$position
139+
)) {
140+
continue;
141+
}
142+
131143
$param->type = new Identifier('array');
132144
$hasChanged = true;
133145
}
@@ -339,6 +351,51 @@ private function isMethodCall(string $paramName, ?Node $node): bool
339351
return false;
340352
}
341353

354+
private function isCalledWithArrayAccessArg(ClassMethod $classMethod, int $position): bool
355+
{
356+
$class = $this->findClassOfMethod($classMethod);
357+
if (! $class instanceof Class_) {
358+
return false;
359+
}
360+
361+
$methodName = $this->getName($classMethod);
362+
363+
$calls = $this->betterNodeFinder->findInstancesOf($class->getMethods(), [MethodCall::class, StaticCall::class]);
364+
365+
return array_any(
366+
$calls,
367+
function (MethodCall|StaticCall $call) use ($methodName, $position): bool {
368+
if ($call->isFirstClassCallable()) {
369+
return false;
370+
}
371+
372+
if (! $this->isName($call->name, $methodName)) {
373+
return false;
374+
}
375+
376+
$args = $call->getArgs();
377+
if (! isset($args[$position])) {
378+
return false;
379+
}
380+
381+
return $this->isArrayAccess($this->getType($args[$position]->value));
382+
}
383+
);
384+
}
385+
386+
private function findClassOfMethod(ClassMethod $classMethod): ?Class_
387+
{
388+
$classes = $this->betterNodeFinder->findInstanceOf($this->getFile()->getNewStmts(), Class_::class);
389+
390+
foreach ($classes as $class) {
391+
if (in_array($classMethod, $class->getMethods(), true)) {
392+
return $class;
393+
}
394+
}
395+
396+
return null;
397+
}
398+
342399
private function isArrayAccess(Type $type): bool
343400
{
344401
if (! $type instanceof ObjectType) {

0 commit comments

Comments
 (0)