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,20 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\Fixture;

class SkipPossiblyUndefinedVariable
{
public function run(bool $cond): array
{
if ($cond) {
$map = ['a' => 1];
}

// empty() tolerates an undefined $map; `$map === []` does not.
if (empty($map)) {
return [];
}

return $map;
}
}
12 changes: 11 additions & 1 deletion src/NodeAnalyzer/ExprAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ExprAnalyzer
final readonly class ExprAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}

public function isBoolExpr(Expr $expr): bool
{
return $expr instanceof BooleanNot
Expand Down Expand Up @@ -158,6 +164,10 @@ public function isNonTypedFromParam(Expr $expr): bool
return true;
}

if (! $scope->hasVariableType((string) $this->nodeNameResolver->getName($expr))->yes()) {
return true;
}

if ($nativeType instanceof UnionType) {
return ! $nativeType->equals($type);
}
Expand Down
Loading