Skip to content

Spreading of general arrays should not lead to a non-empty-array #818

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 2 commits into from
Dec 1, 2021
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
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ private function resolveType(Expr $node): Type
}
} else {
$arrayBuilder->degradeToGeneralArray();
$arrayBuilder->setOffsetValueType(new IntegerType(), $valueType->getIterableValueType());
$arrayBuilder->setOffsetValueType(new IntegerType(), $valueType->getIterableValueType(), !$valueType->isIterableAtLeastOnce()->yes() && !$valueType->getIterableValueType()->isIterableAtLeastOnce()->yes());
}
} else {
$arrayBuilder->setOffsetValueType(
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt

$this->keyTypes[] = TypeUtils::generalizeType($offsetType, GeneralizePrecision::moreSpecific());
$this->valueTypes[] = $valueType;
if ($optional) {
$this->optionalKeys[] = count($this->keyTypes) - 1;
}
$this->degradeToGeneralArray = true;
}

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 @@ -533,6 +533,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5017.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5992.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6001.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5287.php');

if (PHP_VERSION_ID >= 70400) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5458.php');
Expand Down
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/data/bug-5287.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace Bug5287;

use function PHPStan\Testing\assertType;

/**
* @param list<mixed> $arr
*/
function foo(array $arr): void
{
$arrSpread = [...$arr];
assertType('array<int, mixed>', $arrSpread);
}

/**
* @param list<non-empty-array> $arr
*/
function foo2(array $arr): void
{
$arrSpread = [...$arr];
assertType('non-empty-array<int, non-empty-array>', $arrSpread);
}

/**
* @param non-empty-list<string> $arr
*/
function foo3(array $arr): void
{
$arrSpread = [...$arr];
assertType('non-empty-array<int, string>', $arrSpread);
}

/**
* @param non-empty-array<string, int> $arr
*/
function foo3(array $arr): void
{
$arrSpread = [...$arr];
assertType('non-empty-array<int, int>', $arrSpread);
}

/**
* @param non-empty-array<mixed, bool|int> $arr
*/
function foo4(array $arr): void
{
$arrSpread = [...$arr];
assertType('non-empty-array<int, bool|int>', $arrSpread);
}

/**
* @param array{foo: 17, bar: 19} $arr
*/
function bar(array $arr): void
{
$arrSpread = [...$arr];
assertType('array{17, 19}', $arrSpread);
}