Skip to content

Commit 62e4045

Browse files
authored
[TypeDeclaration] Skip property fetch on array dim fetch in StrictArrayParamDimFetchRector (#8140)
1 parent 5f16b59 commit 62e4045

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
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 Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Source\Lead;
6+
7+
class SkipInstanceofParam
8+
{
9+
public function populateCompanyData($entity)
10+
{
11+
if ($entity instanceof Lead) {
12+
$fields = $entity->getPrimaryCompany();
13+
} else {
14+
$fields = $entity['primaryCompany'];
15+
}
16+
17+
return $fields;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;
4+
5+
class SkipPropertyFetchOnDimFetch
6+
{
7+
/**
8+
* @param object $object
9+
*/
10+
private function getDataForObject($object): mixed
11+
{
12+
foreach ($object as $key => $value) {
13+
if (0 === $key) {
14+
continue;
15+
}
16+
17+
$object[0]->$key = $value;
18+
}
19+
20+
return $object[0];
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Source;
6+
7+
final class Lead
8+
{
9+
/**
10+
* @return mixed[]
11+
*/
12+
public function getPrimaryCompany(): array
13+
{
14+
return [];
15+
}
16+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
use PhpParser\Node\Expr\Closure;
1717
use PhpParser\Node\Expr\Empty_;
1818
use PhpParser\Node\Expr\FuncCall;
19+
use PhpParser\Node\Expr\Instanceof_;
1920
use PhpParser\Node\Expr\MethodCall;
21+
use PhpParser\Node\Expr\PropertyFetch;
22+
use PhpParser\Node\Expr\StaticPropertyFetch;
2023
use PhpParser\Node\Expr\Variable;
2124
use PhpParser\Node\FunctionLike;
2225
use PhpParser\Node\Identifier;
@@ -244,6 +247,14 @@ private function shouldStop(Node $node, string $paramName): bool
244247
return true;
245248
}
246249

250+
if ($this->isPropertyFetchedOnArrayDimFetch($node, $paramName)) {
251+
return true;
252+
}
253+
254+
if ($this->isInstanceofParam($node, $paramName)) {
255+
return true;
256+
}
257+
247258
return $this->isReassignAndUseAsArg($node, $paramName);
248259
}
249260

@@ -288,6 +299,28 @@ private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool
288299
return $node instanceof Array_ && $node->expr instanceof Variable && $this->isName($node->expr, $paramName);
289300
}
290301

302+
private function isPropertyFetchedOnArrayDimFetch(Node $node, string $paramName): bool
303+
{
304+
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
305+
return false;
306+
}
307+
308+
$fetchedOn = $node instanceof PropertyFetch ? $node->var : $node->class;
309+
if (! $fetchedOn instanceof ArrayDimFetch) {
310+
return false;
311+
}
312+
313+
return $fetchedOn->var instanceof Variable && $this->isName($fetchedOn->var, $paramName);
314+
}
315+
316+
private function isInstanceofParam(Node $node, string $paramName): bool
317+
{
318+
return $node instanceof Instanceof_ && $node->expr instanceof Variable && $this->isName(
319+
$node->expr,
320+
$paramName
321+
);
322+
}
323+
291324
private function isMethodCall(string $paramName, ?Node $node): bool
292325
{
293326
if ($node instanceof MethodCall) {

0 commit comments

Comments
 (0)