Skip to content

Commit

Permalink
Fix is_array false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 14, 2021
1 parent 428baf5 commit 187921e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public function findSpecifiedType(
return true;
}

if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
return true;
}

return (
$node instanceof FuncCall
|| $node instanceof MethodCall
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,11 @@ public function testBug4657(): void
$this->analyse([__DIR__ . '/data/bug-4657.php'], []);
}

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

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

namespace Bug4999;

class HelloWorld
{
/**
* @param mixed[] $array
*/
public function extractPhone(array $array): void
{
$phone = null;

if (array_key_exists('PHONE', $array)) {
while (null === $phone && is_array($value = array_shift($array['PHONE']))) {
$phone = self::parsePhone($value['VALUE']);
}
}
}

private static function parsePhone($something): ?string
{
return null === $something ? null : '911';
}
}

0 comments on commit 187921e

Please sign in to comment.