Skip to content

Commit 3d1cd0e

Browse files
POC force list in ConstantArrayTypeBuilder
1 parent 5b79cca commit 3d1cd0e

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,13 +1060,15 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
10601060
$builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope), $itemNode->optional);
10611061
}
10621062

1063-
$arrayType = $builder->getArray();
1064-
1065-
$accessories = [];
1066-
if (in_array($typeNode->kind, [
1063+
$isList = in_array($typeNode->kind, [
10671064
ArrayShapeNode::KIND_LIST,
10681065
ArrayShapeNode::KIND_NON_EMPTY_LIST,
1069-
], true)) {
1066+
], true);
1067+
1068+
$arrayType = $isList ? $builder->getList() : $builder->getArray();
1069+
1070+
$accessories = [];
1071+
if ($isList) {
10701072
$accessories[] = new AccessoryArrayListType();
10711073
}
10721074

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\CallableType;
1212
use PHPStan\Type\ClosureType;
1313
use PHPStan\Type\IntersectionType;
14+
use PHPStan\Type\NeverType;
1415
use PHPStan\Type\Type;
1516
use PHPStan\Type\TypeCombinator;
1617
use PHPStan\Type\TypeUtils;
@@ -375,6 +376,17 @@ public function getArray(): Type
375376
return new IntersectionType([$array, ...$types]);
376377
}
377378

379+
public function getList(): Type
380+
{
381+
if ($this->isList->no()) {
382+
return new NeverType();
383+
}
384+
385+
$this->isList = TrinaryLogic::createYes();
386+
387+
return $this->getArray();
388+
}
389+
378390
public function isList(): bool
379391
{
380392
return $this->isList->yes();

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ public function testPr4374(): void
499499
]);
500500
}
501501

502+
public function testIssetConstantArray(): void
503+
{
504+
$this->treatPhpDocTypesAsCertain = true;
505+
506+
$this->analyse([__DIR__ . '/data/isset-constant-array.php'], []);
507+
}
508+
502509
public function testBug10640(): void
503510
{
504511
$this->treatPhpDocTypesAsCertain = true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace IssetConstantArray;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param list{0: string, 1: string, 2?: string, 3?: string, 4?: string} $list
9+
*/
10+
public function sayHello(array $list): bool
11+
{
12+
if (isset($list[3])) {
13+
return isset($list[2]); // offset 3 implies offset 2;
14+
}
15+
16+
if (isset($list[4])) {
17+
return isset($list[3]); // offset 4 implies offset 3;
18+
}
19+
20+
return false;
21+
}
22+
}

0 commit comments

Comments
 (0)