Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 27, 2024
1 parent 5597583 commit c728ffa
Show file tree
Hide file tree
Showing 19 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public static function checkIteratorType(
}

// if it's an empty array, we cannot iterate over it
if ($iterator_atomic_type instanceof TArray && $iterator_atomic_type->isEmptyArray()) {
if ($iterator_atomic_type instanceof TArray && $iterator_atomic_type->isEmpty()) {
$always_non_empty_array = false;
$has_valid_iterator = true;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Storage\MethodStorage;
use Psalm\Type;
use Psalm\Type\Atomic\ArrayInterface;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TCallableKeyedArray;
Expand Down Expand Up @@ -1236,7 +1237,7 @@ private static function evaluateArbitraryParam(

$t = $context->vars_in_scope[$var_id]->getBuilder();
foreach ($t->getAtomicTypes() as $k => $type) {
if ($type instanceof TArray && $type->isEmptyArray()) {
if ($type instanceof ArrayInterface && $type->isEmpty()) {
$t->removeType($k);
$t->addType(
new TArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public static function handleAddition(
array_unshift($properties, $arg_value_type);

$by_ref_type = new Union([$objectlike_list->setProperties($properties)]);
} elseif ($array_type instanceof TArray && $array_type->isEmptyArray()) {
} elseif ($array_type instanceof TArray && $array_type->isEmpty()) {
$by_ref_type = new Union([new TKeyedArray([
$arg_value_type,
], null, null, true)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Psalm\Plugin\EventHandler\Event\AfterFunctionCallAnalysisEvent;
use Psalm\Storage\FunctionLikeStorage;
use Psalm\Type;
use Psalm\Type\Atomic\ArrayInterface;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TCallableKeyedArray;
Expand Down Expand Up @@ -383,8 +384,8 @@ private static function getReturnTypeFromCallMapWithArgs(
return Type::getIntRange($min, $max);
}

if ($atomic_types['array'] instanceof TArray
&& $atomic_types['array']->isEmptyArray()
if ($atomic_types['array'] instanceof ArrayInterface
&& $atomic_types['array']->isEmpty()
) {
return Type::getInt(false, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ private static function handleArrayAccessOnArray(

if ($in_assignment) {
if ($type instanceof TArray) {
$from_empty_array = $type->isEmptyArray();
$from_empty_array = $type->isEmpty();

if (count($key_values) === 1) {
$single_atomic = $key_values[0];
Expand Down Expand Up @@ -1249,15 +1249,15 @@ private static function handleArrayAccessOnTArray(
): void {
// if we're assigning to an empty array with a key offset, refashion that array
if ($in_assignment) {
if ($type->isEmptyArray()) {
if ($type->isEmpty()) {
$type = $type->setTypeParams([
$offset_type->isMixed()
? Type::getArrayKey()
: $offset_type->freeze(),
$type->type_params[1],
]);
}
} elseif (!$type->isEmptyArray()) {
} elseif (!$type->isEmpty()) {
$expected_offset_type = $type->type_params[0]->hasMixed()
? new Union([new TArrayKey])
: $type->type_params[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ private static function handleUnpackedArray(
);
}
} elseif ($unpacked_atomic_type instanceof TArray) {
if ($unpacked_atomic_type->isEmptyArray()) {
if ($unpacked_atomic_type->isEmpty()) {
continue;
}
$array_creation_info->can_create_objectlike = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Codebase/ConstantTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static function resolve(
$visited_constant_ids + [$c_id => true],
);

if ($spread_array instanceof TArray && $spread_array->isEmptyArray()) {
if ($spread_array instanceof TArray && $spread_array->isEmpty()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
continue;
}
if (!$row_shape instanceof TKeyedArray) {
if ($row_shape instanceof TArray && $row_shape->isEmptyArray()) {
if ($row_shape instanceof TArray && $row_shape->isEmpty()) {
continue;
}
$ok = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
) {
$results = [];
foreach ($first_arg_type->getArrays() as $array) {
if ($array instanceof TArray && $array->isEmptyArray()) {
if ($array instanceof TArray && $array->isEmpty()) {
$results []= $array;
continue;
} elseif ($array instanceof TKeyedArray && !$array->fallback_params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
} elseif ($call_arg_type
&& $call_arg_type->isSingle()
&& ($call_arg_atomic = $call_arg_type->getSingleAtomic()) instanceof TArray
&& $call_arg_atomic->isEmptyArray()
&& $call_arg_atomic->isEmpty()
) {
$array_arg_types []= [];
$orig_types []= $call_arg_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}

if ($unpacked_type_part instanceof TArray) {
if ($unpacked_type_part->isEmptyArray()) {
if ($unpacked_type_part->isEmpty()) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ private static function reconcileNonEmptyCountable(
|| ($assertion instanceof HasAtLeastCount
&& $array_atomic_type->min_count < $assertion->count)
) {
if ($array_atomic_type->isEmptyArray()) {
if ($array_atomic_type->isEmpty()) {
$existing_var_type->removeType($k);
} else {
$non_empty_array = new TNonEmptyArray(
Expand Down Expand Up @@ -2399,7 +2399,7 @@ private static function reconcileList(
}
}

if ($type->isEmptyArray()) {
if ($type->isEmpty()) {
//we allow an empty array to pass as a list. We keep the type as empty array though (more precise)
$array_types[] = $type;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Psalm\Storage\Assertion\NotInArray;
use Psalm\Storage\Assertion\NotNonEmptyCountable;
use Psalm\Type;
use Psalm\Type\Atomic\ArrayInterface;
use Psalm\Type\Atomic\Scalar;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TArrayKey;
Expand Down Expand Up @@ -630,7 +631,7 @@ private static function reconcileNotNonEmptyCountable(
));
}
}
} elseif (!$array_atomic_type instanceof TArray || !$array_atomic_type->isEmptyArray()) {
} elseif (!$array_atomic_type instanceof ArrayInterface || !$array_atomic_type->isEmpty()) {
$redundant = false;

if (!$count) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ private static function scrapeTypeProperties(
$combination->array_always_filled = false;
}

if (!$type->isEmptyArray()) {
if (!$type->isEmpty()) {
$combination->all_arrays_lists = false;
$combination->all_arrays_class_string_maps = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Psalm\Internal\TypeVisitor;

use Psalm\Type\Atomic\ArrayInterface;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TLiteralFloat;
Expand Down Expand Up @@ -32,7 +33,7 @@ protected function enterNode(TypeNode $type): ?int
return self::STOP_TRAVERSAL;
}

if ($type instanceof TArray && $type->isEmptyArray()) {
if ($type instanceof ArrayInterface && $type->isEmpty()) {
$this->contains_literal = true;
return self::STOP_TRAVERSAL;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Type/Atomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psalm\Internal\TypeVisitor\ClasslikeReplacer;
use Psalm\Storage\UnserializeMemoryUsageSuppressionTrait;
use Psalm\Type;
use Psalm\Type\Atomic\ArrayInterface;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TBool;
Expand Down Expand Up @@ -908,7 +909,7 @@ public function isFalsy(): bool
return true;
}

if ($this instanceof TArray && $this->isEmptyArray()) {
if ($this instanceof ArrayInterface && $this->isEmpty()) {
return true;
}

Expand Down
9 changes: 2 additions & 7 deletions src/Psalm/Type/Atomic/TArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public function getMaxCount(): ?int
}
public function getCount(): ?int
{
return $this->isEmptyArray() ? 0 : null;
return $this->isEmpty() ? 0 : null;
}
public function isEmpty(): bool
{
return $this->isEmptyArray();
return $this->type_params[1]->isNever();
}
public function isNonEmpty(): bool
{
Expand Down Expand Up @@ -134,11 +134,6 @@ public function getAssertionString(): string
return $this->getId();
}

public function isEmptyArray(): bool
{
return $this->type_params[1]->isNever();
}

/**
* @return static
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ private static function adjustTKeyedArrayType(
foreach ($existing_types[$base_key]->getAtomicTypes() as $base_atomic_type) {
if ($base_atomic_type instanceof TKeyedArray
|| ($base_atomic_type instanceof TArray
&& !$base_atomic_type->isEmptyArray())
&& !$base_atomic_type->isEmpty())
|| $base_atomic_type instanceof TClassStringMap
) {
$new_base_type = $existing_types[$base_key];
Expand Down
4 changes: 1 addition & 3 deletions src/Psalm/Type/UnionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ public function areArraysAllNonEmpty(): bool

public function areArraysAllEmpty(): bool
{
$result = true;
foreach ($this->types as $t) {
if ($t instanceof ArrayInterface
&& !$t->isEmpty()
Expand Down Expand Up @@ -1670,8 +1669,7 @@ public function getSingleAtomic(): Atomic
*/
public function isEmptyArray(): bool
{
return count($this->types) === 1
&& isset($this->types['array<never, never>']);
return $this->isArray() && $this->areArraysAllEmpty();
}

/**
Expand Down

0 comments on commit c728ffa

Please sign in to comment.