Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 27, 2024
1 parent b9741cb commit 563a453
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use function count;
use function explode;
use function is_numeric;
use function max;
use function str_contains;
use function strtolower;
use function substr;
Expand Down Expand Up @@ -337,36 +338,32 @@ public static function handleSplice(
return false;
}

$array_type = null;
$array_size = null;
$array_types = [];
$max_array_size = null;

if (($array_arg_type = $statements_analyzer->node_data->getType($array_arg))
&& $array_arg_type->hasArray()
) {
/**
* @var TArray|TKeyedArray
*/
$array_type = $array_arg_type->getArray();
if ($generic_array_type = ArrayType::infer($array_type)) {
$array_size = $generic_array_type->count;
}
foreach ($array_arg_type->getArrays() as $array_type) {
$max_array_size = max($max_array_size, $array_type->getMaxCount());

if ($array_type instanceof TKeyedArray) {
if ($array_type->is_list && isset($args[3])) {
$array_type = Type::getNonEmptyListAtomic($array_type->getGenericValueType());
} else {
$array_type = $array_type->getGenericArrayType();
if ($array_type instanceof TKeyedArray) {
if ($array_type->is_list && isset($args[3])) {
$array_type = Type::getNonEmptyListAtomic($array_type->getGenericValueType());
} else {
$array_type = $array_type->getGenericArrayType();
}
}
}

if ($array_type instanceof TArray
&& $array_type->type_params[0]->hasInt()
&& !$array_type->type_params[0]->hasString()
) {
if ($array_type instanceof TNonEmptyArray && isset($args[3])) {
$array_type = Type::getNonEmptyListAtomic($array_type->type_params[1]);
} else {
$array_type = Type::getListAtomic($array_type->type_params[1]);
if ($array_type instanceof TArray
&& $array_type->type_params[0]->hasInt()
&& !$array_type->type_params[0]->hasString()
) {
if ($array_type instanceof TNonEmptyArray && isset($args[3])) {
$array_type = Type::getNonEmptyListAtomic($array_type->type_params[1]);
} else {
$array_type = Type::getListAtomic($array_type->type_params[1]);
}
}
}
}
Expand Down Expand Up @@ -401,12 +398,12 @@ public static function handleSplice(
$context,
false,
);
} elseif ($array_type) {
} elseif ($array_types) {
AssignmentAnalyzer::assignByRefParam(
$statements_analyzer,
$array_arg,
new Union([$array_type]),
new Union([$array_type]),
new Union($array_types),
new Union($array_types),
$context,
false,
);
Expand Down Expand Up @@ -436,7 +433,7 @@ public static function handleSplice(
}

$cover_whole_arr = false;
if ($offset_arg_is_zero && is_numeric($array_size)) {
if ($offset_arg_is_zero && $max_array_size !== null) {
if (($length_arg_type = $statements_analyzer->node_data->getType($length_arg))
&& $length_arg_type->hasLiteralValue()
) {
Expand All @@ -459,7 +456,7 @@ public static function handleSplice(
}
}
}
$cover_whole_arr = isset($length_min) && $length_min>= $array_size;
$cover_whole_arr = isset($length_min) && $length_min>= $max_array_size;
} elseif ($length_arg_type&& $length_arg_type->isNull()) {
$cover_whole_arr = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Psalm\Internal\Provider\ReturnTypeProvider;

use Psalm\Internal\Type\ArrayType;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
Expand Down Expand Up @@ -48,11 +47,9 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

if (count($call_args) === 1
&& ($array_arg_type = $nodeTypeProvider->getType($call_args[0]->value))
&& $array_arg_type->isSingle()
&& $array_arg_type->hasArray()
&& ($array_type = ArrayType::infer($array_arg_type->getSingleAtomic()))
&& $array_arg_type->isArray()
) {
return $array_type->value;
return $array_arg_type->getArrayValueTypes();
}

$all_int = true;
Expand Down Expand Up @@ -135,9 +132,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
if ($arg_type = $nodeTypeProvider->getType($arg->value)) {
if ($arg->unpack) {
if ($arg_type->isSingle() && $arg_type->isArray()) {
$array_type = ArrayType::infer($arg_type->getSingleAtomic());
assert($array_type !== null);
$additional_type = $array_type->value;
$additional_type = Type::combineUnionTypeArray($arg_type->getArrayValueTypes());
} else {
$additional_type = Type::getMixed();
}
Expand Down
68 changes: 0 additions & 68 deletions src/Psalm/Internal/Type/ArrayType.php

This file was deleted.

23 changes: 22 additions & 1 deletion src/Psalm/Type/Atomic/TArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @psalm-immutable
*/
class TArray extends Atomic
class TArray extends Atomic implements ArrayInterface
{
use UnserializeMemoryUsageSuppressionTrait;
/**
Expand Down Expand Up @@ -53,6 +53,27 @@ public function getKey(bool $include_extra = true): string
return $this->getId(true);
}

public function getMinCount(): int
{
return 0;
}
public function getMaxCount(): ?int
{
return null;
}
public function getCount(): ?int
{
return null;
}
public function getGenericKeyType(): Union
{
return $this->type_params[0];
}
public function getGenericValueType(): Union
{
return $this->type_params[1];
}

/**
* @param array<lowercase-string, string> $aliased_classes
*/
Expand Down
11 changes: 10 additions & 1 deletion src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @psalm-immutable
*/
class TKeyedArray extends Atomic
class TKeyedArray extends Atomic implements ArrayInterface
{
use UnserializeMemoryUsageSuppressionTrait;
/**
Expand Down Expand Up @@ -427,6 +427,15 @@ public function isNonEmpty(): bool
return false;
}

public function getCount(): ?int
{
$min = $this->getMinCount();
if ($min === $this->getMaxCount()) {
return $min;
}
return null;
}

/**
* @return int<0, max>
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Psalm/Type/Atomic/TNonEmptyArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
*/
final class TNonEmptyArray extends TArray
{
public function getMinCount(): int
{
return $this->count ?? $this->min_count ?? 1;
}
public function getMaxCount(): ?int
{
return $this->count;
}
public function getCount(): ?int
{
return $this->count;
}

/**
* @param array{Union, Union} $type_params
* @param positive-int|null $count
Expand Down

0 comments on commit 563a453

Please sign in to comment.