Skip to content

Commit 0143e40

Browse files
committed
Narrow an array that array_is_list() rejects to a non-empty one
The empty array is a list, so an array that is not a list holds something. In the false branch of array_is_list(), array<K, V> now becomes non-empty-array<K, V> and array{a?: string} becomes array{a: string}. Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
1 parent f542b25 commit 0143e40

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Analyser\SpecifiedTypes;
8+
use PHPStan\Analyser\TypeSpecifier;
9+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
10+
use PHPStan\Analyser\TypeSpecifierContext;
11+
use PHPStan\DependencyInjection\AutowiredService;
12+
use PHPStan\Reflection\FunctionReflection;
13+
use PHPStan\ShouldNotHappenException;
14+
use PHPStan\Type\Accessory\AccessoryArrayListType;
15+
use PHPStan\Type\Accessory\NonEmptyArrayType;
16+
use PHPStan\Type\ArrayType;
17+
use PHPStan\Type\FunctionTypeSpecifyingExtension;
18+
use PHPStan\Type\IntegerRangeType;
19+
use PHPStan\Type\MixedType;
20+
use PHPStan\Type\TypeCombinator;
21+
use function strtolower;
22+
23+
/**
24+
* An array that array_is_list() rejects holds something: the empty array is a
25+
* list. So `!array_is_list($a)` makes an array `$a` non-empty, also where its
26+
* type says it is never a list, as `array<string, int>` - PHPStan treats an
27+
* array whose keys are no list keys as never a list, which the empty one is.
28+
*/
29+
#[AutowiredService]
30+
final class ArrayIsListFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
31+
{
32+
33+
private TypeSpecifier $typeSpecifier;
34+
35+
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
36+
{
37+
return strtolower($functionReflection->getName()) === 'array_is_list'
38+
&& !$context->null();
39+
}
40+
41+
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
42+
{
43+
$args = $node->getArgs();
44+
if (!isset($args[0])) {
45+
return new SpecifiedTypes();
46+
}
47+
if ($context->null()) {
48+
throw new ShouldNotHappenException();
49+
}
50+
51+
$listType = TypeCombinator::intersect(
52+
new ArrayType(IntegerRangeType::fromInterval(0, null), new MixedType()),
53+
new AccessoryArrayListType(),
54+
);
55+
$specifiedTypes = $this->typeSpecifier->create($args[0]->value, $listType, $context, $scope);
56+
$valueType = $scope->getType($args[0]->value);
57+
if ($context->true() || !$valueType->isArray()->yes()) {
58+
return $specifiedTypes;
59+
}
60+
61+
return $specifiedTypes->unionWith(
62+
$this->typeSpecifier->create($args[0]->value, TypeCombinator::intersect($valueType, new NonEmptyArrayType()), TypeSpecifierContext::createTrue(), $scope),
63+
);
64+
}
65+
66+
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
67+
{
68+
$this->typeSpecifier = $typeSpecifier;
69+
}
70+
71+
}

‎tests/PHPStan/Analyser/nsrt/bug-14938.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public function narrowing(array $optStr): void
4949
if (array_is_list($optStr)) {
5050
assertType('array{}', $optStr);
5151
} else {
52-
// array_is_list()'s false branch is not narrowed, so `a` stays optional
53-
// here rather than being refined to array{a: string}.
54-
assertType('array{a?: string}', $optStr);
52+
// an array that is not a list is not empty, so `a` is there
53+
assertType('array{a: string}', $optStr);
5554
}
5655
}
5756

0 commit comments

Comments
 (0)