Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace array<never, never> as a way to detect empty arrays by a dedicated method #7313

Merged
merged 3 commits into from Jan 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}