Repository navigation
Compare branches of two conditional types in isSuperTypeOf() only when they share the same condition, and include negation in equals()
#7097
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
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||
|
Contributor
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 line is not covered by a test |
||||||||||||
| && $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 | ||||||||||||
|
Contributor
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 change is not covered by a test
Collaborator
Author
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 added the missing test coverage in one commit, "Not covered by a test" (the I added two unit tests,
With the old versions of the two PHP classes swapped back in, 8 of these cases fail: each new condition in "Do we really support intersections of conditional types in phpdoc?" Yes, they already worked before this PR; the only problem was the wrong collapse it fixes. phpdoc-parser reads I checked this by analysing a standalone file with
The WordPress |
||||||||||||
| && $this->parameterName === $type->parameterName | ||||||||||||
| && $this->target->equals($type->target) | ||||||||||||
| && $this->if->equals($type->if) | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php // lint >= 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 ) | ||
| * ) | ||
|
Comment on lines
+10
to
+13
Contributor
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. do we really support intersections of conditional types in phpdoc? |
||
| */ | ||
| 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( '' ) ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type; | ||
|
|
||
| use PHPStan\Testing\PHPStanTestCase; | ||
| use PHPStan\TrinaryLogic; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use function sprintf; | ||
|
|
||
| class ConditionalTypeForParameterTest extends PHPStanTestCase | ||
| { | ||
|
|
||
| /** | ||
| * @return iterable<string, array{ConditionalTypeForParameter, Type, TrinaryLogic}> | ||
| */ | ||
| 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<string, array{ConditionalTypeForParameter, Type, bool}> | ||
| */ | ||
| 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)); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type; | ||
|
|
||
| use PHPStan\Testing\PHPStanTestCase; | ||
| use PHPStan\TrinaryLogic; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\Generic\TemplateTypeFactory; | ||
| use PHPStan\Type\Generic\TemplateTypeScope; | ||
| use PHPStan\Type\Generic\TemplateTypeVariance; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use function sprintf; | ||
|
|
||
| class ConditionalTypeTest extends PHPStanTestCase | ||
| { | ||
|
|
||
| /** | ||
| * @param non-empty-string $name | ||
| */ | ||
| private static function templateType(string $name): Type | ||
| { | ||
| return TemplateTypeFactory::create( | ||
| TemplateTypeScope::createWithFunction('_'), | ||
| $name, | ||
| new MixedType(), | ||
| TemplateTypeVariance::createInvariant(), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{ConditionalType, Type, TrinaryLogic}> | ||
| */ | ||
| 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<string, array{ConditionalType, Type, bool}> | ||
| */ | ||
| 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)); | ||
| } | ||
|
|
||
| } |
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.
this change is not covered by a test