Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Type/ConditionalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -92,6 +97,7 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->negated === $type->negated

Copy link
Copy Markdown
Contributor

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

&& $this->subject->equals($type->subject)
&& $this->target->equals($type->target)
&& $this->if->equals($type->if)
Expand Down
8 changes: 7 additions & 1 deletion src/Type/ConditionalTypeForParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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));
}
Expand Down Expand Up @@ -193,6 +198,7 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc
public function equals(Type $type): bool
{
return $type instanceof self
&& $this->negated === $type->negated

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the missing test coverage in one commit, 518de62ab, and changed no source code. make tests, make phpstan and make cs all pass. Nothing is pushed.

"Not covered by a test" (the negated check in equals() of both classes, and the negated and parameterName checks in ConditionalTypeForParameter::isSuperTypeOf()):

I added two unit tests, tests/PHPStan/Type/ConditionalTypeTest.php and tests/PHPStan/Type/ConditionalTypeForParameterTest.php. Each one checks:

  • isSuperTypeOf(): returns "yes" for the same condition, including when the other type's branches are narrower. Returns "maybe" when only the negation, the subject (or parameter name, e.g. $x vs $y), or the target differs.
  • equals(): returns true for identical conditionals, and false when exactly one of these differs: negation, subject or parameter name, target, if, else. Both directions are checked.

With the old versions of the two PHP classes swapped back in, 8 of these cases fail: each new condition in isSuperTypeOf() and the negated check in both equals() methods. All 22 pass with the fix.

"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 (A) & (B) with parenthesised conditionals as an intersection node. TypeNodeResolver::resolveIntersectionTypeNode() (src/PhpDoc/TypeNodeResolver.php:701) combines the parts with TypeCombinator::intersect(), and each conditional is resolved against the argument at the call site.

I checked this by analysing a standalone file with bin/phpstan:

Argument Result
'foo-' lowercase-string
'' decimal-int-string
'FOO' string
an unknown string string

The WordPress wp_unique_id() return type in the issue uses exactly this form. I found no existing test with an intersection of conditional types, so bug-15759.php is the first one.

&& $this->parameterName === $type->parameterName
&& $this->target->equals($type->target)
&& $this->if->equals($type->if)
Expand Down
84 changes: 84 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15759.php
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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( '' ) );
};
114 changes: 114 additions & 0 deletions tests/PHPStan/Type/ConditionalTypeForParameterTest.php
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));
}

}
134 changes: 134 additions & 0 deletions tests/PHPStan/Type/ConditionalTypeTest.php
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));
}

}
Loading
Loading