Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 27, 2024
1 parent cc805de commit c1fe1e2
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -699,16 +699,14 @@ private static function updateArrayAssignmentChildType(
if ($from_countable_object_like) {
$atomic_root_types = $new_child_type->getAtomicTypes();

if (isset($atomic_root_types['array'])) {
$atomic_root_type_array = $atomic_root_types['array'];


$changed = false;
foreach ($atomic_root_types as $k => $atomic_root_type_array) {
if ($atomic_root_type_array instanceof TNonEmptyArray
&& $atomic_root_type_array->count !== null
) {
$atomic_root_types['array'] =
$atomic_root_types[$k] =
$atomic_root_type_array->setCount($atomic_root_type_array->count+1);
$new_child_type = new Union($atomic_root_types);
$changed = true;
} elseif ($atomic_root_type_array instanceof TKeyedArray
&& $atomic_root_type_array->is_list) {
$properties = $atomic_root_type_array->properties;
Expand All @@ -725,12 +723,14 @@ private static function updateArrayAssignmentChildType(
$properties []= $atomic_root_type_array->fallback_params[1];
}

$atomic_root_types['array'] =
$atomic_root_types[$k] =
$atomic_root_type_array->setProperties($properties);

$new_child_type = new Union($atomic_root_types);
$changed = true;
}
}
if ($changed) {
$new_child_type = new Union($atomic_root_types);
}
}

return $new_child_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1403,17 +1403,7 @@ private static function handleByRefFunctionArg(
if (($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
&& $arg_value_type->hasArray()
) {
/**
* @var TArray|TKeyedArray
*/
$array_type = $arg_value_type->getArray();

if ($array_type instanceof TKeyedArray) {
$array_type = $array_type->getGenericArrayType();
}

$by_ref_type = new Union([$array_type]);

$by_ref_type = new Union($arg_value_type->getArrays());
AssignmentAnalyzer::assignByRefParam(
$statements_analyzer,
$arg->value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function getFunctionParams(FunctionParamsProviderEvent $event): ?a
if (!$first_arg_type || $first_arg_type->isMixed()) {
$first_arg_array = $fallback;
} else {
$first_arg_array = $first_arg_type->hasType('array')
$first_arg_array = $first_arg_type->hasArray()
&& ($array_atomic_type = $first_arg_type->getArray())
&& ($array_atomic_type instanceof TArray
|| $array_atomic_type instanceof TKeyedArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
if (!$first_arg_type || $first_arg_type->isMixed()) {
$first_arg_array = $fallback;
} else {
$first_arg_array = $first_arg_type->hasType('array')
$first_arg_array = $first_arg_type->hasArray()
&& ($array_atomic_type = $first_arg_type->getArray())
&& ($array_atomic_type instanceof TArray
|| $array_atomic_type instanceof TKeyedArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

$first_arg_array = $first_arg
&& ($first_arg_type = $statements_source->node_data->getType($first_arg))
&& $first_arg_type->hasType('array')
&& $first_arg_type->hasArray()
&& !$first_arg_type->hasMixed()
&& ($array_atomic_type = $first_arg_type->getArray())
&& ($array_atomic_type instanceof TArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

$first_arg_array = $first_arg
&& ($first_arg_type = $statements_source->node_data->getType($first_arg))
&& $first_arg_type->hasType('array')
&& $first_arg_type->hasArray()
&& ($array_atomic_type = $first_arg_type->getArray())
&& ($array_atomic_type instanceof TArray
|| $array_atomic_type instanceof TKeyedArray)
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static function reconcile(
&& $assertion_type->type_params[1]->isMixed())
|| $assertion instanceof IsNotCountable
) {
$existing_var_type->removeType('array');
$existing_var_type->removeArrays();
}

if ($assertion instanceof IsNotType && $assertion_type instanceof TClassString) {
Expand Down Expand Up @@ -216,7 +216,7 @@ public static function reconcile(
array_merge(array_values($existing_var_type->getAtomicTypes()), [$assertion->getAtomicType()]),
$codebase,
);
$existing_var_type->removeType('array');
$existing_var_type->removeArrays();
if ($combined_type->isSingle()) {
$existing_var_type->addType($combined_type->getSingleAtomic());
}
Expand Down
9 changes: 3 additions & 6 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2775,20 +2775,17 @@ private static function reconcileTruthyOrNonEmpty(
$types []= new TTrue;
}

if (isset($types['array'])) {
$array_atomic_type = $types['array'];


foreach ($types as $k => $array_atomic_type) {
if ($array_atomic_type instanceof TArray
&& !$array_atomic_type instanceof TNonEmptyArray
) {
unset($types['array']);
unset($types[$k]);
$types [] = new TNonEmptyArray($array_atomic_type->type_params);
} elseif ($array_atomic_type instanceof TKeyedArray
&& $array_atomic_type->is_list
&& $array_atomic_type->properties[0]->possibly_undefined
) {
unset($types['array']);
unset($types[$k]);
$properties = $array_atomic_type->properties;
$properties[0] = $properties[0]->setPossiblyUndefined(false);
$types [] = $array_atomic_type->setProperties($properties);
Expand Down
18 changes: 18 additions & 0 deletions src/Psalm/Type/MutableUnion.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
Expand Down Expand Up @@ -264,6 +265,23 @@ public function addType(Atomic $type): self
return $this;
}

/**
* @psalm-external-mutation-free
*/
public function removeArrays(): bool
{
$did = false;
foreach ($this->types as $k => $t) {
if ($t instanceof TKeyedArray || $t instanceof TArray) {
unset($this->types[$k]);
$did = true;
}
}
if ($did) {
$this->bustCache();
}
return $did;
}
/**
* @psalm-external-mutation-free
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Psalm/Type/UnionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,17 @@ public function hasArray(): bool
}

/**
* @return Generator<int, (TArray|TKeyedArray)>
* @return list<(TArray|TKeyedArray)>
*/
public function getArrays(): Generator
public function getArrays(): array
{
$result = [];
foreach ($this->types as $t) {
if ($t instanceof TKeyedArray || $t instanceof TArray) {
yield $t;
$result []= $t;
}
}
return $result;
}

/**
Expand Down

0 comments on commit c1fe1e2

Please sign in to comment.