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

Make int ranges generic #2642

Open
wants to merge 1 commit into
base: 1.11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericIntegerRangeType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Helper\GetTemplateTypeType;
Expand Down Expand Up @@ -669,23 +670,25 @@ static function (string $variance): TemplateTypeVariance {
} elseif ($mainTypeName === 'int') {
if (count($genericTypes) === 2) { // int<min, max>, int<1, 3>

if ($genericTypes[0] instanceof ConstantIntegerType) {
$min = $genericTypes[0]->getValue();
} elseif ($typeNode->genericTypes[0] instanceof IdentifierTypeNode && $typeNode->genericTypes[0]->name === 'min') {
if ($typeNode->genericTypes[0] instanceof IdentifierTypeNode && $typeNode->genericTypes[0]->name === 'min') {
$min = null;
} else {
} elseif (!$genericTypes[0]->isInteger()->yes()) {
return new ErrorType();
} else {
$min = $genericTypes[0];
}

if ($genericTypes[1] instanceof ConstantIntegerType) {
$max = $genericTypes[1]->getValue();
} elseif ($typeNode->genericTypes[1] instanceof IdentifierTypeNode && $typeNode->genericTypes[1]->name === 'max') {
if ($typeNode->genericTypes[1] instanceof IdentifierTypeNode && $typeNode->genericTypes[1]->name === 'max') {
$max = null;
} else {
} elseif (!$genericTypes[1]->isInteger()->yes()) {
return new ErrorType();
} else {
$max = $genericTypes[1];
}

return IntegerRangeType::fromInterval($min, $max);
$type = new GenericIntegerRangeType($min, $max);

return $type->isResolvable() ? $type->resolve() : $type;
}
} elseif ($mainTypeName === 'key-of') {
if (count($genericTypes) === 1) { // key-of<ValueType>
Expand Down
139 changes: 139 additions & 0 deletions src/Type/Generic/GenericIntegerRangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Generic;

use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\LateResolvableType;
use PHPStan\Type\Traits\LateResolvableTypeTrait;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function array_merge;
use function sprintf;

/** @api */
class GenericIntegerRangeType implements CompoundType, LateResolvableType
{

use LateResolvableTypeTrait;
use NonGeneralizableTypeTrait;

public function __construct(private ?Type $min, private ?Type $max)
{
}

public function getReferencedClasses(): array
{
return array_merge(
$this->min !== null ? $this->min->getReferencedClasses() : [],
$this->max !== null ? $this->max->getReferencedClasses() : [],
);
}

public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
return array_merge(
$this->min !== null ? $this->min->getReferencedTemplateTypes($positionVariance) : [],
$this->max !== null ? $this->max->getReferencedTemplateTypes($positionVariance) : [],
);
}

public function equals(Type $type): bool
{
if (!$type instanceof self) {
return false;
}

$compare = static fn (?Type $left, ?Type $right) => $left === null ? $right === null : $right !== null && $left->equals($right);

return $compare($this->min, $type->min) && $compare($this->max, $type->max);
}

public function describe(VerbosityLevel $level): string
{
return sprintf(
'int<%s, %s>',
$this->min === null ? 'min' : $this->min->describe($level),
$this->max === null ? 'max' : $this->max->describe($level),
);
}

public function isResolvable(): bool
{
return ($this->min === null || !TypeUtils::containsTemplateType($this->min))
&& ($this->max === null || !TypeUtils::containsTemplateType($this->max));
}

protected function getResult(): Type
{
if ($this->min !== null && !$this->min->isInteger()->yes()) {
return new ErrorType();

}

if ($this->max !== null && !$this->max->isInteger()->yes()) {
return new ErrorType();
}

$min = $this->min instanceof ConstantIntegerType ? $this->min->getValue() : null;
$max = $this->max instanceof ConstantIntegerType ? $this->max->getValue() : null;

return IntegerRangeType::fromInterval($min, $max);
}

/**
* @param callable(Type): Type $cb
*/
public function traverse(callable $cb): Type
{
$min = $this->min !== null ? $cb($this->min) : null;
$max = $this->max !== null ? $cb($this->max) : null;

if ($this->min === $min && $this->max === $max) {
return $this;
}

return new self($min, $max);
}

public function traverseSimultaneously(Type $right, callable $cb): Type
{
return $this;
}

public function toPhpDocNode(): TypeNode
{
if ($this->min === null) {
$min = new IdentifierTypeNode('min');
} else {
$min = $this->min->toPhpDocNode();
}

if ($this->max === null) {
$max = new IdentifierTypeNode('max');
} else {
$max = $this->max->toPhpDocNode();
}

return new GenericTypeNode(new IdentifierTypeNode('int'), [$min, $max]);
}

/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): Type
{
return new self(
$properties['min'],
$properties['max'],
);
}

}