Skip to content

Commit

Permalink
[TypeDeclaration] Skip with empty() check on StrictArrayParamDimFetch…
Browse files Browse the repository at this point in the history
…Rector (#5432)

* [TypeDeclaration] Skip with empty() check on StrictArrayParamDimFetchRector

* fix

* remove composer phpstan-config command
  • Loading branch information
samsonasik committed Jan 5, 2024
1 parent 7654e2b commit 971571b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ jobs:
name: 'PHPStan'
run: vendor/bin/phpstan analyse --ansi --error-format symplify

-
name: 'PHPStan for config'
run: composer phpstan-config

-
name: 'Commented Code'
run: vendor/bin/easy-ci check-commented-code src rules tests rules-tests --line-limit 5 --ansi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipEmptyCheck
{
public function run($productOptions)
{
if (empty($productOptions)) {
return [];
}

echo $productOptions['list'][0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -156,7 +157,9 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
// skip integer in possibly string type as string can be accessed via int
$dimType = $this->getType($node->dim);
if ($dimType->isInteger()->yes() && $variableType->isString()->maybe()) {
return null;
// force set to false to avoid too early replaced
$isParamAccessedArrayDimFetch = false;
return NodeTraverser::STOP_TRAVERSAL;
}

$isParamAccessedArrayDimFetch = true;
Expand Down Expand Up @@ -212,11 +215,15 @@ private function shouldStop(Node $node, string $paramName): bool
return true;
}

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

private function isEchoedOrCasted(Node $node, string $paramName): bool
private function isEmptyOrEchoedOrCasted(Node $node, string $paramName): bool
{
if ($node instanceof Empty_ && $node->expr instanceof Variable && $this->isName($node->expr, $paramName)) {
return true;
}

if ($this->isEchoed($node, $paramName)) {
return true;
}
Expand Down

0 comments on commit 971571b

Please sign in to comment.