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
13 changes: 13 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
use function is_array;
use function is_string;
use function ltrim;
use function max;
use function md5;
use function sprintf;
use function str_starts_with;
Expand Down Expand Up @@ -3338,6 +3339,18 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
) {
continue 2;
}

$guardConstantArrays = $conditionalTypeHolder->getType()->getConstantArrays();
if (count($guardConstantArrays) === 0) {
continue;
}

$guardMaxKeyCount = max(array_map(static fn ($a) => count($a->getKeyTypes()), $guardConstantArrays));
Comment thread
VincentLanglet marked this conversation as resolved.
foreach ($specifiedExpressions[$holderExprString]->getType()->getConstantArrays() as $currentConstantArray) {
if (count($currentConstantArray->getKeyTypes()) > $guardMaxKeyCount) {
continue 3;
}
}
}

$conditions[$conditionalExprString][] = $conditionalExpression;
Expand Down
80 changes: 80 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14595.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types = 1);

namespace Bug14595;

use function PHPStan\Testing\assertType;

/**
* @param array<mixed> $data
* @param array{
* multiple: 0|1|2
* , total: bool
* } $options
*/
function formulaire_edition(array $data, array $options): void {
$instructions = [ ];
$instructions[] = "foo";
if ($options['multiple'] != 1 || $options['total'])
$instructions[] = "bar";
assertType('0|1|2', $options['multiple']);
if (!$options['total'])
$instructions[] = "baz";
assertType('0|1|2', $options['multiple']);
if (!$options['total'])
$instructions[] = "qux";
assertType('0|1|2', $options['multiple']);
}

/**
* @param array<mixed> $data
* @param array{
* multiple: 0|1|2
* } $options
*/
function formulaire_edition_separate_bool(array $data, array $options, bool $total): void {
$instructions = [ ];
$instructions[] = "foo";
if ($options['multiple'] != 1 || $total) {
$instructions[] = "bar";
}
assertType('0|1|2', $options['multiple']);
if (!$total) {
$instructions[] = "baz";
}
assertType('0|1|2', $options['multiple']);
if (!$total) {
$instructions[] = "qux";
}
assertType('0|1|2', $options['multiple']);
}

/**
* @param array<mixed> $data
* @param array{
* multiple: 0|1|2
* } $options
*/
function multiple_guard_constant_arrays(array $data, array $options, bool $flag1, bool $flag2): void {
$instructions = [];
if ($flag1) {
$instructions[] = "a";
$instructions[] = "b";
} else {
$instructions[] = "c";
}
// $instructions is array{'a', 'b'}|array{'c'} — guard has 2 constant arrays with key counts 2 and 1
if ($options['multiple'] != 1 || $flag2) {
$instructions[] = "d";
}
assertType('0|1|2', $options['multiple']);
if (!$flag2) {
$instructions[] = "e";
}
assertType('0|1|2', $options['multiple']);
if (!$flag2) {
$instructions[] = "f";
}
assertType('0|1|2', $options['multiple']);
}
Loading