Skip to content

Commit 360c096

Browse files
committed
Add TemplateIntegerRangeType, TemplateFloatRangeType and TemplateConstantFloatType
Template types bounded by an integer range, a float range or a float constant were created as TemplateIntegerType or TemplateFloatType, which kept the bound but behaved as plain int or float, and TemplateTypeCheck reported these bounds as not supported. Each now has its own template class over the bound's type, so comparisons, arithmetic and abs() use the range or the constant. A template range is an unknown part of its range, not all of it: the range types no longer merge it in tryUnion()/tryIntersect() or remove it as a whole in tryRemove(), UnionType::isSuperTypeOf() no longer hands it back to isSubTypeOf(), and FiniteTypesRemoveRule no longer removes all the values of a template that has several of them. Amp-Thread-ID: https://ampcode.com/threads/T-01a23674-8e36-7696-8d80-30a0668f254a
1 parent 575f038 commit 360c096

30 files changed

Lines changed: 945 additions & 23 deletions

‎phpstan-baseline.neon‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,18 @@ parameters:
12371237
count: 3
12381238
path: src/Type/Generic/TemplateConstantArrayType.php
12391239

1240+
-
1241+
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
1242+
identifier: phpstanApi.instanceofType
1243+
count: 3
1244+
path: src/Type/Generic/TemplateConstantFloatType.php
1245+
1246+
-
1247+
rawMessage: 'Method PHPStan\Type\Generic\TemplateConstantFloatType::toPhpDocNode() should return PHPStan\PhpDocParser\Ast\Type\ConstTypeNode but returns PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode.'
1248+
identifier: return.type
1249+
count: 1
1250+
path: src/Type/Generic/TemplateConstantFloatType.php
1251+
12401252
-
12411253
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
12421254
identifier: phpstanApi.instanceofType
@@ -1255,6 +1267,12 @@ parameters:
12551267
count: 3
12561268
path: src/Type/Generic/TemplateConstantStringType.php
12571269

1270+
-
1271+
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
1272+
identifier: phpstanApi.instanceofType
1273+
count: 3
1274+
path: src/Type/Generic/TemplateFloatRangeType.php
1275+
12581276
-
12591277
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
12601278
identifier: phpstanApi.instanceofType
@@ -1267,6 +1285,12 @@ parameters:
12671285
count: 3
12681286
path: src/Type/Generic/TemplateGenericObjectType.php
12691287

1288+
-
1289+
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
1290+
identifier: phpstanApi.instanceofType
1291+
count: 3
1292+
path: src/Type/Generic/TemplateIntegerRangeType.php
1293+
12701294
-
12711295
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
12721296
identifier: phpstanApi.instanceofType

‎src/Rules/Generics/TemplateTypeCheck.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
use PHPStan\Type\ArrayType;
1818
use PHPStan\Type\BooleanType;
1919
use PHPStan\Type\Constant\ConstantArrayType;
20+
use PHPStan\Type\Constant\ConstantFloatType;
2021
use PHPStan\Type\Constant\ConstantIntegerType;
2122
use PHPStan\Type\Constant\ConstantStringType;
23+
use PHPStan\Type\FloatRangeType;
2224
use PHPStan\Type\FloatType;
2325
use PHPStan\Type\Generic\GenericObjectType;
2426
use PHPStan\Type\Generic\TemplateType;
2527
use PHPStan\Type\Generic\TemplateTypeScope;
28+
use PHPStan\Type\IntegerRangeType;
2629
use PHPStan\Type\IntegerType;
2730
use PHPStan\Type\IntersectionType;
2831
use PHPStan\Type\IterableType;
@@ -143,7 +146,10 @@ public function check(
143146
&& $boundTypeClass !== StringType::class
144147
&& $boundTypeClass !== ConstantIntegerType::class
145148
&& $boundTypeClass !== IntegerType::class
149+
&& $boundTypeClass !== IntegerRangeType::class
150+
&& $boundTypeClass !== ConstantFloatType::class
146151
&& $boundTypeClass !== FloatType::class
152+
&& $boundTypeClass !== FloatRangeType::class
147153
&& $boundTypeClass !== BooleanType::class
148154
&& $boundTypeClass !== ObjectWithoutClassType::class
149155
&& $boundTypeClass !== ObjectType::class

‎src/Type/Combinator/FiniteTypesRemoveRule.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type\Combinator;
44

5+
use PHPStan\Type\Generic\TemplateType;
56
use PHPStan\Type\NeverType;
67
use PHPStan\Type\Type;
78
use PHPStan\Type\UnionType;
@@ -27,6 +28,11 @@ public function tryRemove(Type $fromType, Type $typeToRemove): ?Type
2728
return null;
2829
}
2930

31+
// a template of several values stands for an unknown part of them, not for all of them
32+
if ($typeToRemove instanceof TemplateType && count($finiteTypesToRemove) > 1) {
33+
return null;
34+
}
35+
3036
$result = [];
3137
foreach ($fromFiniteTypes as $finiteType) {
3238
foreach ($finiteTypesToRemove as $finiteTypeToRemove) {

‎src/Type/FloatRangeType.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Constant\ConstantBooleanType;
1717
use PHPStan\Type\Constant\ConstantFloatType;
1818
use PHPStan\Type\Constant\ConstantIntegerType;
19+
use PHPStan\Type\Generic\TemplateFloatRangeType;
1920
use function array_map;
2021
use function get_class;
2122
use function is_finite;
@@ -49,7 +50,7 @@
4950
class FloatRangeType extends FloatType implements CompoundType
5051
{
5152

52-
private function __construct(private float $min, private float $max)
53+
protected function __construct(private float $min, private float $max)
5354
{
5455
parent::__construct();
5556
}
@@ -484,6 +485,11 @@ public function toAbsoluteNumber(): Type
484485
*/
485486
public function tryIntersect(Type $otherType): ?Type
486487
{
488+
// a template stands for an unknown part of its range, not for all of it
489+
if ($otherType instanceof TemplateFloatRangeType) {
490+
return null;
491+
}
492+
487493
$bounds = self::getOrdinalBounds($otherType);
488494
if ($bounds !== null) {
489495
[$otherMin, $otherMax] = $bounds;
@@ -515,6 +521,10 @@ public function tryRemove(Type $typeToRemove): ?Type
515521
return new NeverType();
516522
}
517523

524+
if ($typeToRemove instanceof TemplateFloatRangeType) {
525+
return null;
526+
}
527+
518528
$bounds = self::getOrdinalBounds($typeToRemove);
519529
if ($bounds === null) {
520530
// NAN is in no range
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Generic;
4+
5+
use PHPStan\Turbo\ShadowedByTurboExtension;
6+
use PHPStan\Type\Constant\ConstantFloatType;
7+
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
8+
use PHPStan\Type\Type;
9+
10+
/** @api */
11+
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateConstantFloatType.cpp')]
12+
final class TemplateConstantFloatType extends ConstantFloatType implements TemplateType
13+
{
14+
15+
/** @use TemplateTypeTrait<ConstantFloatType> */
16+
use TemplateTypeTrait;
17+
use UndecidedComparisonCompoundTypeTrait;
18+
19+
/**
20+
* @param non-empty-string $name
21+
*/
22+
public function __construct(
23+
TemplateTypeScope $scope,
24+
TemplateTypeStrategy $templateTypeStrategy,
25+
TemplateTypeVariance $templateTypeVariance,
26+
string $name,
27+
ConstantFloatType $bound,
28+
?Type $default,
29+
)
30+
{
31+
parent::__construct($bound->getValue());
32+
$this->scope = $scope;
33+
$this->strategy = $templateTypeStrategy;
34+
$this->variance = $templateTypeVariance;
35+
$this->name = $name;
36+
$this->bound = $bound;
37+
$this->default = $default;
38+
}
39+
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Generic;
4+
5+
use PHPStan\Turbo\ShadowedByTurboExtension;
6+
use PHPStan\Type\FloatRangeType;
7+
use PHPStan\Type\Type;
8+
9+
/** @api */
10+
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateFloatRangeType.cpp')]
11+
final class TemplateFloatRangeType extends FloatRangeType implements TemplateType
12+
{
13+
14+
/** @use TemplateTypeTrait<FloatRangeType> */
15+
use TemplateTypeTrait;
16+
17+
/**
18+
* @param non-empty-string $name
19+
*/
20+
public function __construct(
21+
TemplateTypeScope $scope,
22+
TemplateTypeStrategy $templateTypeStrategy,
23+
TemplateTypeVariance $templateTypeVariance,
24+
string $name,
25+
FloatRangeType $bound,
26+
?Type $default,
27+
)
28+
{
29+
parent::__construct($bound->getMin(), $bound->getMax());
30+
$this->scope = $scope;
31+
$this->strategy = $templateTypeStrategy;
32+
$this->variance = $templateTypeVariance;
33+
$this->name = $name;
34+
$this->bound = $bound;
35+
$this->default = $default;
36+
}
37+
38+
/** A T is an unknown part of its range: merged with another range it would no longer be a T. */
39+
public function tryUnion(Type $otherType): ?Type
40+
{
41+
return null;
42+
}
43+
44+
public function tryIntersect(Type $otherType): ?Type
45+
{
46+
return null;
47+
}
48+
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Generic;
4+
5+
use PHPStan\Turbo\ShadowedByTurboExtension;
6+
use PHPStan\Type\IntegerRangeType;
7+
use PHPStan\Type\Type;
8+
9+
/** @api */
10+
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateIntegerRangeType.cpp')]
11+
final class TemplateIntegerRangeType extends IntegerRangeType implements TemplateType
12+
{
13+
14+
/** @use TemplateTypeTrait<IntegerRangeType> */
15+
use TemplateTypeTrait;
16+
17+
/**
18+
* @param non-empty-string $name
19+
*/
20+
public function __construct(
21+
TemplateTypeScope $scope,
22+
TemplateTypeStrategy $templateTypeStrategy,
23+
TemplateTypeVariance $templateTypeVariance,
24+
string $name,
25+
IntegerRangeType $bound,
26+
?Type $default,
27+
)
28+
{
29+
parent::__construct($bound->getMin(), $bound->getMax());
30+
$this->scope = $scope;
31+
$this->strategy = $templateTypeStrategy;
32+
$this->variance = $templateTypeVariance;
33+
$this->name = $name;
34+
$this->bound = $bound;
35+
$this->default = $default;
36+
}
37+
38+
/** A T is an unknown part of its range: merged with another range it would no longer be a T. */
39+
public function tryUnion(Type $otherType): ?Type
40+
{
41+
return null;
42+
}
43+
44+
public function tryIntersect(Type $otherType): ?Type
45+
{
46+
return null;
47+
}
48+
49+
}

‎src/Type/Generic/TemplateTypeFactory.php‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use PHPStan\Type\BenevolentUnionType;
99
use PHPStan\Type\BooleanType;
1010
use PHPStan\Type\Constant\ConstantArrayType;
11+
use PHPStan\Type\Constant\ConstantFloatType;
1112
use PHPStan\Type\Constant\ConstantIntegerType;
1213
use PHPStan\Type\Constant\ConstantStringType;
14+
use PHPStan\Type\FloatRangeType;
1315
use PHPStan\Type\FloatType;
1416
use PHPStan\Type\IntegerRangeType;
1517
use PHPStan\Type\IntegerType;
@@ -77,18 +79,27 @@ public static function create(TemplateTypeScope $scope, string $name, ?Type $bou
7779
return new TemplateConstantStringType($scope, $strategy, $variance, $name, $bound, $default);
7880
}
7981

80-
if ($bound instanceof IntegerType && ($boundClass === IntegerType::class || $bound instanceof IntegerRangeType || $bound instanceof TemplateType)) {
82+
if ($bound instanceof IntegerRangeType && ($boundClass === IntegerRangeType::class || $bound instanceof TemplateType)) {
83+
return new TemplateIntegerRangeType($scope, $strategy, $variance, $name, $bound, $default);
84+
}
85+
86+
if ($bound instanceof IntegerType && ($boundClass === IntegerType::class || $bound instanceof TemplateType)) {
8187
return new TemplateIntegerType($scope, $strategy, $variance, $name, $bound, $default);
8288
}
8389

8490
if ($bound instanceof ConstantIntegerType && ($boundClass === ConstantIntegerType::class || $bound instanceof TemplateType)) {
8591
return new TemplateConstantIntegerType($scope, $strategy, $variance, $name, $bound, $default);
8692
}
8793

88-
// Catches plain FloatType and the float subtypes that narrowing a bound
89-
// produces, a FloatRangeType or a ConstantFloatType, neither of which has a
90-
// dedicated Template* class to fall into.
91-
if ($bound instanceof FloatType) {
94+
if ($bound instanceof FloatRangeType && ($boundClass === FloatRangeType::class || $bound instanceof TemplateType)) {
95+
return new TemplateFloatRangeType($scope, $strategy, $variance, $name, $bound, $default);
96+
}
97+
98+
if ($bound instanceof ConstantFloatType && ($boundClass === ConstantFloatType::class || $bound instanceof TemplateType)) {
99+
return new TemplateConstantFloatType($scope, $strategy, $variance, $name, $bound, $default);
100+
}
101+
102+
if ($bound instanceof FloatType && ($boundClass === FloatType::class || $bound instanceof TemplateType)) {
92103
return new TemplateFloatType($scope, $strategy, $variance, $name, $bound, $default);
93104
}
94105

‎src/Type/IntegerRangeType.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Constant\ConstantBooleanType;
1717
use PHPStan\Type\Constant\ConstantFloatType;
1818
use PHPStan\Type\Constant\ConstantIntegerType;
19+
use PHPStan\Type\Generic\TemplateIntegerRangeType;
1920
use PHPStan\Type\Traits\OperatorTypeTrait;
2021
use function array_filter;
2122
use function array_map;
@@ -42,7 +43,7 @@ class IntegerRangeType extends IntegerType implements CompoundType
4243

4344
use OperatorTypeTrait;
4445

45-
private function __construct(private ?int $min, private ?int $max)
46+
protected function __construct(private ?int $min, private ?int $max)
4647
{
4748
parent::__construct();
4849
assert($min === null || $max === null || $min <= $max);
@@ -640,6 +641,11 @@ public function toString(): Type
640641
*/
641642
public function tryUnion(Type $otherType): ?Type
642643
{
644+
// a template stands for an unknown part of its range, not for all of it
645+
if ($otherType instanceof TemplateIntegerRangeType) {
646+
return null;
647+
}
648+
643649
if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
644650
if ($otherType instanceof self) {
645651
$otherMin = $otherType->min;
@@ -673,6 +679,10 @@ public function tryUnion(Type $otherType): ?Type
673679
*/
674680
public function tryIntersect(Type $otherType): ?Type
675681
{
682+
if ($otherType instanceof TemplateIntegerRangeType) {
683+
return null;
684+
}
685+
676686
if ($otherType instanceof self || $otherType instanceof ConstantIntegerType) {
677687
if ($otherType instanceof self) {
678688
$otherMin = $otherType->min;
@@ -722,6 +732,10 @@ public function tryRemove(Type $typeToRemove): ?Type
722732
return new NeverType();
723733
}
724734

735+
if ($typeToRemove instanceof TemplateIntegerRangeType) {
736+
return null;
737+
}
738+
725739
if ($typeToRemove instanceof self || $typeToRemove instanceof ConstantIntegerType) {
726740
if ($typeToRemove instanceof self) {
727741
$removeMin = $typeToRemove->min;

‎src/Type/IntegerType.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\Constant\ConstantArrayType;
1313
use PHPStan\Type\Constant\ConstantBooleanType;
1414
use PHPStan\Type\Constant\ConstantIntegerType;
15+
use PHPStan\Type\Generic\TemplateIntegerRangeType;
1516
use PHPStan\Type\Traits\NonArrayTypeTrait;
1617
use PHPStan\Type\Traits\NonCallableTypeTrait;
1718
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
@@ -188,6 +189,11 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
188189

189190
public function tryRemove(Type $typeToRemove): ?Type
190191
{
192+
// a template stands for an unknown part of its range, not for all of it
193+
if ($typeToRemove instanceof TemplateIntegerRangeType) {
194+
return null;
195+
}
196+
191197
if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
192198
if ($typeToRemove instanceof IntegerRangeType) {
193199
$removeValueMin = $typeToRemove->getMin();

0 commit comments

Comments
 (0)