Skip to content

Commit

Permalink
[TypeDeclaration] Skip default not array type on StrictArrayParamDimF…
Browse files Browse the repository at this point in the history
…etchRector (#5259)

* [TypeDeclaration] Skip default not array type on StrictArrayParamDimFetchRector

* fixed 🎉
  • Loading branch information
samsonasik committed Nov 18, 2023
1 parent 4a3e698 commit 03c7ab7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
@@ -0,0 +1,15 @@
<?php

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

final class SkipDefaultNotArrayType
{
public function run($param = 'foo')
{
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}
@@ -0,0 +1,35 @@
<?php

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

final class WithDefaultArrayType
{
public function run($param = [])
{
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}

?>
-----
<?php

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

final class WithDefaultArrayType
{
public function run(array $param = [])
{
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}

?>
Expand Up @@ -5,6 +5,7 @@
namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\AssignOp\Coalesce as AssignOpCoalesce;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
Expand Down Expand Up @@ -87,6 +88,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($param->default instanceof Expr && ! $this->getType($param->default)->isArray()->yes()) {
continue;
}

if (! $this->isParamAccessedArrayDimFetch($param, $node)) {
continue;
}
Expand Down

0 comments on commit 03c7ab7

Please sign in to comment.