Skip to content

Commit

Permalink
merge ArrayAnalyzer to ExprAnalyzer due to cyclic dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 5, 2023
1 parent b6802ec commit 9893185
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 70 deletions.
2 changes: 0 additions & 2 deletions rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
use Rector\Core\NodeManipulator\ArrayAnalyzer;

final class ComplexNewAnalyzer
{
public function __construct(
private readonly ArrayAnalyzer $arrayManipulator,
private readonly ExprAnalyzer $exprAnalyzer
) {
}
Expand Down
52 changes: 45 additions & 7 deletions src/NodeAnalyzer/ExprAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use PHPStan\Type\UnionType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeManipulator\ArrayAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ExprAnalyzer
{
public function __construct(
private readonly ArrayAnalyzer $arrayManipulator
) {
}

public function isNonTypedFromParam(Expr $expr): bool
{
if (! $expr instanceof Variable) {
Expand Down Expand Up @@ -64,7 +61,26 @@ public function isDynamicExpr(Expr $expr): bool
return ! $this->isAllowedConstFetchOrClassConstFetch($expr);
}

return $this->arrayManipulator->isDynamicArray($expr);
return $this->isDynamicArray($expr);
}

private function isDynamicArray(Array_ $array): bool
{
foreach ($array->items as $item) {
if (! $item instanceof ArrayItem) {
continue;
}

if (! $this->isAllowedArrayKey($item->key)) {
return true;
}

if (! $this->isAllowedArrayValue($item->value)) {
return true;
}
}

return false;
}

private function isAllowedConstFetchOrClassConstFetch(Expr $expr): bool
Expand All @@ -88,4 +104,26 @@ private function isAllowedConstFetchOrClassConstFetch(Expr $expr): bool

return false;
}

private function isAllowedArrayKey(?Expr $expr): bool
{
if (! $expr instanceof Expr) {
return true;
}

if ($expr instanceof String_) {
return true;
}

return $expr instanceof LNumber;
}

private function isAllowedArrayValue(Expr $expr): bool
{
if ($expr instanceof Array_) {
return ! $this->isDynamicArray($expr);
}

return ! $this->isDynamicExpr($expr);
}
}
61 changes: 0 additions & 61 deletions src/NodeManipulator/ArrayAnalyzer.php

This file was deleted.

0 comments on commit 9893185

Please sign in to comment.