Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Source\Lead;

class SkipInstanceofParam
{
public function populateCompanyData($entity)
{
if ($entity instanceof Lead) {
$fields = $entity->getPrimaryCompany();
} else {
$fields = $entity['primaryCompany'];
}

return $fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

class SkipPropertyFetchOnDimFetch
{
/**
* @param object $object
*/
private function getDataForObject($object): mixed
{
foreach ($object as $key => $value) {
if (0 === $key) {
continue;
}

$object[0]->$key = $value;
}

return $object[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Source;

final class Lead
{
/**
* @return mixed[]
*/
public function getPrimaryCompany(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -244,6 +247,14 @@ private function shouldStop(Node $node, string $paramName): bool
return true;
}

if ($this->isPropertyFetchedOnArrayDimFetch($node, $paramName)) {
return true;
}

if ($this->isInstanceofParam($node, $paramName)) {
return true;
}

return $this->isReassignAndUseAsArg($node, $paramName);
}

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

private function isPropertyFetchedOnArrayDimFetch(Node $node, string $paramName): bool
{
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
return false;
}

$fetchedOn = $node instanceof PropertyFetch ? $node->var : $node->class;
if (! $fetchedOn instanceof ArrayDimFetch) {
return false;
}

return $fetchedOn->var instanceof Variable && $this->isName($fetchedOn->var, $paramName);
}

private function isInstanceofParam(Node $node, string $paramName): bool
{
return $node instanceof Instanceof_ && $node->expr instanceof Variable && $this->isName(
$node->expr,
$paramName
);
}

private function isMethodCall(string $paramName, ?Node $node): bool
{
if ($node instanceof MethodCall) {
Expand Down
Loading