Skip to content

Commit b50f182

Browse files
committed
Keep subclasses of string, int and float apart in unions
StringType::getStringRefinement() and IntegerType/FloatType::toExactNumberSet() now describe only the exact class. A subclass defined by an extension, as Larastan's model property of X, may hold fewer values than its parent, so ScalarUnion merged two of them by their cells or numbers and kept only the first: model property of User|model property of Account became model property of User. They now get null and are unioned by isSuperTypeOf(). ConstantIntegerType, ConstantFloatType and FloatRangeType, whose numbers are exact, override toExactNumberSet() to keep theirs. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-e25b-74f0-9147-bfb3ae88286a
1 parent ef9ff3f commit b50f182

17 files changed

Lines changed: 279 additions & 90 deletions

‎src/Type/Constant/ConstantFloatType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public function toNumberSet(): NumberSet
136136
return new NumberSet([], [[$this->value, $this->value]], false);
137137
}
138138

139+
public function toExactNumberSet(): ?NumberSet
140+
{
141+
return $this->toNumberSet();
142+
}
143+
139144
public function toInteger(): Type
140145
{
141146
return new ConstantIntegerType($this->toIntegerValue());

‎src/Type/Constant/ConstantIntegerType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function toNumberSet(): NumberSet
4141
return new NumberSet([[$this->value, $this->value]], [], false);
4242
}
4343

44+
public function toExactNumberSet(): ?NumberSet
45+
{
46+
return $this->toNumberSet();
47+
}
48+
4449
public function getValue(): int
4550
{
4651
return $this->value;

‎src/Type/FloatRangeType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ public function toNumberSet(): NumberSet
431431
return new NumberSet([], [[$this->min, $this->max]], false);
432432
}
433433

434+
public function toExactNumberSet(): ?NumberSet
435+
{
436+
return $this->toNumberSet();
437+
}
438+
434439
public function toInteger(): Type
435440
{
436441
// casting a float outside of the int range is undefined

‎src/Type/FloatType.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ public function toNumberSet(): NumberSet
148148
return new NumberSet([], [[-INF, INF]], true);
149149
}
150150

151+
/** A subclass may hold numbers the set does not describe, so only the subclasses that override this give theirs. */
151152
public function toExactNumberSet(): ?NumberSet
152153
{
153-
return $this->toNumberSet();
154+
return static::class === self::class ? $this->toNumberSet() : null;
154155
}
155156

156157
public function toBitwiseNotType(): Type

‎src/Type/IntegerType.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public function toNumberSet(): NumberSet
6666
return new NumberSet([[null, null]], [], false);
6767
}
6868

69+
/** A subclass may hold numbers the set does not describe, so only the subclasses that override this give theirs. */
6970
public function toExactNumberSet(): ?NumberSet
7071
{
71-
return $this->toNumberSet();
72+
return static::class === self::class ? $this->toNumberSet() : null;
7273
}
7374

7475
public function toBitwiseNotType(): Type

‎src/Type/StringType.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,10 @@ public function isLiteralString(): TrinaryLogic
285285
return TrinaryLogic::createMaybe();
286286
}
287287

288+
/** A subclass may hold strings the cells do not describe, so only string itself holds them all. */
288289
public function getStringRefinement(): ?int
289290
{
290-
return StringRefinement::ALL;
291+
return static::class === self::class ? StringRefinement::ALL : null;
291292
}
292293

293294
public function isLowercaseString(): TrinaryLogic

‎tests/PHPStan/Type/TypeCombinatorTest.php‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,143 @@ public static function dataUnion(): iterable
34383438
IntersectionType::class,
34393439
'lowercase-string&non-empty-string',
34403440
];
3441+
yield [
3442+
[
3443+
self::createPropertyNameType('User'),
3444+
self::createPropertyNameType('Account'),
3445+
],
3446+
UnionType::class,
3447+
'property name of Account|property name of User',
3448+
];
3449+
yield [
3450+
[
3451+
self::createPropertyNameType('User'),
3452+
new StringType(),
3453+
],
3454+
StringType::class,
3455+
'string',
3456+
];
3457+
yield [
3458+
[
3459+
self::createIdType('User'),
3460+
self::createIdType('Account'),
3461+
],
3462+
UnionType::class,
3463+
'id of Account|id of User',
3464+
];
3465+
yield [
3466+
[
3467+
self::createWeightType('User'),
3468+
self::createWeightType('Account'),
3469+
],
3470+
UnionType::class,
3471+
'weight of Account|weight of User',
3472+
];
3473+
}
3474+
3475+
/** A subclass of StringType holding the names of the properties of a class, as Larastan's model property of X. */
3476+
private static function createPropertyNameType(string $className): StringType
3477+
{
3478+
return new class (new ObjectType($className)) extends StringType {
3479+
3480+
public function __construct(private ObjectType $class)
3481+
{
3482+
parent::__construct();
3483+
}
3484+
3485+
public function describe(VerbosityLevel $level): string
3486+
{
3487+
return sprintf('property name of %s', $this->class->describe($level));
3488+
}
3489+
3490+
public function equals(Type $type): bool
3491+
{
3492+
return $type instanceof self && $this->class->equals($type->class);
3493+
}
3494+
3495+
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
3496+
{
3497+
if ($type instanceof self) {
3498+
return $this->class->isSuperTypeOf($type->class);
3499+
}
3500+
if ($type instanceof CompoundType) {
3501+
return $type->isSubTypeOf($this);
3502+
}
3503+
3504+
return IsSuperTypeOfResult::createNo();
3505+
}
3506+
3507+
};
3508+
}
3509+
3510+
/** The IntegerType counterpart of createPropertyNameType(). */
3511+
private static function createIdType(string $className): IntegerType
3512+
{
3513+
return new class (new ObjectType($className)) extends IntegerType {
3514+
3515+
public function __construct(private ObjectType $class)
3516+
{
3517+
parent::__construct();
3518+
}
3519+
3520+
public function describe(VerbosityLevel $level): string
3521+
{
3522+
return sprintf('id of %s', $this->class->describe($level));
3523+
}
3524+
3525+
public function equals(Type $type): bool
3526+
{
3527+
return $type instanceof self && $this->class->equals($type->class);
3528+
}
3529+
3530+
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
3531+
{
3532+
if ($type instanceof self) {
3533+
return $this->class->isSuperTypeOf($type->class);
3534+
}
3535+
if ($type instanceof CompoundType) {
3536+
return $type->isSubTypeOf($this);
3537+
}
3538+
3539+
return IsSuperTypeOfResult::createNo();
3540+
}
3541+
3542+
};
3543+
}
3544+
3545+
/** The FloatType counterpart of createPropertyNameType(). */
3546+
private static function createWeightType(string $className): FloatType
3547+
{
3548+
return new class (new ObjectType($className)) extends FloatType {
3549+
3550+
public function __construct(private ObjectType $class)
3551+
{
3552+
parent::__construct();
3553+
}
3554+
3555+
public function describe(VerbosityLevel $level): string
3556+
{
3557+
return sprintf('weight of %s', $this->class->describe($level));
3558+
}
3559+
3560+
public function equals(Type $type): bool
3561+
{
3562+
return $type instanceof self && $this->class->equals($type->class);
3563+
}
3564+
3565+
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
3566+
{
3567+
if ($type instanceof self) {
3568+
return $this->class->isSuperTypeOf($type->class);
3569+
}
3570+
if ($type instanceof CompoundType) {
3571+
return $type->isSubTypeOf($this);
3572+
}
3573+
3574+
return IsSuperTypeOfResult::createNo();
3575+
}
3576+
3577+
};
34413578
}
34423579

34433580
/**

‎turbo-ext/src/ConstantFloatType.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ PT_MINIT_REGISTRATION(pt_register_constant_float_type)
356356

357357
cls.method<&ConstantFloatType::toInteger>(sigs::toInteger);
358358
cls.method<&ConstantFloatType::toNumberSet>(sigs::toNumberSet);
359+
/* mirrors toExactNumberSet() */
360+
cls.method(sigs::toExactNumberSet, pt_type_exact_number_set_handler);
359361

360362
cls.method<&ConstantFloatType::toBitwiseNotType>(sigs::toBitwiseNotType);
361363

‎turbo-ext/src/ConstantIntegerType.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ PT_MINIT_REGISTRATION(pt_register_constant_integer_type)
292292

293293
cls.method<&ConstantIntegerType::toString>(sigs::toString);
294294
cls.method<&ConstantIntegerType::toNumberSet>(sigs::toNumberSet);
295+
/* mirrors toExactNumberSet() */
296+
cls.method(sigs::toExactNumberSet, pt_type_exact_number_set_handler);
295297

296298
cls.method<&ConstantIntegerType::toArrayKey>(sigs::toArrayKey);
297299
cls.op<PT_OP_TO_ARRAY_KEY, &ConstantIntegerType::toArrayKey>();

‎turbo-ext/src/FloatRangeType.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,8 @@ PT_MINIT_REGISTRATION(pt_register_float_range_type)
959959
});
960960

961961
cls.method<&FloatRangeType::toNumberSet>(sigs::toNumberSet);
962+
/* mirrors toExactNumberSet() */
963+
cls.method(sigs::toExactNumberSet, pt_type_exact_number_set_handler);
962964
cls.method<&FloatRangeType::describe, zp::Obj>(sigs::describe);
963965
cls.op<PT_OP_DESCRIBE, &FloatRangeType::describe>();
964966

0 commit comments

Comments
 (0)