Skip to content

Do not merge BenevolentUnionType with UnionType #2058

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

Closed
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
36 changes: 2 additions & 34 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,12 @@ public static function union(Type ...$types): Type
return new NeverType();
}

$benevolentTypes = [];
$benevolentUnionObject = null;
// transform A | (B | C) to A | B | C
for ($i = 0; $i < $typesCount; $i++) {
if ($types[$i] instanceof BenevolentUnionType) {
if ($types[$i] instanceof TemplateBenevolentUnionType && $benevolentUnionObject === null) {
$benevolentUnionObject = $types[$i];
}
$benevolentTypesCount = 0;
$typesInner = $types[$i]->getTypes();
foreach ($typesInner as $benevolentInnerType) {
$benevolentTypesCount++;
$benevolentTypes[$benevolentInnerType->describe(VerbosityLevel::value())] = $benevolentInnerType;
}
array_splice($types, $i, 1, $typesInner);
$typesCount += $benevolentTypesCount - 1;
if (!($types[$i] instanceof UnionType)) {
continue;
}
if (!($types[$i] instanceof UnionType)) {
if ($types[$i] instanceof BenevolentUnionType) {
continue;
}
if ($types[$i] instanceof TemplateType) {
Expand Down Expand Up @@ -347,25 +334,6 @@ public static function union(Type ...$types): Type
return $types[0];
}

if ($benevolentTypes !== []) {
$tempTypes = $types;
foreach ($tempTypes as $i => $type) {
if (!isset($benevolentTypes[$type->describe(VerbosityLevel::value())])) {
break;
}

unset($tempTypes[$i]);
}

if ($tempTypes === []) {
if ($benevolentUnionObject instanceof TemplateBenevolentUnionType) {
return $benevolentUnionObject->withTypes($types);
}

return new BenevolentUnionType($types);
}
}

return new UnionType($types, true);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function __construct(private array $types, private bool $normalized = fal
if (!($type instanceof UnionType)) {
continue;
}
if ($type instanceof BenevolentUnionType) {
continue;
}
if ($type instanceof TemplateType) {
continue;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4942,11 +4942,11 @@ public function dataArrayFunctions(): array
'array_search(new stdClass, $generalStringKeys, true)',
],
[
'int|string|false',
'(int|string)|false',
'array_search($mixed, $array, true)',
],
[
'int|string|false',
'(int|string)|false',
'array_search($mixed, $array, false)',
],
[
Expand Down Expand Up @@ -5006,15 +5006,15 @@ public function dataArrayFunctions(): array
'array_search(\'id\', doFoo() ? $thisDoesNotExistAndIsMixedInUnion : false, true)',
],
[
'int|string|false',
'(int|string)|false',
'array_search(1, $generalIntegers, true)',
],
[
'int|string|false',
'(int|string)|false',
'array_search(1, $generalIntegers, false)',
],
[
'int|string|false',
'(int|string)|false',
'array_search(1, $generalIntegers)',
],
[
Expand Down Expand Up @@ -8820,11 +8820,11 @@ public function dataPhp73Functions(): array
'json_decode($mixed, false, 512, $integer | JSON_THROW_ON_ERROR | JSON_NUMERIC_CHECK)',
],
[
'int|string|null',
'(int|string)|null',
'array_key_first($mixedArray)',
],
[
'int|string|null',
'(int|string)|null',
'array_key_last($mixedArray)',
],
[
Expand Down Expand Up @@ -8904,7 +8904,7 @@ public function dataPhp73Functions(): array
'$hrtime3',
],
[
'array{int, int}|float|int',
'(float|int)|array{int, int}',
'$hrtime4',
],
];
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/array-search-php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Foo
public function mixedAndSubtractedArray($mixed, string $string): void
{
if (is_array($mixed)) {
assertType('int|string|false', array_search('foo', $mixed, true));
assertType('int|string|false', array_search('foo', $mixed));
assertType('int|string|false', array_search($string, $mixed, true));
assertType('(int|string)|false', array_search('foo', $mixed, true));
assertType('(int|string)|false', array_search('foo', $mixed));
assertType('(int|string)|false', array_search($string, $mixed, true));
} else {
assertType('mixed~array', $mixed);
assertType('null', array_search('foo', $mixed, true));
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/array-search-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Foo
public function mixedAndSubtractedArray($mixed, string $string): void
{
if (is_array($mixed)) {
assertType('int|string|false', array_search('foo', $mixed, true));
assertType('int|string|false', array_search('foo', $mixed));
assertType('int|string|false', array_search($string, $mixed, true));
assertType('(int|string)|false', array_search('foo', $mixed, true));
assertType('(int|string)|false', array_search('foo', $mixed));
assertType('(int|string)|false', array_search($string, $mixed, true));
} else {
assertType('mixed~array', $mixed);
assertType('*NEVER*', array_search('foo', $mixed, true));
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/array-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Foo
public function nonEmpty(array $arr, string $string): void
{
/** @var non-empty-array<string> $arr */
assertType('int|string|false', array_search('foo', $arr, true));
assertType('int|string|false', array_search('foo', $arr));
assertType('int|string|false', array_search($string, $arr, true));
assertType('(int|string)|false', array_search('foo', $arr, true));
assertType('(int|string)|false', array_search('foo', $arr));
assertType('(int|string)|false', array_search($string, $arr, true));
}

public function normalArrays(array $arr, string $string): void
Expand Down
87 changes: 87 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7279.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types = 1);

namespace Bug7279;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
class Timeline {}
class Percentage {}

/**
* @template K of array-key
* @template T
*
* @param array<K, T> $array
* @param (callable(T, K): bool) $fn
*
* @return ($array is non-empty-array ? T|null : null)
*/
function find(array $array, callable $fn): mixed
{
foreach ($array as $key => $value) {
if ($fn($value, $key)) {
return $value;
}
}

return null;
}

/**
* @template K of array-key
* @template T
*
* @param array<K, T> $array
* @param (callable(T, K): bool) $fn
*
* @return ($array is non-empty-array ? K|null : null)
*/
function findKey(array $array, callable $fn): string|int|null
{
foreach ($array as $key => $value) {
if ($fn($value, $key)) {
return $key;
}
}

return null;
}

/**
* @param callable(mixed): bool $callback
* @param array<never, never> $emptyList
* @param array{} $emptyMap
* @param array<int, string> $unknownList
* @param array{id?: int, name?: string} $unknownMap
* @param non-empty-array<int, Timeline<Percentage>> $nonEmptyList
* @param array{work: Timeline<Percentage>} $nonEmptyMap
*/
function doFoo(callable $callback, array $emptyList, array $emptyMap, array $unknownList, array $unknownMap, array $nonEmptyList, array $nonEmptyMap): void
{
// Everything works great for find()

assertType('null', find([], $callback));
assertType('null', find($emptyList, $callback));
assertType('null', find($emptyMap, $callback));

assertType('string|null', find($unknownList, $callback));
assertType('int|string|null', find($unknownMap, $callback));

assertType('Bug7279\Timeline<Bug7279\Percentage>|null', find($nonEmptyList, $callback));
assertType('Bug7279\Timeline<Bug7279\Percentage>|null', find($nonEmptyMap, $callback));

// But everything goes to hell for findKey() ?!?

assertType('null', findKey([], $callback));
assertType('null', findKey($emptyList, $callback));
assertType('null', findKey($emptyMap, $callback));

assertType('int|null', findKey($unknownList, $callback));
assertType("'id'|'name'|null", findKey($unknownMap, $callback));

assertType('int|null', findKey($nonEmptyList, $callback));
assertType("'work'|null", findKey($nonEmptyMap, $callback));
}
4 changes: 3 additions & 1 deletion tests/PHPStan/Analyser/nsrt/bug-7291.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php // lint >= 8.0

declare(strict_types=1);

namespace Bug7291;

Expand Down
Loading
Loading