Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/Type/BenevolentUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function filterTypes(callable $filterCb): Type
return $result;
}

public function tryRemove(Type $typeToRemove): ?Type
{
$result = parent::tryRemove($typeToRemove);
if ($result === null) {
return null;
}

return TypeUtils::toBenevolentUnion($result);
}

public function describe(VerbosityLevel $level): string
{
return '(' . parent::describe($level) . ')';
Expand Down
6 changes: 3 additions & 3 deletions src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ public function isArgument(): bool

public function toArgument(): TemplateType
{
return new self(
return TemplateTypeFactory::create(
$this->scope,
new TemplateTypeArgumentStrategy(),
$this->variance,
$this->name,
TemplateTypeHelper::toArgument($this->getBound()),
$this->variance,
new TemplateTypeArgumentStrategy(),
$this->default !== null ? TemplateTypeHelper::toArgument($this->default) : null,
);
}
Expand Down
33 changes: 32 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,38 @@ public function traverseSimultaneously(Type $right, callable $cb): Type

public function tryRemove(Type $typeToRemove): ?Type
{
return $this->unionTypes(static fn (Type $type): Type => TypeCombinator::remove($type, $typeToRemove));
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.

@phpstan-bot do we need a similar fix in IntersectionType->tryRemove() or any other Type->tryRemove() implementation? try to come up with a failling test and implement a fix afterwards

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.

The working tree is clean — no code changes are needed. My investigation confirms that IntersectionType::tryRemove() and all other tryRemove() implementations are not affected by the same normalization bug that this PR fixes in UnionType::tryRemove().

$innerTypes = [];
$changed = false;
foreach ($this->types as $innerType) {
$removed = TypeCombinator::remove($innerType, $typeToRemove);
if (!$removed->equals($innerType)) {
$changed = true;
}
if ($removed instanceof NeverType) {
continue;
}
if ($removed instanceof self && !$removed instanceof TemplateType) {
foreach ($removed->getTypes() as $removedInnerType) {
$innerTypes[] = $removedInnerType;
}
} else {
$innerTypes[] = $removed;
}
}

if (!$changed) {
return null;
}

if (count($innerTypes) === 0) {
return new NeverType();
}

if (count($innerTypes) === 1) {
return $innerTypes[0];
}

return new UnionType($innerTypes);
}

public function exponentiate(Type $exponent): Type
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/finite-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function doFoo(bool $a, bool $b): void
return;
}

assertType('array{bool, true}', $array);
assertType('array{false, true}|array{true, true}', $array);
if ($array === [true, true]) {
assertType('array{true, true}', $array);
return;
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,18 @@ public function testBug12790(): void
$this->analyse([__DIR__ . '/data/bug-12790.php'], []);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug10128(): void
{
$this->analyse([__DIR__ . '/data/bug-10128.php'], []);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug11453(): void
{
$this->analyse([__DIR__ . '/data/bug-11453.php'], []);
}

#[RequiresPhp('>= 8.0.0')]
public function testBug11310(): void
{
Expand Down
84 changes: 84 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-10128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug10128;

enum FooBar {
case Bar;
case Baz;
case Foo;
}

function getFoo(): FooBar {
return FooBar::Bar;
}

function test(): void {
$first = getFoo();
$second = getFoo();

$type = match ([$first, $second]) {
[FooBar::Bar, FooBar::Bar] => 1,
[FooBar::Bar, FooBar::Baz] => 2,
[FooBar::Bar, FooBar::Foo] => 3,

[FooBar::Baz, FooBar::Bar] => 1,
[FooBar::Baz, FooBar::Baz] => 2,
[FooBar::Baz, FooBar::Foo] => 3,

[FooBar::Foo, FooBar::Bar] => 1,
[FooBar::Foo, FooBar::Baz] => 2,
[FooBar::Foo, FooBar::Foo] => 3,
};

$type2 = match ([$first, $second]) {
[FooBar::Bar, FooBar::Bar],
[FooBar::Bar, FooBar::Baz],
[FooBar::Bar, FooBar::Foo] => 1,

[FooBar::Baz, FooBar::Bar] => 1,
[FooBar::Baz, FooBar::Baz] => 2,
[FooBar::Baz, FooBar::Foo] => 3,

[FooBar::Foo, FooBar::Bar] => 1,
[FooBar::Foo, FooBar::Baz] => 2,
[FooBar::Foo, FooBar::Foo] => 3,
};

$type3 = match ([$first, $second]) {
[FooBar::Bar, FooBar::Bar],
[FooBar::Baz, FooBar::Baz],
[FooBar::Foo, FooBar::Foo],
[FooBar::Foo, FooBar::Baz] => 1,

[FooBar::Bar, FooBar::Baz],
[FooBar::Baz, FooBar::Bar],
[FooBar::Bar, FooBar::Foo],
[FooBar::Foo, FooBar::Bar] => 2,

[FooBar::Baz, FooBar::Foo] => 3,
};
}

enum TwoCases {
case Foo;
case Bar;
}

function getTwoCases(): TwoCases {
return TwoCases::Foo;
}

function testTwoCases(): void {
$first = getTwoCases();
$second = getTwoCases();

$type = match ([$first, $second]) {
[TwoCases::Foo, TwoCases::Foo] => 1,
[TwoCases::Bar, TwoCases::Bar] => 1,

[TwoCases::Bar, TwoCases::Foo] => 2,
[TwoCases::Foo, TwoCases::Bar] => 2,
};
}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11453.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug11453;

/** @return 1|2|3 */
function getInt(): int {
return 1;
}

/** @return 'a'|'b'|'c' */
function getString(): string {
return 'a';
}

function test(): void {
$int = getInt();
$string = getString();

$type = match ([$int, $string]) {
[1, 'a'] => 'one-a',
[1, 'b'] => 'one-b',
[1, 'c'] => 'one-c',

[2, 'a'] => 'two-a',
[2, 'b'] => 'two-b',
[2, 'c'] => 'two-c',

[3, 'a'] => 'three-a',
[3, 'b'] => 'three-b',
[3, 'c'] => 'three-c',
};
}
Loading