Skip to content

Commit

Permalink
Fixed in_array() with non-empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 9, 2020
1 parent 3e8ad90 commit a36843e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Type/TypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public static function getArrays(Type $type): array
return $matchingTypes;
}

if ($type instanceof IntersectionType) {
$matchingTypes = [];
foreach ($type->getTypes() as $innerType) {
if (!$innerType instanceof ArrayType) {
continue;
}
foreach (self::getArrays($innerType) as $innerInnerType) {
$matchingTypes[] = $innerInnerType;
}
}

return $matchingTypes;
}

return [];
}

Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/check-type-function-call.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,37 @@ public function doLorem(\SimpleXMLElement $xml): void
}

}

class InArray2
{

/**
* @param array<int> $ints
*/
public function doFoo($ints): void
{
if ($ints === []) {
return;
}

if (in_array(0, $ints, true)) {

}
}

/**
* @param \stdClass $std
* @param array<int, \stdClass|null> $stdClassesOrNull
*/
public function doBar($std, $stdClassesOrNull): void
{
if ($stdClassesOrNull === []) {
return;
}

if (in_array($std, $stdClassesOrNull, true)) {

}
}

}

0 comments on commit a36843e

Please sign in to comment.