Skip to content

Commit

Permalink
[TypeDeclaration] Skip coalesce on StrictArrayParamDimFetchRector (#4589
Browse files Browse the repository at this point in the history
)

* [TypeDeclaration] Skip coalesce on StrictArrayParamDimFetchRector

* Fix

* assingop

* stop early

* fix

* fix phpstan
  • Loading branch information
samsonasik committed Jul 24, 2023
1 parent e9e6a42 commit 6658a0e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
16 changes: 16 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,19 @@ parameters:

# deprecated
- '#Fetching class constant class of deprecated class Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector#'

-
message: '#Public method "AutoloadIncluder\:\:.*\(\)" is never used#'
path: bin/rector.php

-
message: '#Public method "Rector\\Core\\Bootstrap\\RectorConfigsResolver\:\:provide\(\)" is never used#'
path: src/Bootstrap/RectorConfigsResolver.php

-
message: '#Public method "Rector\\Core\\Console\\Style\\RectorConsoleOutputStyleFactory\:\:create\(\)" is never used#'
path: src/Console/Style/RectorConsoleOutputStyleFactory.php

-
message: '#Public method "Rector\\Core\\DependencyInjection\\RectorContainerFactory\:\:createFromBootstrapConfigs\(\)" is never used#'
path: src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class SkipCoalesce
{
public function run($productOptions)
{
$productOptions = $productOptions ?? [];

$productOptions['list'] = $productOptions['list'] ?? 'list';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class SkipCoalesce2
{
public function run($productOptions)
{
$productOptions ??= [];

$productOptions['list'] = $productOptions['list'] ?? 'list';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\AssignOp\Coalesce as AssignOpCoalesce;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -100,19 +102,14 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
$paramName = $this->getName($param);

$isParamAccessedArrayDimFetch = false;

$this->traverseNodesWithCallable($functionLike, function (Node $node) use (
$paramName,
&$isParamAccessedArrayDimFetch
&$isParamAccessedArrayDimFetch,
): int|null {
if ($node instanceof FuncCall && $this->isNames(
$node,
['is_array', 'is_string', 'is_int', 'is_bool', 'is_float']
)) {
$firstArg = $node->getArgs()[0];
if ($this->isName($firstArg->value, $paramName)) {
return NodeTraverser::STOP_TRAVERSAL;
}
if ($this->shouldStop($node, $paramName)) {
// force set to false to avoid too early replaced
$isParamAccessedArrayDimFetch = false;
return NodeTraverser::STOP_TRAVERSAL;
}

if (! $node instanceof ArrayDimFetch) {
Expand All @@ -133,4 +130,21 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function

return $isParamAccessedArrayDimFetch;
}

private function shouldStop(Node $node, string $paramName): bool
{
if ($node instanceof FuncCall
&& ! $node->isFirstClassCallable()
&& $this->isNames($node, ['is_array', 'is_string', 'is_int', 'is_bool', 'is_float'])) {

$firstArg = $node->getArgs()[0];
return $firstArg->value instanceof Variable && $this->isName($firstArg->value, $paramName);
}

if ($node instanceof Coalesce && $node->left instanceof Variable && $this->isName($node->left, $paramName)) {
return true;
}

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

0 comments on commit 6658a0e

Please sign in to comment.