Skip to content

Commit

Permalink
Improve internals of TIntRange (#6208)
Browse files Browse the repository at this point in the history
* Improve internals of TIntRange

* fix PositiveInt aliasing
  • Loading branch information
orklah authored Jul 31, 2021
1 parent c62adf9 commit 7e137f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/Psalm/Internal/Type/Comparator/IntegerRangeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public static function isContainedBy(
TIntRange $input_type_part,
TIntRange $container_type_part
) : bool {
$is_input_min = $input_type_part->min_bound === TIntRange::BOUND_MIN;
$is_input_max = $input_type_part->max_bound === TIntRange::BOUND_MAX;
$is_container_min = $container_type_part->min_bound === TIntRange::BOUND_MIN;
$is_container_max = $container_type_part->max_bound === TIntRange::BOUND_MAX;
$is_input_min = $input_type_part->min_bound === null;
$is_input_max = $input_type_part->max_bound === null;
$is_container_min = $container_type_part->min_bound === null;
$is_container_max = $container_type_part->max_bound === null;

$is_input_min_in_container = (
$is_container_min ||
Expand Down
10 changes: 4 additions & 6 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,6 @@ private static function getTypeFromGenericTree(
throw new TypeParseTreeException('int range must have 2 params');
}

$min_bound = Atomic\TIntRange::BOUND_MIN;
$max_bound = Atomic\TIntRange::BOUND_MAX;

$param0_union_types = array_values($generic_params[0]->getAtomicTypes());
$param1_union_types = array_values($generic_params[1]->getAtomicTypes());

Expand All @@ -805,19 +802,20 @@ private static function getTypeFromGenericTree(
throw new TypeParseTreeException("max bound for int range param can't be 'min'");
}


$min_bound = null;
$max_bound = null;
if ($param0_union_types[0] instanceof TLiteralInt) {
$min_bound = $param0_union_types[0]->value;
}
if ($param1_union_types[0] instanceof TLiteralInt) {
$max_bound = $param1_union_types[0]->value;
}

if ($min_bound === TIntRange::BOUND_MIN && $max_bound === TIntRange::BOUND_MAX) {
if ($min_bound === null && $max_bound === null) {
return new Atomic\TInt();
}

if ($min_bound === 0 && $max_bound === TIntRange::BOUND_MAX) {
if ($min_bound === 1 && $max_bound === null) {
return new Atomic\TPositiveInt();
}

Expand Down
20 changes: 8 additions & 12 deletions src/Psalm/Type/Atomic/TIntRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ class TIntRange extends TInt
const BOUND_MAX = 'max';

/**
* @var int|string
* @psalm-var int|'min'
* @var int|null
*/
public $min_bound;
/**
* @var int|string
* @var int|'max'
* @var int|null
*/
public $max_bound;

/**
* @param int|self::BOUND_MIN $min_bound
* @param int|self::BOUND_MAX $max_bound
*/
public function __construct($min_bound, $max_bound)
public function __construct(?int $min_bound, ?int $max_bound)
{
$this->min_bound = $min_bound;
$this->max_bound = $max_bound;
Expand All @@ -37,7 +31,7 @@ public function __toString(): string

public function getKey(bool $include_extra = true): string
{
return 'int<' . $this->min_bound . ', ' . $this->max_bound . '>';
return 'int<' . ($this->min_bound ?? 'min') . ', ' . ($this->max_bound ?? 'max') . '>';
}

public function canBeFullyExpressedInPhp(int $php_major_version, int $php_minor_version): bool
Expand Down Expand Up @@ -67,11 +61,13 @@ public function toNamespacedString(
?string $this_class,
bool $use_phpdoc_format
): string {
return $use_phpdoc_format ? 'int' : 'int<' . $this->min_bound . ', ' . $this->max_bound . '>';
return $use_phpdoc_format ?
'int' :
'int<' . ($this->min_bound ?? 'min') . ', ' . ($this->max_bound ?? 'max') . '>';
}

public function isPositive(): bool
{
return $this->min_bound !== self::BOUND_MIN && $this->min_bound > 0;
return $this->min_bound !== null && $this->min_bound > 0;
}
}

0 comments on commit 7e137f5

Please sign in to comment.