Skip to content

Avoid false positif in_array always true #4073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public function findSpecifiedType(
continue;
}

$haystackArrayValueConstantScalarTypes = $haystackArrayValueType->getConstantScalarTypes();
if (count($haystackArrayValueConstantScalarTypes) > 1) {
continue;
}

foreach ($haystackArrayValueType->getConstantScalarTypes() as $constantScalarType) {
if ($constantScalarType->isSuperTypeOf($needleType)->yes()) {
continue 3;
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Variables/data/bug-10577.php';
yield __DIR__ . '/../Rules/Variables/data/bug-10610.php';
yield __DIR__ . '/../Rules/Comparison/data/bug-2550.php';
yield __DIR__ . '/../Rules/Comparison/data/bug-12412.php';
yield __DIR__ . '/../Rules/Properties/data/bug-3777.php';
yield __DIR__ . '/../Rules/Methods/data/bug-4552.php';
yield __DIR__ . '/../Rules/Methods/data/infer-array-key.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,28 @@ public function testBugPR3404(): void
]);
}

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

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

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

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

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

namespace Bug12412;

use function PHPStan\Testing\assertType;

/**
* @param 'A'|'B' $type
* @return list<string>
*/
function f(string $type): array {
$field_list = [ ];
if ($type === 'A') {
array_push($field_list, 'x1');
}
if ($type === 'B') {
array_push($field_list, 'x2');
}

assertType('bool', in_array('x1', $field_list, true));

array_push($field_list, 'x3');
assertType('bool', in_array('x1', $field_list, true));

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

namespace Bug12755;

class HelloWorld
{
/**
* @param array{
* key1: ?int,
* key2: ?string,
* } $myArray
*/
public function testOther(array $myArray): ?\stdClass
{
if (\in_array(null, $myArray, true)) {
return null;
}

return (object) $myArray;
}

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

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

namespace Bug13151;

class HelloWorld
{
/**
* @return array{a:0|1, b:0|1, c:0|1}
*/
public function extractAsArray(): array
{
return [
'a' => 1,
'b' => 1,
'c' => 1
];
}

public function test(): void
{
echo in_array(0, $this->extractAsArray(), true) ? "True" : "False";
}

public function test2(): void
{
echo in_array(0, $this->extractAsArray()) ? "True" : "False";
}
}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8818.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Bug8818;

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

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