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
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function findSpecifiedType(
foreach ($haystackArrayTypes as $haystackArrayType) {
if ($haystackArrayType instanceof ConstantArrayType) {
foreach ($haystackArrayType->getValueTypes() as $i => $haystackArrayValueType) {
if ($haystackArrayType->isOptionalKey($i)) {
if (count($haystackArrayValueType->getFiniteTypes()) > 1 || $haystackArrayType->isOptionalKey($i)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,16 @@ public function testBug8818(): void

public function testBug12755(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12755.php'], []);
$this->analyse([__DIR__ . '/data/bug-12755.php'], [
[
'Call to function in_array() with arguments null, array{key1: bool|null, key2: null} and true will always evaluate to true.',
51,
$tipText,
],
]);
}

public function testBug12412(): void
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-12755.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace Bug12755;

class MyEnum
{
public const ONE = 'one';
public const TWO = 'two';
public const THREE = 'three';
}

class HelloWorld
{
/**
Expand Down Expand Up @@ -32,4 +39,52 @@ public function testBool(array $myArray): ?\stdClass

return (object) $myArray;
}

/**
* @param array{
* key1: ?bool,
* key2: null,
* } $myArray
*/
public function testNull(array $myArray): ?\stdClass
{
if (\in_array(null, $myArray, true)) {
return null;
}

return (object) $myArray;
}

/**
* @param list<MyEnum::*> $stack
*/
public function testEnum(array $stack): bool
{
return count($stack) === 1 && in_array(MyEnum::ONE, $stack, true);
}

/**
* @param array{1|2|3} $stack
* @param array{1|2|3, 1|2|3} $stack2
* @param array{1|2|3, 2|3} $stack3
* @param array{a?: 1, b: 2|3} $stack4
* @param array{a?: 1} $stack5
*/
public function sayHello(array $stack, array $stack2, array $stack3, array $stack4, array $stack5): void
{
if (in_array(1, $stack, true)) {
}

if (in_array(1, $stack2, true)) {
}

if (in_array(1, $stack3, true)) {
}

if (in_array(1, $stack4, true)) {
}

if (in_array(1, $stack5, true)) {
}
}
}
Loading