-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Add IntRangeType
to hold specific type details for int
builtin types
#59676
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
Changes from all commits
16d885d
49a43b4
f67554e
e76cbc1
79249d9
3f63fb9
007cb6d
acee37f
50c5576
c6e6ead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -963,15 +963,22 @@ public static function pseudoTypesProvider(): iterable | |||||||
yield ['traitString', Type::string()]; | ||||||||
yield ['interfaceString', Type::string()]; | ||||||||
yield ['literalString', Type::string()]; | ||||||||
yield ['positiveInt', Type::int()]; | ||||||||
yield ['negativeInt', Type::int()]; | ||||||||
yield ['nonEmptyArray', Type::array()]; | ||||||||
yield ['nonEmptyList', Type::list()]; | ||||||||
yield ['scalar', Type::union(Type::int(), Type::float(), Type::string(), Type::bool())]; | ||||||||
yield ['number', Type::union(Type::int(), Type::float())]; | ||||||||
yield ['numeric', Type::union(Type::int(), Type::float(), Type::string())]; | ||||||||
yield ['arrayKey', Type::union(Type::int(), Type::string())]; | ||||||||
yield ['double', Type::float()]; | ||||||||
|
||||||||
// BC layer for symfony/type-info < 7.3 | ||||||||
if (method_exists(Type::class, 'intRange')) { | ||||||||
yield ['positiveInt', Type::intRange(1)]; | ||||||||
yield ['negativeInt', Type::intRange(\PHP_INT_MIN, -1)]; | ||||||||
} else { | ||||||||
yield ['positiveInt', Type::int()]; | ||||||||
yield ['negativeInt', Type::int()]; | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It misses the |
||||||||
} | ||||||||
|
||||||||
public function testDummyNamespace() | ||||||||
|
@@ -1001,9 +1008,16 @@ public function testExtractorIntRangeType(string $property, ?Type $type) | |||||||
*/ | ||||||||
public static function intRangeTypeProvider(): iterable | ||||||||
{ | ||||||||
yield ['a', Type::int()]; | ||||||||
yield ['b', Type::nullable(Type::int())]; | ||||||||
yield ['c', Type::int()]; | ||||||||
// BC layer for symfony/type-info < 7.3 | ||||||||
if (method_exists(Type::class, 'intRange')) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
yield ['a', Type::intRange(0, 100)]; | ||||||||
yield ['b', Type::nullable(Type::intRange(\PHP_INT_MIN, 100))]; | ||||||||
yield ['c', Type::intRange(50, \PHP_INT_MAX)]; | ||||||||
} else { | ||||||||
yield ['a', Type::int()]; | ||||||||
yield ['b', Type::nullable(Type::int())]; | ||||||||
yield ['c', Type::int()]; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\Tests\Type; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\TypeInfo\Type; | ||
use Symfony\Component\TypeInfo\Type\IntRangeType; | ||
|
||
class IntRangeTypeTest extends TestCase | ||
{ | ||
public function testToString() | ||
{ | ||
$this->assertSame('int<0, max>', (string) new IntRangeType(from: 0)); | ||
$this->assertSame('int<min, 0>', (string) new IntRangeType(to: 0)); | ||
$this->assertSame('int<1, max>', (string) new IntRangeType(from: 1)); | ||
$this->assertSame('int<min, -1>', (string) new IntRangeType(to: -1)); | ||
$this->assertSame('int<-3, 5>', (string) new IntRangeType(from: -3, to: 5)); | ||
$this->assertSame('int<min, max>', (string) new IntRangeType()); | ||
$this->assertSame('int<min, 5>', (string) new IntRangeType(to: 5)); | ||
} | ||
|
||
public function testAccepts() | ||
{ | ||
$this->assertFalse((new IntRangeType(from: 0, to: 5))->accepts('string')); | ||
$this->assertFalse((new IntRangeType(from: 0, to: 5))->accepts([])); | ||
|
||
$this->assertFalse((new IntRangeType(from: -1, to: 5))->accepts(-3)); | ||
$this->assertTrue((new IntRangeType(from: -1, to: 5))->accepts(0)); | ||
$this->assertTrue((new IntRangeType(from: -1, to: 5))->accepts(2)); | ||
$this->assertFalse((new IntRangeType(from: -1, to: 5))->accepts(6)); | ||
|
||
$this->assertFalse(Type::union(Type::intRange(to: -1), Type::intRange(from: 1))->accepts(0)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the Symfony package. | ||||||
* | ||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
namespace Symfony\Component\TypeInfo\Type; | ||||||
|
||||||
use Symfony\Component\TypeInfo\TypeIdentifier; | ||||||
|
||||||
/** | ||||||
* Int range type. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* @author Martin Rademacher <mano@radebatz.net> | ||||||
* | ||||||
* @extends BuiltinType<TypeIdentifier::INT> | ||||||
*/ | ||||||
final class IntRangeType extends BuiltinType | ||||||
{ | ||||||
public function __construct( | ||||||
private int $from = \PHP_INT_MIN, | ||||||
private int $to = \PHP_INT_MAX, | ||||||
) { | ||||||
parent::__construct(TypeIdentifier::INT); | ||||||
} | ||||||
|
||||||
public function getFrom(): int | ||||||
{ | ||||||
return $this->from; | ||||||
} | ||||||
|
||||||
public function getTo(): int | ||||||
{ | ||||||
return $this->to; | ||||||
} | ||||||
|
||||||
public function accepts(mixed $value): bool | ||||||
{ | ||||||
return parent::accepts($value) && $this->from <= $value && $value <= $this->to; | ||||||
} | ||||||
|
||||||
public function __toString(): string | ||||||
{ | ||||||
$min = \PHP_INT_MIN === $this->from ? 'min' : $this->from; | ||||||
$max = \PHP_INT_MAX === $this->to ? 'max' : $this->to; | ||||||
|
||||||
return \sprintf('int<%s, %s>', $min, $max); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,12 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ | |
'bool', 'boolean' => Type::bool(), | ||
'true' => Type::true(), | ||
'false' => Type::false(), | ||
'int', 'integer', 'positive-int', 'negative-int', 'non-positive-int', 'non-negative-int', 'non-zero-int' => Type::int(), | ||
'int', 'integer' => Type::int(), | ||
'positive-int' => Type::intRange(from: 1), | ||
'negative-int' => Type::intRange(to: -1), | ||
'non-positive-int' => Type::intRange(to: 0), | ||
'non-negative-int' => Type::intRange(from: 0), | ||
'non-zero-int' => Type::union(Type::intRange(to: -1), Type::intRange(from: 1)), | ||
'float', 'double' => Type::float(), | ||
'string', | ||
'class-string', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was wondering about that. Seems wrong to leave things half finished. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant to move all the string related stuff (ie: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. Getting late here... |
||
|
@@ -184,9 +189,27 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ | |
if ($node instanceof GenericTypeNode) { | ||
$type = $this->getTypeFromNode($node->type, $typeContext); | ||
|
||
// handle integer ranges as simple integers | ||
// handle integer ranges | ||
if ($type->isIdentifiedBy(TypeIdentifier::INT)) { | ||
return $type; | ||
$getBoundaryFromNode = function (TypeNode $node) { | ||
if ($node instanceof IdentifierTypeNode) { | ||
return match ($node->name) { | ||
'min' => \PHP_INT_MIN, | ||
'max' => \PHP_INT_MAX, | ||
default => throw new \DomainException(\sprintf('Invalid int range value "%s".', $node->name)), | ||
}; | ||
} | ||
|
||
if ($node->constExpr instanceof ConstExprIntegerNode) { | ||
return (int) $node->constExpr->value; | ||
} | ||
|
||
throw new \DomainException(\sprintf('Invalid int range expression "%s".', \get_class($node->constExpr))); | ||
}; | ||
|
||
$boundaries = array_map(static fn (TypeNode $t): int => $getBoundaryFromNode($t), $node->genericTypes); | ||
|
||
return Type::intRange($boundaries[0], $boundaries[1]); | ||
} | ||
|
||
$variableTypes = array_map(fn (TypeNode $t): Type => $this->getTypeFromNode($t, $typeContext), $node->genericTypes); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.