Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,10 @@

$keyIndexMap = $this->getKeyIndexMap();
$otherKeyValues = [];
// the merged array has $this's key order, so a different order of the
// shared integer keys would make the merged list-ness wrong
$checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no();

Check warning on line 3164 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": @@ @@ $otherKeyValues = []; // the merged array has $this's key order, so a different order of the // shared integer keys would make the merged list-ness wrong - $checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no(); + $checkKeyOrder = !$this->isList->no() || $otherArray->isList->yes(); $previousIndex = -1; foreach ($otherArray->keyTypes as $j => $keyType) {

Check warning on line 3164 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": @@ @@ $otherKeyValues = []; // the merged array has $this's key order, so a different order of the // shared integer keys would make the merged list-ness wrong - $checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no(); + $checkKeyOrder = $this->isList->yes() || !$otherArray->isList->no(); $previousIndex = -1; foreach ($otherArray->keyTypes as $j => $keyType) {

Check warning on line 3164 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": @@ @@ $otherKeyValues = []; // the merged array has $this's key order, so a different order of the // shared integer keys would make the merged list-ness wrong - $checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no(); + $checkKeyOrder = !$this->isList->no() || $otherArray->isList->yes(); $previousIndex = -1; foreach ($otherArray->keyTypes as $j => $keyType) {

Check warning on line 3164 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": @@ @@ $otherKeyValues = []; // the merged array has $this's key order, so a different order of the // shared integer keys would make the merged list-ness wrong - $checkKeyOrder = !$this->isList->no() || !$otherArray->isList->no(); + $checkKeyOrder = $this->isList->yes() || !$otherArray->isList->no(); $previousIndex = -1; foreach ($otherArray->keyTypes as $j => $keyType) {
$previousIndex = -1;

foreach ($otherArray->keyTypes as $j => $keyType) {
$keyValue = $keyType->getValue();
Expand All @@ -3167,6 +3171,13 @@
return false;
}

if ($checkKeyOrder && is_int($keyValue)) {
if ($i < $previousIndex) {
return false;
}
$previousIndex = $i;
}

$otherKeyValues[$keyValue] = true;

$valueType = $this->valueTypes[$i];
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15679.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types = 1);

namespace Bug15679;

use function PHPStan\Testing\assertType;

function f(bool $b): void
{
$x = $b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0];
assertType('array{1, 0}|array{1: 0, 0: 0}', $x);
assertType('bool', array_is_list($x));
if (array_is_list($x)) {
assertType('array{1, 0}', $x);
} else {
assertType('array{1: 0, 0: 0}', $x);
}

$y = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'];
assertType("array{'c', 'a'}|array{1: 'a', 0: 'b'}", $y);
assertType('bool', array_is_list($y));
}

function listFirst(bool $b): void
{
$x = $b ? [0 => 1, 1 => 0] : [1 => 0, 0 => 0];
assertType('array{1, 0}|array{1: 0, 0: 0}', $x);
assertType('bool', array_is_list($x));
if (array_is_list($x)) {
assertType('array{1, 0}', $x);
} else {
assertType('array{1: 0, 0: 0}', $x);
}
}

function sameValues(bool $b): void
{
$x = $b ? [1 => 0, 0 => 0] : [0 => 0, 1 => 0];
assertType('array{0, 0}|array{1: 0, 0: 0}', $x);
assertType('bool', array_is_list($x));

$y = $b ? [0 => 0, 1 => 0] : [1 => 0, 0 => 0];
assertType('array{0, 0}|array{1: 0, 0: 0}', $y);
assertType('bool', array_is_list($y));
}

function differingAtTwoKeys(bool $b): void
{
$x = $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'd'];
assertType("array{'c', 'd'}|array{1: 'a', 0: 'b'}", $x);
assertType('bool', array_is_list($x));
}

function sameOrder(bool $b): void
{
$x = $b ? [0 => 0, 1 => 0] : [0 => 1, 1 => 0];
assertType('array{0|1, 0}', $x);
assertType('true', array_is_list($x));

$y = $b ? [1 => 0, 0 => 0] : [1 => 0, 0 => 1];
assertType('array{1: 0, 0: 0|1}', $y);
assertType('false', array_is_list($y));
}

function ifElse(bool $b): void
{
if ($b) {
$x = [];
$x[1] = 0;
$x[0] = 0;
} else {
$x = [];
$x[0] = 1;
$x[1] = 0;
}
assertType('array{1, 0}|array{1: 0, 0: 0}', $x);
assertType('bool', array_is_list($x));
}
18 changes: 18 additions & 0 deletions turbo-ext/src/ConstantArrayType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4974,6 +4974,16 @@ class ConstantArrayType
zv::Val keyIndexMap = getKeyIndexMap();
if (UNEXPECTED(keyIndexMap.isUndef())) return false;
zv::Arr otherKeyValues = zv::Arr::create((uint32_t) otherKeyTypesCount);
/* the merged array has $this's key order, so a different order of the
* shared integer keys would make the merged list-ness wrong */
zval *l = isListSlot();
zval *otherL = l != NULL ? slotOf(other, slots::isList, "isList") : NULL;
if (UNEXPECTED(otherL == NULL)) return false;
zend_long isListValue = trinaryOf(l);
zend_long otherIsListValue = trinaryOf(otherL);
if (UNEXPECTED(isListValue < 0 || otherIsListValue < 0)) return false;
bool checkKeyOrder = isListValue != PT_TRI_NO || otherIsListValue != PT_TRI_NO;
zend_long previousIndex = -1;

for (zv::ArrayEntry entry : zv::ArrRef(otherK)) {
zend_long j = (zend_long) entry.indexKey();
Expand All @@ -4985,6 +4995,14 @@ class ConstantArrayType
return true;
}

if (checkKeyOrder && Z_TYPE_P(keyValueZv.raw()) == IS_LONG) {
if (Z_LVAL_P(iZv) < previousIndex) {
out = false;
return true;
}
previousIndex = Z_LVAL_P(iZv);
}

zval trueZv;
ZVAL_TRUE(&trueZv);
if (UNEXPECTED(!mapSet(otherKeyValues.table(), keyValueZv.raw(), &trueZv))) return false;
Expand Down
Loading