Skip to content

Commit c4ad5bd

Browse files
committed
Add test cases
1 parent b514893 commit c4ad5bd

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,16 @@ public function testAlwaysTruePregMatch(): void
973973

974974
public function testBug12755(): void
975975
{
976+
$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%</>.';
977+
976978
$this->treatPhpDocTypesAsCertain = true;
977-
$this->analyse([__DIR__ . '/data/bug-12755.php'], []);
979+
$this->analyse([__DIR__ . '/data/bug-12755.php'], [
980+
[
981+
'Call to function in_array() with arguments null, array{key1: bool|null, key2: null} and true will always evaluate to true.',
982+
51,
983+
$tipText,
984+
],
985+
]);
978986
}
979987

980988
}

tests/PHPStan/Rules/Comparison/data/bug-12755.php

+55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace Bug12755;
44

5+
class MyEnum
6+
{
7+
public const ONE = 'one';
8+
public const TWO = 'two';
9+
public const THREE = 'three';
10+
}
11+
512
class HelloWorld
613
{
714
/**
@@ -32,4 +39,52 @@ public function testBool(array $myArray): ?\stdClass
3239

3340
return (object) $myArray;
3441
}
42+
43+
/**
44+
* @param array{
45+
* key1: ?bool,
46+
* key2: null,
47+
* } $myArray
48+
*/
49+
public function testNull(array $myArray): ?\stdClass
50+
{
51+
if (\in_array(null, $myArray, true)) {
52+
return null;
53+
}
54+
55+
return (object) $myArray;
56+
}
57+
58+
/**
59+
* @param list<MyEnum::*> $stack
60+
*/
61+
public function testEnum(array $stack): bool
62+
{
63+
return count($stack) === 1 && in_array(MyEnum::ONE, $stack, true);
64+
}
65+
66+
/**
67+
* @param array{1|2|3} $stack
68+
* @param array{1|2|3, 1|2|3} $stack2
69+
* @param array{1|2|3, 2|3} $stack3
70+
* @param array{a?: 1, b: 2|3} $stack4
71+
* @param array{a?: 1} $stack5
72+
*/
73+
public function sayHello(array $stack, array $stack2, array $stack3, array $stack4, array $stack5): void
74+
{
75+
if (in_array(1, $stack, true)) {
76+
}
77+
78+
if (in_array(1, $stack2, true)) {
79+
}
80+
81+
if (in_array(1, $stack3, true)) {
82+
}
83+
84+
if (in_array(1, $stack4, true)) {
85+
}
86+
87+
if (in_array(1, $stack5, true)) {
88+
}
89+
}
3590
}

0 commit comments

Comments
 (0)