Skip to content

Tagged unions #1547

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

Merged
merged 1 commit into from
Aug 27, 2022
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/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,18 @@ public function isKeysSupersetOf(self $otherArray): bool
}

$otherKeys = $otherArray->keyTypes;
foreach ($this->keyTypes as $keyType) {
foreach ($this->keyTypes as $i => $keyType) {
foreach ($otherArray->keyTypes as $j => $otherKeyType) {
if (!$keyType->equals($otherKeyType)) {
continue;
}

$valueType = $this->valueTypes[$i];
$otherValueType = $otherArray->valueTypes[$j];
if ($valueType->isSuperTypeOf($otherValueType)->no()) {
continue;
}

unset($otherKeys[$j]);
continue 2;
}
Expand Down
24 changes: 12 additions & 12 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3056,7 +3056,7 @@ public function dataBinaryOperations(): array
"sprintf('%s %s', 'foo', 'bar')",
],
[
'array{}|array{0: \'password\'|\'username\', 1?: \'password\'}',
'array{}|array{\'password\'}|array{0: \'username\', 1?: \'password\'}',
'$coalesceArray',
],
[
Expand Down Expand Up @@ -4571,7 +4571,7 @@ public function dataArrayFunctions(): array
'array_intersect_key($integers, [])',
],
[
'array{1|4, 2|5, 3|6}',
'array{1, 2, 3}|array{4, 5, 6}',
'array_intersect_key(...[$integers, [4, 5, 6]])',
],
[
Expand Down Expand Up @@ -5389,7 +5389,7 @@ public function dataFunctions(): array
'$strSplitConstantStringWithInvalidSplitLengthType',
],
[
'array{\'a\'|\'g\', \'b\'|\'h\', \'c\'|\'i\', \'d\'|\'j\', \'e\'|\'k\', \'f\'|\'l\'}',
"array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}",
'$strSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
Expand Down Expand Up @@ -5632,7 +5632,7 @@ public function dataRangeFunction(): array
'range(1, doFoo() ? 1 : 2)',
],
[
'array{0: -1|1, 1?: 0|2, 2?: 1, 3?: 2}',
'array{0: -1, 1: 0, 2: 1, 3?: 2}|array{0: 1, 1?: 2}',
'range(doFoo() ? -1 : 1, doFoo() ? 1 : 2)',
],
[
Expand Down Expand Up @@ -8347,19 +8347,19 @@ public function dataIsset(): array
'$array[\'b\']',
],
[
'array{a: 1|2|3, b: 2|3, c?: 4}',
'array{a: 1, b: 2}|array{a: 3, b: 3, c: 4}',
'$array',
],
[
'array{a: 1|2|3, b: 2|3|null, c?: 4}',
'array{a: 1, b: 2}|array{a: 3, b: 3, c: 4}|array{a: 3, b: null}',
'$arrayCopy',
],
[
'array{a: 1|2|3, c?: 4}',
'array{a: 2}',
'$anotherArrayCopy',
],
[
'array{a: 1|2|3, b?: 2|3|null, c?: 4}',
'array{a: 1, b: 2}|array{a: 2}|array{a: 3, b: 3, c: 4}|array{a: 3, b: null}',
'$yetAnotherArrayCopy',
],
[
Expand Down Expand Up @@ -8391,15 +8391,15 @@ public function dataIsset(): array
'$lookup[$a] ?? false',
],
[
'\'foo\'|false',
'\'foo\'',
'$nullableArray[\'a\'] ?? false',
],
[
'\'bar\'',
'$nullableArray[\'b\'] ?? false',
],
[
'\'baz\'|false',
'\'baz\'',
'$nullableArray[\'c\'] ?? false',
],
];
Expand Down Expand Up @@ -8777,7 +8777,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithInvalidSplitLengthType',
],
[
'array{\'a\'|\'g\', \'b\'|\'h\', \'c\'|\'i\', \'d\'|\'j\', \'e\'|\'k\', \'f\'|\'l\'}',
"array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}",
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength',
],
[
Expand Down Expand Up @@ -8833,7 +8833,7 @@ public function dataPhp74Functions(): array
'$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding',
],
[
'array{\'a\'|\'g\', \'b\'|\'h\', \'c\'|\'i\', \'d\'|\'j\', \'e\'|\'k\', \'f\'|\'l\'}',
"array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}",
'$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding',
],
[
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-filter-constant.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-intersect-key-constant.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-array-bug.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/tagged-unions.php');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/array-merge2.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function arrayMergeUnionType($array1, $array2): void
public function arrayMergeUnionTypeArrayShapes($array1, $array2): void
{
assertType("array<int, array{bar: '2'}|array{foo: '1'}>", array_merge($array1, $array1));
assertType("array<int, array{bar: '2'|'3'}|array{foo: '1'|'2'}>", array_merge($array1, $array2));
assertType("array<int, array{bar: '2'|'3'}|array{foo: '1'|'2'}>", array_merge($array2, $array1));
assertType("array<int, array{bar: '2'}|array{bar: '3'}|array{foo: '1'}|array{foo: '2'}>", array_merge($array1, $array2));
assertType("array<int, array{bar: '2'}|array{bar: '3'}|array{foo: '1'}|array{foo: '2'}>", array_merge($array2, $array1));
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/array-replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function arrayReplaceUnionType($array1, $array2): void
public function arrayReplaceUnionTypeArrayShapes($array1, $array2): void
{
assertType("array<int, array{bar: '2'}|array{foo: '1'}>", array_replace($array1, $array1));
assertType("array<int, array{bar: '2'|'3'}|array{foo: '1'|'2'}>", array_replace($array1, $array2));
assertType("array<int, array{bar: '2'|'3'}|array{foo: '1'|'2'}>", array_replace($array2, $array1));
assertType("array<int, array{bar: '2'}|array{bar: '3'}|array{foo: '1'}|array{foo: '2'}>", array_replace($array1, $array2));
assertType("array<int, array{bar: '2'}|array{bar: '3'}|array{foo: '1'}|array{foo: '2'}>", array_replace($array2, $array1));
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/bug-3269.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static function bar(array $intervalGroups): void
}
}

assertType('array<int, array{version: string, operator: string, side: \'end\'|\'start\'}>', $borders);
assertType("array<int, array{version: string, operator: string, side: 'end'}|array{version: string, operator: string, side: 'start'}>", $borders);

foreach ($borders as $border) {
assertType('array{version: string, operator: string, side: \'end\'|\'start\'}', $border);
assertType("array{version: string, operator: string, side: 'end'}|array{version: string, operator: string, side: 'start'}", $border);
assertType('\'end\'|\'start\'', $border['side']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-6383.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function doFoo(string $country): void {

foreach ($options as $key => $option) {
if (isset($option['only_in_country'])) {
assertType("array{value: 'a'|'b'|'c', checked: false, only_in_country: array{0: 'BE'|'DE', 1?: 'CH', 2?: 'DE', 3?: 'DK', 4?: 'FR', 5?: 'NL', 6?: 'SE'}}", $option);
assertType("array{value: 'a', checked: false, only_in_country: array{'DE'}}|array{value: 'b', checked: false, only_in_country: array{'BE', 'CH', 'DE', 'DK', 'FR', 'NL', 'SE'}}", $option);
continue;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/bug-6936-limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public function testLimits():void
$arr[] = 'g';
}

assertType("array{0: 1|'a'|'b'|'c'|'d'|'e'|'f'|'g', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}", $arr + $arr2);
assertType("array{'e', 2|'f'|'g', 3|'g'}|array{'f', 2|'g', 3}|array{'g', 2, 3}|array{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}|array{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g', 2: 3|'d'|'e'|'f'|'g', 3?: 'e'|'f'|'g', 4?: 'f'|'g', 5?: 'g'}|array{0: 'c', 1: 2|'d'|'e'|'f'|'g', 2: 3|'e'|'f'|'g', 3?: 'f'|'g', 4?: 'g'}|array{0: 'd', 1: 2|'e'|'f'|'g', 2: 3|'f'|'g', 3?: 'g'}|array{1, 2, 3}", $arr + $arr2);
if (rand(0,1)) {
$arr[] = 'h';
}

assertType("array{0: 1|'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}", $arr + $arr2);
assertType("array{'f', 2|'g'|'h', 3|'h'}|array{'g', 2|'h', 3}|array{'h', 2, 3}|array{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}|array{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'d'|'e'|'f'|'g'|'h', 3?: 'e'|'f'|'g'|'h', 4?: 'f'|'g'|'h', 5?: 'g'|'h', 6?: 'h'}|array{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h', 2: 3|'e'|'f'|'g'|'h', 3?: 'f'|'g'|'h', 4?: 'g'|'h', 5?: 'h'}|array{0: 'd', 1: 2|'e'|'f'|'g'|'h', 2: 3|'f'|'g'|'h', 3?: 'g'|'h', 4?: 'h'}|array{0: 'e', 1: 2|'f'|'g'|'h', 2: 3|'g'|'h', 3?: 'h'}|array{1, 2, 3}", $arr + $arr2);
if (rand(0,1)) {
$arr[] = 'i';
}

assertType("array{0: 1|'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}", $arr + $arr2);
assertType("array{'g', 2|'h'|'i', 3|'i'}|array{'h', 2|'i', 3}|array{'i', 2, 3}|array{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}|array{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'e'|'f'|'g'|'h'|'i', 4?: 'f'|'g'|'h'|'i', 5?: 'g'|'h'|'i', 6?: 'h'|'i', 7?: 'i'}|array{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'e'|'f'|'g'|'h'|'i', 3?: 'f'|'g'|'h'|'i', 4?: 'g'|'h'|'i', 5?: 'h'|'i', 6?: 'i'}|array{0: 'd', 1: 2|'e'|'f'|'g'|'h'|'i', 2: 3|'f'|'g'|'h'|'i', 3?: 'g'|'h'|'i', 4?: 'h'|'i', 5?: 'i'}|array{0: 'e', 1: 2|'f'|'g'|'h'|'i', 2: 3|'g'|'h'|'i', 3?: 'h'|'i', 4?: 'i'}|array{0: 'f', 1: 2|'g'|'h'|'i', 2: 3|'h'|'i', 3?: 'i'}|array{1, 2, 3}", $arr + $arr2);
}
}
184 changes: 184 additions & 0 deletions tests/PHPStan/Analyser/data/tagged-unions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

namespace TaggedUnions;

use function PHPStan\Testing\assertType;

class Foo
{

/** @param array{A: int}|array{A: string} $foo */
public function doFoo(array $foo)
{
assertType('array{A: int}|array{A: string}', $foo);
if (is_int($foo['A'])) {
assertType("array{A: int}", $foo);
$foo['B'] = 'yo';
assertType("array{A: int, B: 'yo'}", $foo);
} else {
assertType('array{A: string}', $foo);
}

assertType("array{A: int, B: 'yo'}|array{A: string}", $foo);
}

/** @param array{A: int, B: 1}|array{A: string, B: 2} $foo */
public function doFoo2(array $foo)
{
assertType('array{A: int, B: 1}|array{A: string, B: 2}', $foo);
if (is_int($foo['A'])) {
assertType("array{A: int, B: 1}", $foo);
} else {
assertType("array{A: string, B: 2}", $foo);
}

assertType('array{A: int, B: 1}|array{A: string, B: 2}', $foo);
}

/** @param array{A: int, B: 1}|array{A: string, C: 1} $foo */
public function doFoo3(array $foo)
{
assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
if (is_int($foo['A'])) {
assertType("array{A: int, B: 1}", $foo);
} else {
assertType("array{A: string, C: 1}", $foo);
}

assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
}

/** @param array{A: int, B: 1}|array{A: string, C: 1} $foo */
public function doFoo4(array $foo)
{
assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
if (isset($foo['C'])) {
assertType("array{A: string, C: 1}", $foo);
} else {
assertType("array{A: int, B: 1}|array{A: string, C: 1}", $foo); // could be array{A: int, B: 1}
}

assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
}

/**
* @param array{A: int}|array{A: int|string} $foo
* @return void
*/
public function doBar(array $foo)
{
assertType('array{A: int|string}', $foo);
}

}

/**
* @phpstan-type Topping string
* @phpstan-type Salsa string
*
* @phpstan-type Pizza array{type: 'pizza', toppings: Topping[]}
* @phpstan-type Pasta array{type: 'pasta', salsa: Salsa}
* @phpstan-type Meal Pizza|Pasta
*/
class Test
{
/**
* @param Meal $meal
*/
function test($meal): void {
assertType("array{type: 'pasta', salsa: string}|array{type: 'pizza', toppings: array<string>}", $meal);
if ($meal['type'] === 'pizza') {
assertType("array{type: 'pizza', toppings: array<string>}", $meal);
} else {
assertType("array{type: 'pasta', salsa: string}", $meal);
}
assertType("array{type: 'pasta', salsa: string}|array{type: 'pizza', toppings: array<string>}", $meal);
}
}

class HelloWorld
{
/**
* @return array{updated: true, id: int}|array{updated: false, id: null}
*/
public function sayHello(): array
{
return ['updated' => false, 'id' => 5];
}

public function doFoo()
{
$x = $this->sayHello();
assertType("array{updated: false, id: null}|array{updated: true, id: int}", $x);
if ($x['updated']) {
assertType('array{updated: true, id: int}', $x);
}
}
}

/**
* @psalm-type A array{tag: 'A', foo: bool}
* @psalm-type B array{tag: 'B'}
*/
class X {
/** @psalm-param A|B $arr */
public function ooo(array $arr): void {
assertType("array{tag: 'A', foo: bool}|array{tag: 'B'}", $arr);
if ($arr['tag'] === 'A') {
assertType("array{tag: 'A', foo: bool}", $arr);
} else {
assertType("array{tag: 'B'}", $arr);
}
assertType("array{tag: 'A', foo: bool}|array{tag: 'B'}", $arr);
}
}

class TipsFromArnaud
{

// https://github.com/phpstan/phpstan/issues/7666#issuecomment-1191563801

/**
* @param array{a: int}|array{a: int} $a
*/
public function doFoo(array $a): void
{
assertType('array{a: int}', $a);
}

/**
* @param array{a: int}|array{a: string} $a
*/
public function doFoo2(array $a): void
{
// could be: array{a: int|string}
assertType('array{a: int}|array{a: string}', $a);
}

/**
* @param array{a: int, b: int}|array{a: string, b: string} $a
*/
public function doFoo3(array $a): void
{
assertType('array{a: int, b: int}|array{a: string, b: string}', $a);
}

/**
* @param array{a: int, b: string}|array{a: string, b:string} $a
*/
public function doFoo4(array $a): void
{
// could be: array{a: int|string, b: string}
assertType('array{a: int, b: string}|array{a: string, b: string}', $a);
}

/**
* @param array{a: int, b: string, c: string}|array{a: string, b: string, c: string} $a
*/
public function doFoo5(array $a): void
{
// could be: array{a: int|string, b: string, c: string}
assertType('array{a: int, b: string, c: string}|array{a: string, b: string, c: string}', $a);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testRule(): void
145,
],
[
'Offset \'c\' does not exist on array{c: bool}|array{e: true}.',
'Offset \'c\' does not exist on array{c: false}|array{c: true}|array{e: true}.',
171,
],
[
Expand Down
Loading