Skip to content

Commit 8207a19

Browse files
Simplify
1 parent f149a5c commit 8207a19

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
10651065
ArrayShapeNode::KIND_NON_EMPTY_LIST,
10661066
], true);
10671067

1068-
$arrayType = $isList ? $builder->getList() : $builder->getArray();
1068+
$arrayType = $builder->getArray($isList);
10691069

10701070
$accessories = [];
10711071
if ($isList) {

src/Type/Constant/ConstantArrayType.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use function count;
6262
use function implode;
6363
use function in_array;
64+
use function is_int;
6465
use function is_string;
6566
use function min;
6667
use function pow;
@@ -1812,23 +1813,21 @@ public function makeOffsetRequired(Type $offsetType): self
18121813
}
18131814

18141815
$keyValue = $keyType->getValue();
1815-
$changed = false;
18161816
foreach ($optionalKeys as $j => $key) {
18171817
if (
18181818
$i === $key
18191819
|| (
18201820
$isList
1821-
&& \is_int($keyValue)
1822-
&& \is_int($this->keyTypes[$key]->getValue())
1821+
&& is_int($keyValue)
1822+
&& is_int($this->keyTypes[$key]->getValue())
18231823
&& $this->keyTypes[$key]->getValue() < $keyValue
18241824
)
18251825
) {
18261826
unset($optionalKeys[$j]);
1827-
$changed = true;
18281827
}
18291828
}
18301829

1831-
if ($changed) {
1830+
if (count($this->optionalKeys) !== count($optionalKeys)) {
18321831
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, array_values($optionalKeys), $this->isList);
18331832
}
18341833

src/Type/Constant/ConstantArrayTypeBuilder.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,16 @@ public function disableArrayDegradation(): void
325325
$this->disableArrayDegradation = true;
326326
}
327327

328-
public function getArray(): Type
328+
public function getArray(bool $forceList = false): Type
329329
{
330+
if ($forceList) {
331+
if ($this->isList->no()) {
332+
return new NeverType();
333+
}
334+
335+
$this->isList = TrinaryLogic::createYes();
336+
}
337+
330338
$keyTypesCount = count($this->keyTypes);
331339
if ($keyTypesCount === 0) {
332340
return new ConstantArrayType([], []);
@@ -376,17 +384,6 @@ public function getArray(): Type
376384
return new IntersectionType([$array, ...$types]);
377385
}
378386

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-
390387
public function isList(): bool
391388
{
392389
return $this->isList->yes();

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,17 @@ public function testIssetConstantArray(): void
503503
{
504504
$this->treatPhpDocTypesAsCertain = true;
505505

506-
$this->analyse([__DIR__ . '/data/isset-constant-array.php'], []);
506+
$this->analyse([__DIR__ . '/data/isset-constant-array.php'], [
507+
[
508+
'Offset 2 on list{0: string, 1: string, 2: string, 3: string, 4?: string} in isset() always exists and is not nullable.',
509+
13,
510+
],
511+
[
512+
'Offset 3 on array{string, string, string, string, string} in isset() always exists and is not nullable.',
513+
17,
514+
],
515+
516+
]);
507517
}
508518

509519
public function testBug10640(): void

0 commit comments

Comments
 (0)