Skip to content
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
22 changes: 19 additions & 3 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use function count;
use function implode;
use function in_array;
use function is_int;
use function is_string;
use function min;
use function pow;
Expand Down Expand Up @@ -1848,16 +1849,31 @@
{
$offsetType = $offsetType->toArrayKey();
$optionalKeys = $this->optionalKeys;
$isList = $this->isList->yes();

Check warning on line 1852 in src/Type/Constant/ConstantArrayType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { $offsetType = $offsetType->toArrayKey(); $optionalKeys = $this->optionalKeys; - $isList = $this->isList->yes(); + $isList = !$this->isList->no(); foreach ($this->keyTypes as $i => $keyType) { if (!$keyType->equals($offsetType)) { continue;

Check warning on line 1852 in src/Type/Constant/ConstantArrayType.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { $offsetType = $offsetType->toArrayKey(); $optionalKeys = $this->optionalKeys; - $isList = $this->isList->yes(); + $isList = !$this->isList->no(); foreach ($this->keyTypes as $i => $keyType) { if (!$keyType->equals($offsetType)) { continue;
foreach ($this->keyTypes as $i => $keyType) {
if (!$keyType->equals($offsetType)) {
continue;
}

$keyValue = $keyType->getValue();
foreach ($optionalKeys as $j => $key) {
if ($i === $key) {
unset($optionalKeys[$j]);
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, array_values($optionalKeys), $this->isList);
if (
$i !== $key
&& (
!$isList
|| !is_int($keyValue)
|| !is_int($this->keyTypes[$key]->getValue())
|| $this->keyTypes[$key]->getValue() >= $keyValue
)
) {
continue;
}

unset($optionalKeys[$j]);
}

if (count($this->optionalKeys) !== count($optionalKeys)) {
return new self($this->keyTypes, $this->valueTypes, $this->nextAutoIndexes, array_values($optionalKeys), $this->isList);
}

break;
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-14177.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HelloWorld
public function testList(array $b): void
{
if (array_key_exists(3, $b)) {
assertType('list{0: string, 1: string, 2?: string, 3: string}', $b);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 3 is required, 2 shouldn't be described as optional.

assertType('array{string, string, string, string}', $b);
} else {
assertType('array{0: string, 1: string, 2?: string}', $b);
}
Expand Down Expand Up @@ -208,10 +208,10 @@ public function testFoo($l): void
{
if (array_key_exists(2, $l, true)) {
assertType('true', array_is_list($l));
assertType('list{0?: string, 1?: string, 2: string}', $l);
assertType('array{string, string, string}', $l);
if (array_key_exists(1, $l, true)) {
assertType('true', array_is_list($l));
assertType('list{0?: string, 1: string, 2: string}', $l);
assertType('array{string, string, string}', $l);
} else {
assertType('true', array_is_list($l));
assertType('*NEVER*', $l);
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ public function testPr4374(): void
]);
}

public function testIssetConstantArray(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/isset-constant-array.php'], [
[
'Offset 2 on array{0: string, 1: string, 2: string, 3: string, 4?: string} in isset() always exists and is not nullable.',
13,
],
[
'Offset 3 on array{string, string, string, string, string} in isset() always exists and is not nullable.',
17,
],
]);
}

public function testBug10640(): void
{
$this->treatPhpDocTypesAsCertain = true;
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Variables/data/isset-constant-array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace IssetConstantArray;

class HelloWorld
{
/**
* @param list{0: string, 1: string, 2?: string, 3?: string, 4?: string} $list
*/
public function sayHello(array $list): bool
{
if (isset($list[3])) {
return isset($list[2]); // offset 3 implies offset 2;
}

if (isset($list[4])) {
return isset($list[3]); // offset 4 implies offset 3;
}

return false;
}
}
Loading