-
-
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 1 commit
16d885d
49a43b4
f67554e
e76cbc1
79249d9
3f63fb9
007cb6d
acee37f
50c5576
c6e6ead
8000 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
IntRangeType
and ExplicitStringType
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\BuiltinType; | ||
use Symfony\Component\TypeInfo\Type\IntRangeType; | ||
use Symfony\Component\TypeInfo\TypeIdentifier; | ||
|
||
class IntRangeTypeTest extends TestCase | ||
{ | ||
public function testToString() | ||
{ | ||
$this->assertSame('non-negative-int', (string) new IntRangeType(0, \PHP_INT_MAX)); | ||
$this->assertSame('non-positive-int', (string) new IntRangeType(\PHP_INT_MIN, 0)); | ||
$this->assertSame('positive-int', (string) new IntRangeType(1, \PHP_INT_MAX)); | ||
$this->assertSame('negative-int', (string) new IntRangeType(\PHP_INT_MIN, -1)); | ||
$this->assertSame('int<-3, 5>', (string) new IntRangeType(-3, 5)); | ||
$this->assertSame('int<min, max>', (string) new IntRangeType(\PHP_INT_MIN, \PHP_INT_MAX)); | ||
$this->assertSame('int<min, 5>', (string) new IntRangeType(\PHP_INT_MIN, 5)); | ||
} | ||
|
||
public function testIsIdentifiedBy() | ||
{ | ||
$this->assertFalse((new IntRangeType(0, 5))->isIdentifiedBy(TypeIdentifier::ARRAY)); | ||
$this->assertTrue((new BuiltinType(TypeIdentifier::INT))->isIdentifiedBy(TypeIdentifier::INT)); | ||
|
||
$this->assertFalse((new IntRangeType(0, 5))->isIdentifiedBy('array')); | ||
$this->assertTrue((new IntRangeType(0, 5))->isIdentifiedBy('int')); | ||
|
||
$this->assertTrue((new IntRangeType(0, 5))->isIdentifiedBy('string', '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. I'm not sure this test is needed (we are testing that 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. fair enough, removing |
||
|
||
public function testIsNullable() | ||
{ | ||
$this->assertFalse((new IntRangeType(0, 5))->isNullable()); | ||
} | ||
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. Same here |
||
|
||
public function testAccepts() | ||
{ | ||
$this->assertFalse((new IntRangeType(0, 5))->accepts('string')); | ||
$this->assertFalse((new IntRangeType(0, 5))->accepts([])); | ||
|
||
$this->assertFalse((new IntRangeType(-1, 5))->accepts(-3)); | ||
$this->assertTrue((new IntRangeType(-1, 5))->accepts(0)); | ||
$this->assertTrue((new IntRangeType(-1, 5))->accepts(2)); | ||
$this->assertFalse((new IntRangeType(-1, 5))->accepts(6)); | ||
|
||
$this->assertFalse((new IntRangeType(\PHP_INT_MIN, \PHP_INT_MAX, false))->accepts(0)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -98,26 +98,17 @@ public static function resolveDataProvider(): iterable | |||||||||
yield [Type::false(), 'false']; | ||||||||||
yield [Type::int(), 'int']; | ||||||||||
yield [Type::int(), 'integer']; | ||||||||||
yield [Type::int(), 'positive-int']; | ||||||||||
yield [Type::int(), 'negative-int']; | ||||||||||
yield [Type::int(), 'non-positive-int']; | ||||||||||
yield [Type::int(), 'non-negative-int']; | ||||||||||
yield [Type::int(), 'non-zero-int']; | ||||||||||
yield [Type::intRange(1, \PHP_INT_MAX), 'positive-int']; | ||||||||||
yield [Type::intRange(\PHP_INT_MIN, -1), 'negative-int']; | ||||||||||
yield [Type::intRange(\PHP_INT_MIN, 0), 'non-positive-int']; | ||||||||||
yield [Type::intRange(0, \PHP_INT_MAX), 'non-negative-int']; | ||||||||||
yield [Type::intRange(\PHP_INT_MIN, \PHP_INT_MAX, false), 'non-zero-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. These can be moved to |
||||||||||
yield [Type::float(), 'float']; | ||||||||||
yield [Type::float(), 'double']; | ||||||||||
yield [Type::string(), 'string']; | ||||||||||
yield [Type::string(), 'class-string']; | ||||||||||
yield [Type::string(), 'trait-string']; | ||||||||||
yield [Type::string(), 'interface-string']; | ||||||||||
yield [Type::string(), 'callable-string']; | ||||||||||
yield [Type::string(), 'n 8000 umeric-string']; | ||||||||||
yield [Type::string(), 'lowercase-string']; | ||||||||||
yield [Type::string(), 'non-empty-lowercase-string']; | ||||||||||
yield [Type::string(), 'non-empty-string']; | ||||||||||
yield [Type::string(), 'non-falsy-string']; | ||||||||||
yield [Type::string(), 'truthy-string']; | ||||||||||
yield [Type::string(), 'literal-string']; | ||||||||||
yield [Type::string(), 'html-escaped-string']; | ||||||||||
yield [Type::explicitString('class-string'), 'class-string']; | ||||||||||
yield [Type::explicitString('literal-string'), 'literal-string']; | ||||||||||
yield [Type::explicitString('html-escaped-string'), 'html-escaped-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. Let's have a section for these as well |
||||||||||
yield [Type::resource(), 'resource']; | ||||||||||
yield [Type::object(), 'object']; | ||||||||||
yield [Type::callable(), 'callable']; | ||||||||||
|
@@ -152,7 +143,8 @@ public static function resolveDataProvider(): iterable | |||||||||
// generic | ||||||||||
yield [Type::generic(Type::object(\DateTime::class), Type::string(), Type::bool()), \DateTime::class.'<string, bool>']; | ||||||||||
yield [Type::generic(Type::object(\DateTime::class), Type::generic(Type::object(\Stringable::class), Type::bool())), \sprintf('%s<%s<bool>>', \DateTime::class, \Stringable::class)]; | ||||||||||
yield [Type::int(), 'int<0, 100>']; | ||||||||||
yield [Type::intRange(0, 100), 'int<0, 100>']; | ||||||||||
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
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. This can be put under |
||||||||||
yield [Type::intRange(\PHP_INT_MIN, \PHP_INT_MAX), 'int<min, max>']; | ||||||||||
|
||||||||||
// union | ||||||||||
yield [Type::union(Type::int(), Type::string()), 'int|string']; | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
<?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; | ||||||
|
||||||
/** | ||||||
* Explicit string 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::STRING> | ||||||
*/ | ||||||
class ExplicitStringType extends BuiltinType | ||||||
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
|
||||||
{ | ||||||
public function __construct(private string $explicitType) | ||||||
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. Indeed, I do think we need an enum here:
Suggested change
And then maybe By doing that, we'll be able to implement the |
||||||
{ | ||||||
parent::__construct(TypeIdentifier::STRING); | ||||||
} | ||||||
|
||||||
public function getExplicitType(): string | ||||||
{ | ||||||
return $this->explicitType; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||
<?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> | ||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||
class IntRangeType extends BuiltinType | ||||||||||||||||||||||||||||
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
|
||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
public function __construct( | ||||||||||||||||||||||||||||
private int $from = \PHP_INT_MIN, | ||||||||||||||||||||||||||||
private int $to = \PHP_INT_MAX, | ||||||||||||||||||||||||||||
private bool $zeroIncluded = true, | ||||||||||||||||||||||||||||
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. This property is problematic to me, as zero shouldn't be a "special value". To handle the And the 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. Good solution. Bugged me too that it is ambiguous and creates extra overhead. 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 might break the symetrie of serialize/deserialize, but I guess that is a fair trade-off. |
||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||
parent::__construct(TypeIdentifier::INT); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public function getFrom(): int | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return $this->from; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public function getTo(): int | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return $this->to; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public function isZeroIncluded(): bool | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return $this->zeroIncluded; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public function accepts(mixed $value): bool | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return \is_int($value) | ||||||||||||||||||||||||||||
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
|
||||||||||||||||||||||||||||
&& $this->from <= $value && $value <= $this->to | ||||||||||||||||||||||||||||
&& (0 !== $value || $this->zeroIncluded); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
public function __toString(): string | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
$min = \PHP_INT_MIN === $this->from ? 'min' : $this->from; | ||||||||||||||||||||||||||||
$max = \PHP_INT_MAX === $this->to ? 'max' : $this->to; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
$template = 'int<%s, %s>'; | ||||||||||||||||||||||||||||
if (\is_string($min) && \is_string($max)) { | ||||||||||||||||||||||||||||
return $this->zeroIncluded ? \sprintf($template, $min, $max) : 'non-zero-int'; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (\in_array($min, [0, 1], true) && 'max' === $max) { | ||||||||||||||||||||||||||||
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'd have removed these conditions to only have a 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 might be more in line with what we do with |
||||||||||||||||||||||||||||
return 0 === $min ? 'non-negative-int' : 'positive-int'; | ||||||||||||||||||||||||||||
} elseif ('min' === $min && \in_array($max, [-1, 0])) { | ||||||||||||||||||||||||||||
return 0 === $max ? 'non-positive-int' : 'negative-int'; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return \sprintf($template, $min, $max); | ||||||||||||||||||||||||||||
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
I'd always kept the same canonical format (it is only possible if we drop the "excluding zero" stuff). |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,8 +15,10 @@ | |||||
use Symfony\Component\TypeInfo\Type\BuiltinType; | ||||||
use Symfony\Component\TypeInfo\Type\CollectionType; | ||||||
use Symfony\Component\TypeInfo\Type\EnumType; | ||||||
use Symfony\Component\TypeInfo\Type\ExplicitStringType; | ||||||
use Symfony\Component\TypeInfo\Type\GenericType; | ||||||
use Symfony\Component\TypeInfo\Type\IntersectionType; | ||||||
use Symfony\Component\TypeInfo\Type\IntRangeType; | ||||||
use Symfony\Component\TypeInfo\Type\NullableType; | ||||||
use Symfony\Component\TypeInfo\Type\ObjectType; | ||||||
use Symfony\Component\TypeInfo\Type\TemplateType; | ||||||
|
@@ -54,6 +56,11 @@ public static function int(): BuiltinType | |||||
return self::builtin(TypeIdentifier::INT); | ||||||
} | ||||||
|
||||||
public static function intRange(int $from, int $to, bool $zeroIncluded = true): IntRangeType | ||||||
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
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. Ok, but keeing 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. Because of #59676 (comment), I think we can remove it 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. Ah, good point. Sorry, I missed that one. |
||||||
{ | ||||||
return new IntRangeType($from, $to, $zeroIncluded); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return BuiltinType<TypeIdentifier::FLOAT> | ||||||
*/ | ||||||
|
@@ -70,6 +77,11 @@ public static function string(): BuiltinType | |||||
return self::builtin(TypeIdentifier::STRING); | ||||||
} | ||||||
|
||||||
public static function explicitString(string $explicitType): ExplicitStringType | ||||||
{ | ||||||
return new ExplicitStringType($explicitType); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return BuiltinType<TypeIdentifier::BOOL> | ||||||
*/ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -135,9 +135,14 @@ 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(1, \PHP_INT_MAX), | ||||||
'negative-int' => Type::intRange(\PHP_INT_MIN, -1), | ||||||
'non-positive-int' => Type::intRange(\PHP_INT_MIN, 0), | ||||||
'non-negative-int' => Type::intRange(0, \PHP_INT_MAX), | ||||||
'non-zero-int' => Type::intRange(\PHP_INT_MIN, \PHP_INT_MAX, false), | ||||||
'float', 'double' => Type::float(), | ||||||
'string', | ||||||
'string' => Type::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... |
||||||
'trait-string', | ||||||
'interface-string', | ||||||
|
@@ -149,7 +154,7 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ | |||||
'non-falsy-string', | ||||||
'truthy-string', | ||||||
'literal-string', | ||||||
'html-escaped-string' => Type::string(), | ||||||
'html-escaped-string' => Type::explicitString($node->name), | ||||||
'resource' => Type::resource(), | ||||||
'object' => Type::object(), | ||||||
'callable' => Type::callable(), | ||||||
|
@@ -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; | ||||||
$getValueFromNode = 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))); | ||||||
}; | ||||||
|
||||||
$range = array_map(fn (TypeNode $t): int => $getValueFromNode($t), $node->genericTypes); | ||||||
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
|
||||||
|
||||||
return Type::intRange(...$range); | ||||||
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
|
||||||
} | ||||||
|
||||||
$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.