From edfdfab0bc1a832f1005f870f529ce246e897d4d Mon Sep 17 00:00:00 2001 From: phpstan-bot <79867460+phpstan-bot@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:52:49 +0000 Subject: [PATCH 1/2] Compare branches of two conditional types in `isSuperTypeOf()` only when they share the same condition, and include negation in `equals()` - ConditionalType::isSuperTypeOf() and ConditionalTypeForParameter::isSuperTypeOf() compared the if/else branches of any two conditionals, ignoring subject / parameter name, target and negation. `($p is lowercase-string ? lowercase-string : string)` was therefore considered a supertype of `($p is '' ? decimal-int-string : string)` (decimal-int-string is lowercase), and TypeCombinator::intersect() dropped it. The branch comparison now only happens when the negation, the subject (or parameter name) and the target are equal; otherwise the late-resolvable default is used. - The same mistake made TypeCombinator::union() collapse conditionals with different conditions (wrong union results, and `is` vs `is not` conditionals absorbing each other). - ConditionalType::equals() and ConditionalTypeForParameter::equals() ignored `negated`, so `($p is int ? A : B)` equalled `($p is not int ? A : B)`. - Ported both changes to the turbo-ext C++ mirrors (ConditionalType.cpp, ConditionalTypeForParameter.cpp) via a shared hasSameCondition() helper, and extended the type-family differential coverage. --- src/Type/ConditionalType.php | 8 +- src/Type/ConditionalTypeForParameter.php | 8 +- tests/PHPStan/Analyser/nsrt/bug-15759.php | 84 +++++++++++++++++++ turbo-ext/src/ConditionalType.cpp | 61 ++++++++++---- turbo-ext/src/ConditionalTypeForParameter.cpp | 69 ++++++++++----- turbo-ext/tests/type-family.php | 7 ++ 6 files changed, 199 insertions(+), 38 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-15759.php diff --git a/src/Type/ConditionalType.php b/src/Type/ConditionalType.php index 65a3d363e7e..d6ce9e00558 100644 --- a/src/Type/ConditionalType.php +++ b/src/Type/ConditionalType.php @@ -61,7 +61,12 @@ public function isNegated(): bool public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { - if ($type instanceof self) { + if ( + $type instanceof self + && $this->negated === $type->negated + && $this->subject->equals($type->subject) + && $this->target->equals($type->target) + ) { return $this->if->isSuperTypeOf($type->if) ->and($this->else->isSuperTypeOf($type->else)); } @@ -92,6 +97,7 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc public function equals(Type $type): bool { return $type instanceof self + && $this->negated === $type->negated && $this->subject->equals($type->subject) && $this->target->equals($type->target) && $this->if->equals($type->if) diff --git a/src/Type/ConditionalTypeForParameter.php b/src/Type/ConditionalTypeForParameter.php index 56876bc7c2f..ebccbbc0f18 100644 --- a/src/Type/ConditionalTypeForParameter.php +++ b/src/Type/ConditionalTypeForParameter.php @@ -164,7 +164,12 @@ public function toConditional(Type $subject): Type public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { - if ($type instanceof self) { + if ( + $type instanceof self + && $this->negated === $type->negated + && $this->parameterName === $type->parameterName + && $this->target->equals($type->target) + ) { return $this->if->isSuperTypeOf($type->if) ->and($this->else->isSuperTypeOf($type->else)); } @@ -193,6 +198,7 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc public function equals(Type $type): bool { return $type instanceof self + && $this->negated === $type->negated && $this->parameterName === $type->parameterName && $this->target->equals($type->target) && $this->if->equals($type->if) diff --git a/tests/PHPStan/Analyser/nsrt/bug-15759.php b/tests/PHPStan/Analyser/nsrt/bug-15759.php new file mode 100644 index 00000000000..c8c6c13d032 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15759.php @@ -0,0 +1,84 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug15759; + +use function PHPStan\Testing\assertType; + +/** + * @return ( + * ( $p is '' ? decimal-int-string : string ) + * & ( $p is lowercase-string ? lowercase-string : string ) + * ) + */ +function with_decimal_int_string( string $p ): string { + return $p . '1'; +} + +/** + * @return ( + * ( $p is '' ? numeric-string : string ) + * & ( $p is lowercase-string ? lowercase-string : string ) + * ) + */ +function with_numeric_string( string $p ): string { + return $p . '1'; +} + +/** + * @return ( + * ( $p is '' ? non-decimal-int-string : string ) + * & ( $p is lowercase-string ? lowercase-string : string ) + * ) + */ +function with_non_decimal_int_string( string $p ): string { + return $p . '1'; +} + +function (): void { + assertType( 'lowercase-string', with_decimal_int_string( 'foo-' ) ); + assertType( 'lowercase-string', with_numeric_string( 'foo-' ) ); + assertType( 'lowercase-string', with_non_decimal_int_string( 'foo-' ) ); + assertType( 'decimal-int-string', with_decimal_int_string( '' ) ); + assertType( 'string', with_decimal_int_string( 'FOO' ) ); +}; + +/** + * @return ( $p is '' ? decimal-int-string : string )|( $p is lowercase-string ? lowercase-string : string ) + */ +function union_with_decimal_int_string( string $p ): string { + return $p . '1'; +} + +function (): void { + assertType( 'lowercase-string', union_with_decimal_int_string( '' ) ); + assertType( 'string', union_with_decimal_int_string( 'foo-' ) ); +}; + +/** + * @return ( $p is int ? int : string )|( $p is not int ? int : string ) + */ +function union_with_negated( int|string $p ): int|string { + return $p; +} + +function (): void { + assertType( 'int|string', union_with_negated( 1 ) ); + assertType( 'int|string', union_with_negated( 'a' ) ); +}; + +/** + * @template T of string + * @param T $p + * @return ( T is '' ? decimal-int-string : string ) + * & ( T is lowercase-string ? lowercase-string : string ) + */ +function template_with_decimal_int_string( string $p ): string { + return $p . '1'; +} + +function (): void { + assertType( 'lowercase-string', template_with_decimal_int_string( 'foo-' ) ); + assertType( 'decimal-int-string', template_with_decimal_int_string( '' ) ); +}; diff --git a/turbo-ext/src/ConditionalType.cpp b/turbo-ext/src/ConditionalType.cpp index a74661b27d5..162c4801298 100644 --- a/turbo-ext/src/ConditionalType.cpp +++ b/turbo-ext/src/ConditionalType.cpp @@ -79,12 +79,48 @@ class ConditionalType zv::Val getIf() const { return copyOfSlot(ifType()); } zv::Val getElse() const { return copyOfSlot(elseType()); } - /* for another conditional the if branches' answer combined with the - * else branches', the trait's default otherwise; UNDEF = pending - * exception */ + /* $type instanceof self with the same negation, an equal subject and + * an equal target; false with an exception pending */ + [[nodiscard]] bool hasSameCondition(zval *type, bool &out) const + { + if (!instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type)) { + out = false; + return true; + } + bool isNegated; + if (UNEXPECTED(!negated(isNegated))) return false; + zval *theirNegated = slot(Z_OBJ_P(type), slots::negated, "negated"); + if (UNEXPECTED(theirNegated == NULL)) return false; + if (isNegated != (Z_TYPE_P(theirNegated) == IS_TRUE)) { + out = false; + return true; + } + static const uint32_t parts[2] = { slots::subject, slots::target }; + static const char *const names[2] = { "subject", "target" }; + for (int i = 0; i < 2; i++) { + zval *mine = slot(self, parts[i], names[i]); + if (UNEXPECTED(mine == NULL)) return false; + zval *theirs = slot(Z_OBJ_P(type), parts[i], names[i]); + if (UNEXPECTED(theirs == NULL)) return false; + zv::Val equal = pt_type_op(Z_OBJ_P(mine), PT_OP_EQUALS, 1, theirs); + if (UNEXPECTED(equal.isUndef())) return false; + if (!zend_is_true(equal.raw())) { + out = false; + return true; + } + } + out = true; + return true; + } + + /* for another conditional with the same condition the if branches' + * answer combined with the else branches', the trait's default + * otherwise; UNDEF = pending exception */ zv::Val isSuperTypeOf(zval *type) const { - if (instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type)) { + bool sameCondition; + if (UNEXPECTED(!hasSameCondition(type, sameCondition))) return zv::Val(); + if (sameCondition) { zval *i = ifType(); if (UNEXPECTED(i == NULL)) return zv::Val(); zval *theirIf = slot(Z_OBJ_P(type), slots::if_, "if"); @@ -108,18 +144,15 @@ class ConditionalType /* array_merge() of the four parts' getReferencedTemplateTypes($positionVariance) */ zv::Val getReferencedTemplateTypes(zval *positionVariance) const { return mergedOfParts(PT_LC("getreferencedtemplatetypes"), 1, positionVariance); } - /* $type instanceof self with equal subject, target, if and else (the - * negation is not compared, as the twin does not); false with an - * exception pending */ + /* $type instanceof self with the same negation and equal subject, + * target, if and else; false with an exception pending */ bool equals(zval *type, bool &out) const { - if (!instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type)) { - out = false; - return true; - } - static const uint32_t parts[4] = { slots::subject, slots::target, slots::if_, slots::else_ }; - static const char *const names[4] = { "subject", "target", "if", "else" }; - for (int i = 0; i < 4; i++) { + if (UNEXPECTED(!hasSameCondition(type, out))) return false; + if (!out) return true; + static const uint32_t parts[2] = { slots::if_, slots::else_ }; + static const char *const names[2] = { "if", "else" }; + for (int i = 0; i < 2; i++) { zval *mine = slot(self, parts[i], names[i]); if (UNEXPECTED(mine == NULL)) return false; zval *theirs = slot(Z_OBJ_P(type), parts[i], names[i]); diff --git a/turbo-ext/src/ConditionalTypeForParameter.cpp b/turbo-ext/src/ConditionalTypeForParameter.cpp index bb59e3f028f..7a36cc9280d 100644 --- a/turbo-ext/src/ConditionalTypeForParameter.cpp +++ b/turbo-ext/src/ConditionalTypeForParameter.cpp @@ -196,12 +196,48 @@ class ConditionalTypeForParameter return pt_type_traverser_map_of(type, callback.raw()); } - /* for another conditional-for-parameter the if branches' answer - * combined with the else branches', the trait's default otherwise; - * UNDEF = pending exception */ + /* $type instanceof self with the same negation, the same parameter + * name and an equal target; false with an exception pending */ + [[nodiscard]] bool hasSameCondition(zval *type, bool &out) const + { + if (!instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type_for_parameter)) { + out = false; + return true; + } + bool isNegated; + if (UNEXPECTED(!negated(isNegated))) return false; + zval *theirNegated = slot(Z_OBJ_P(type), slots::negated, "negated"); + if (UNEXPECTED(theirNegated == NULL)) return false; + if (isNegated != (Z_TYPE_P(theirNegated) == IS_TRUE)) { + out = false; + return true; + } + zval *name = parameterName(); + if (UNEXPECTED(name == NULL)) return false; + zval *theirName = slot(Z_OBJ_P(type), slots::parameterName, "parameterName"); + if (UNEXPECTED(theirName == NULL)) return false; + if (!zend_string_equals(Z_STR_P(name), Z_STR_P(theirName))) { + out = false; + return true; + } + zval *mine = target(); + if (UNEXPECTED(mine == NULL)) return false; + zval *theirs = slot(Z_OBJ_P(type), slots::target, "target"); + if (UNEXPECTED(theirs == NULL)) return false; + zv::Val equal = pt_type_op(Z_OBJ_P(mine), PT_OP_EQUALS, 1, theirs); + if (UNEXPECTED(equal.isUndef())) return false; + out = zend_is_true(equal.raw()); + return true; + } + + /* for another conditional-for-parameter with the same condition the if + * branches' answer combined with the else branches', the trait's + * default otherwise; UNDEF = pending exception */ zv::Val isSuperTypeOf(zval *type) const { - if (instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type_for_parameter)) { + bool sameCondition; + if (UNEXPECTED(!hasSameCondition(type, sameCondition))) return zv::Val(); + if (sameCondition) { zval *i = ifType(); if (UNEXPECTED(i == NULL)) return zv::Val(); zval *theirIf = slot(Z_OBJ_P(type), slots::if_, "if"); @@ -225,26 +261,15 @@ class ConditionalTypeForParameter /* array_merge() of the target's, if's and else's getReferencedTemplateTypes($positionVariance) */ zv::Val getReferencedTemplateTypes(zval *positionVariance) const { return mergedOfParts(PT_LC("getreferencedtemplatetypes"), 1, positionVariance); } - /* $type instanceof self with the same parameter name and equal target, - * if and else (the negation is not compared, as the twin does not); - * false with an exception pending */ + /* $type instanceof self with the same negation, the same parameter name + * and equal target, if and else; false with an exception pending */ [[nodiscard]] bool equals(zval *type, bool &out) const { - if (!instanceof_function(Z_OBJCE_P(type), pt_ce_conditional_type_for_parameter)) { - out = false; - return true; - } - zval *name = parameterName(); - if (UNEXPECTED(name == NULL)) return false; - zval *theirName = slot(Z_OBJ_P(type), slots::parameterName, "parameterName"); - if (UNEXPECTED(theirName == NULL)) return false; - if (!zend_string_equals(Z_STR_P(name), Z_STR_P(theirName))) { - out = false; - return true; - } - static const uint32_t parts[3] = { slots::target, slots::if_, slots::else_ }; - static const char *const names[3] = { "target", "if", "else" }; - for (int i = 0; i < 3; i++) { + if (UNEXPECTED(!hasSameCondition(type, out))) return false; + if (!out) return true; + static const uint32_t parts[2] = { slots::if_, slots::else_ }; + static const char *const names[2] = { "if", "else" }; + for (int i = 0; i < 2; i++) { zval *mine = slot(self, parts[i], names[i]); if (UNEXPECTED(mine == NULL)) return false; zval *theirs = slot(Z_OBJ_P(type), parts[i], names[i]); diff --git a/turbo-ext/tests/type-family.php b/turbo-ext/tests/type-family.php index c6ff137585e..e4ec5cb9e4c 100644 --- a/turbo-ext/tests/type-family.php +++ b/turbo-ext/tests/type-family.php @@ -5330,6 +5330,13 @@ public function getKeyType(): \PHPStan\Type\Type $subjects['condParamYIsT']->isSuperTypeOf($subjects['condParamIsInt']), $subjects['condIntIsInt']->equals($subjects['condIntIsIntNegated']), $subjects['condParamIsInt']->equals($subjects['condParamIsIntNegated']), + // only conditionals on the same condition compare their branches + $subjects['condIntIsInt']->isSuperTypeOf($subjects['condStringIsInt']), + $subjects['condIntIsInt']->isSuperTypeOf(new \PHPStan\Type\ConditionalType(new \PHPStan\Type\IntegerType(), new \PHPStan\Type\IntegerType(), new \PHPStan\Type\Constant\ConstantStringType('a'), new \PHPStan\Type\Constant\ConstantBooleanType(true), false)), + $subjects['condIntIsInt']->isSuperTypeOf(new \PHPStan\Type\ConditionalType(new \PHPStan\Type\IntegerType(), new \PHPStan\Type\Constant\ConstantIntegerType(1), new \PHPStan\Type\Constant\ConstantStringType('a'), new \PHPStan\Type\Constant\ConstantBooleanType(true), false)), + $subjects['condParamIsInt']->isSuperTypeOf(new \PHPStan\Type\ConditionalTypeForParameter('$x', new \PHPStan\Type\IntegerType(), new \PHPStan\Type\Constant\ConstantStringType('a'), new \PHPStan\Type\Constant\ConstantBooleanType(true), false)), + $subjects['condParamIsInt']->isSuperTypeOf(new \PHPStan\Type\ConditionalTypeForParameter('$x', new \PHPStan\Type\StringType(), new \PHPStan\Type\Constant\ConstantStringType('a'), new \PHPStan\Type\Constant\ConstantBooleanType(true), false)), + $subjects['condParamIsInt']->isSuperTypeOf(new \PHPStan\Type\ConditionalTypeForParameter('$y', new \PHPStan\Type\IntegerType(), new \PHPStan\Type\Constant\ConstantStringType('a'), new \PHPStan\Type\Constant\ConstantBooleanType(true), false)), ]); // a traversed conditional keeps the mapped normalized branches as its // own: a chain of conditions on the same template type, narrowed level From 518de62aba589e01944ddbdd464e8a98e92e0cb6 Mon Sep 17 00:00:00 2001 From: phpstan-bot Date: Tue, 17 Nov 2026 09:44:09 +0000 Subject: [PATCH 2/2] Test equals() and isSuperTypeOf() of conditional types with differing conditions Co-Authored-By: Claude Opus 5.5 --- .../Type/ConditionalTypeForParameterTest.php | 114 +++++++++++++++ tests/PHPStan/Type/ConditionalTypeTest.php | 134 ++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 tests/PHPStan/Type/ConditionalTypeForParameterTest.php create mode 100644 tests/PHPStan/Type/ConditionalTypeTest.php diff --git a/tests/PHPStan/Type/ConditionalTypeForParameterTest.php b/tests/PHPStan/Type/ConditionalTypeForParameterTest.php new file mode 100644 index 00000000000..161d005864c --- /dev/null +++ b/tests/PHPStan/Type/ConditionalTypeForParameterTest.php @@ -0,0 +1,114 @@ + + */ + public static function dataIsSuperTypeOf(): iterable + { + $type = new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), false); + + yield 'same condition' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createYes(), + ]; + + yield 'same condition, narrower branches' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new ConstantIntegerType(1), new StringType(), false), + TrinaryLogic::createYes(), + ]; + + yield 'different negation' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), true), + TrinaryLogic::createMaybe(), + ]; + + yield 'different parameter name' => [ + $type, + new ConditionalTypeForParameter('$y', new IntegerType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createMaybe(), + ]; + + yield 'different target' => [ + $type, + new ConditionalTypeForParameter('$x', new StringType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createMaybe(), + ]; + } + + #[DataProvider('dataIsSuperTypeOf')] + public function testIsSuperTypeOf(ConditionalTypeForParameter $type, Type $otherType, TrinaryLogic $expectedResult): void + { + $actualResult = $type->isSuperTypeOf($otherType); + $this->assertSame( + $expectedResult->describe(), + $actualResult->describe(), + sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())), + ); + } + + /** + * @return iterable + */ + public static function dataEquals(): iterable + { + $type = new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), false); + + yield 'same' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), false), + true, + ]; + + yield 'different negation' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new StringType(), true), + false, + ]; + + yield 'different parameter name' => [ + $type, + new ConditionalTypeForParameter('$y', new IntegerType(), new IntegerType(), new StringType(), false), + false, + ]; + + yield 'different target' => [ + $type, + new ConditionalTypeForParameter('$x', new StringType(), new IntegerType(), new StringType(), false), + false, + ]; + + yield 'different if' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new StringType(), new StringType(), false), + false, + ]; + + yield 'different else' => [ + $type, + new ConditionalTypeForParameter('$x', new IntegerType(), new IntegerType(), new IntegerType(), false), + false, + ]; + } + + #[DataProvider('dataEquals')] + public function testEquals(ConditionalTypeForParameter $type, Type $otherType, bool $expected): void + { + $this->assertSame($expected, $type->equals($otherType)); + $this->assertSame($expected, $otherType->equals($type)); + } + +} diff --git a/tests/PHPStan/Type/ConditionalTypeTest.php b/tests/PHPStan/Type/ConditionalTypeTest.php new file mode 100644 index 00000000000..ffc2f9fe9af --- /dev/null +++ b/tests/PHPStan/Type/ConditionalTypeTest.php @@ -0,0 +1,134 @@ + + */ + public static function dataIsSuperTypeOf(): iterable + { + $t = self::templateType('T'); + $u = self::templateType('U'); + $tIsIntThenIntElseString = new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), false); + + yield 'same condition' => [ + $tIsIntThenIntElseString, + new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createYes(), + ]; + + yield 'same condition, narrower branches' => [ + $tIsIntThenIntElseString, + new ConditionalType($t, new IntegerType(), new ConstantIntegerType(1), new StringType(), false), + TrinaryLogic::createYes(), + ]; + + yield 'different negation' => [ + $tIsIntThenIntElseString, + new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), true), + TrinaryLogic::createMaybe(), + ]; + + yield 'different subject' => [ + $tIsIntThenIntElseString, + new ConditionalType($u, new IntegerType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createMaybe(), + ]; + + yield 'different target' => [ + $tIsIntThenIntElseString, + new ConditionalType($t, new StringType(), new IntegerType(), new StringType(), false), + TrinaryLogic::createMaybe(), + ]; + } + + #[DataProvider('dataIsSuperTypeOf')] + public function testIsSuperTypeOf(ConditionalType $type, Type $otherType, TrinaryLogic $expectedResult): void + { + $actualResult = $type->isSuperTypeOf($otherType); + $this->assertSame( + $expectedResult->describe(), + $actualResult->describe(), + sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())), + ); + } + + /** + * @return iterable + */ + public static function dataEquals(): iterable + { + $t = self::templateType('T'); + $u = self::templateType('U'); + $type = new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), false); + + yield 'same' => [ + $type, + new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), false), + true, + ]; + + yield 'different negation' => [ + $type, + new ConditionalType($t, new IntegerType(), new IntegerType(), new StringType(), true), + false, + ]; + + yield 'different subject' => [ + $type, + new ConditionalType($u, new IntegerType(), new IntegerType(), new StringType(), false), + false, + ]; + + yield 'different target' => [ + $type, + new ConditionalType($t, new StringType(), new IntegerType(), new StringType(), false), + false, + ]; + + yield 'different if' => [ + $type, + new ConditionalType($t, new IntegerType(), new StringType(), new StringType(), false), + false, + ]; + + yield 'different else' => [ + $type, + new ConditionalType($t, new IntegerType(), new IntegerType(), new IntegerType(), false), + false, + ]; + } + + #[DataProvider('dataEquals')] + public function testEquals(ConditionalType $type, Type $otherType, bool $expected): void + { + $this->assertSame($expected, $type->equals($otherType)); + $this->assertSame($expected, $otherType->equals($type)); + } + +}