Skip to content
Closed
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
16 changes: 15 additions & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,21 @@ public function unsetOffset(Type $offsetType): Type
$k++;
}

return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, TrinaryLogic::createNo());
$newIsList = TrinaryLogic::createNo();
if (!$this->isList->no() && in_array($i, $this->optionalKeys, true)) {
$preserveIsList = true;
foreach ($newKeyTypes as $k2 => $newKeyType2) {
if (!$newKeyType2 instanceof ConstantIntegerType || $newKeyType2->getValue() !== $k2) {
$preserveIsList = false;
break;
}
}
if ($preserveIsList) {
$newIsList = $this->isList;
}
}

return new self($newKeyTypes, $newValueTypes, $this->nextAutoIndexes, $newOptionalKeys, $newIsList);
}

return $this;
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14177.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug14177;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param list{0: string, 1: string, 2?: string, 3?: string} $b
*/
public function testList(array $b): void
{
if (array_key_exists(3, $b)) {
assertType('list{0: string, 1: string, 2?: string, 3: string}', $b);
} else {
assertType('list{0: string, 1: string, 2?: string}', $b);
}
assertType('list{0: string, 1: string, 2?: string, 3?: string}', $b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1145,4 +1145,10 @@ public function testPr4375(): void
$this->analyse([__DIR__ . '/data/pr-4375.php'], []);
}

public function testBug14177(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-14177.php'], []);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-14177.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug14177;

class HelloWorld
{
public function placeholderToEditor(string $html): void
{
$result = preg_replace_callback(
'~\[image\\sid="(\\d+)"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?]~',
function (array $matches): string {
$id = (int) $matches[1];

$replacement = sprintf(
'<img src="%s"%s/>',
$id,
array_key_exists(3, $matches) ? sprintf(' class="%s"', $matches[3]) : '',
);

return array_key_exists(2, $matches) && $matches[2] !== ''
? sprintf('<a href="%s">%s</a>', $matches[2], $replacement)
: $replacement;
},
$html,
);
}
}
Loading