Skip to content

Commit

Permalink
Merge pull request #7313 from orklah/empty-arrays
Browse files Browse the repository at this point in the history
replace `array<never, never>` as a way to detect empty arrays by a dedicated method
  • Loading branch information
orklah committed Jan 5, 2022
2 parents 17793d1 + 7f40489 commit bf4bf92
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -503,7 +503,7 @@ public function analyze(
&& !$inferred_return_type->isSingleIntLiteral()
&& !$inferred_return_type->isSingleStringLiteral()
&& !$inferred_return_type->isTrue()
&& $inferred_return_type->getId() !== 'array<never, never>'
&& !$inferred_return_type->isEmptyArray()
) {
$manipulator->makePure();
}
Expand Down
Expand Up @@ -1024,7 +1024,7 @@ public static function assignByRefParam(
$by_ref_out_type->parent_nodes += $existing_type->parent_nodes;
}

if ($existing_type->getId() !== 'array<never, never>') {
if (!$existing_type->isEmptyArray()) {
$context->vars_in_scope[$var_id] = $by_ref_out_type;

if (!($stmt_type = $statements_analyzer->node_data->getType($stmt))
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Expand Up @@ -554,7 +554,7 @@ private static function reconcileNonEmptyCountable(
if (!$array_atomic_type instanceof TNonEmptyArray
|| ($array_atomic_type->count < $min_count)
) {
if ($array_atomic_type->getId() === 'array<never, never>') {
if ($array_atomic_type->isEmptyArray()) {
$existing_var_type->removeType('array');
} else {
$non_empty_array = new TNonEmptyArray(
Expand Down
Expand Up @@ -486,7 +486,7 @@ private static function reconcileNonEmptyCountable(
$did_remove_type = true;

$existing_var_type->removeType('array');
} elseif ($array_atomic_type->getId() !== 'array<never, never>') {
} elseif (!($array_atomic_type instanceof TArray && $array_atomic_type->isEmptyArray())) {
$did_remove_type = true;

if (!$min_count) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic.php
Expand Up @@ -785,7 +785,7 @@ public function isFalsy(): bool
return true;
}

if ($this instanceof TArray && $this->getId() === 'array<never, never>') {
if ($this instanceof TArray && $this->isEmptyArray()) {
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Type/Union.php
Expand Up @@ -1598,4 +1598,12 @@ public function getSingleAtomic(): Atomic
{
return reset($this->types);
}

public function isEmptyArray(): bool
{
return count($this->types) === 1
&& isset($this->types['array'])
&& $this->types['array'] instanceof TArray
&& $this->types['array']->isEmptyArray();
}
}

0 comments on commit bf4bf92

Please sign in to comment.