1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #110 - failure to eliminate false from array|false on isset

This commit is contained in:
Matthew Brown 2017-03-05 23:47:53 -05:00
parent 4619ef1390
commit 048d7f0142
2 changed files with 35 additions and 1 deletions

View File

@ -154,9 +154,19 @@ class TypeChecker
$clauses[] = new Clause([$base => ['isset']]);
foreach ($key_parts as $key_part_dim) {
if (count($key_parts)) {
$clauses[] = new Clause([$base => ['!false']]);
$clauses[] = new Clause([$base => ['!int']]);
}
foreach ($key_parts as $i => $key_part_dim) {
$base .= '[' . $key_part_dim . ']';
$clauses[] = new Clause([$base => ['isset']]);
if ($i < count($key_parts) - 1) {
$clauses[] = new Clause([$base => ['!false']]);
$clauses[] = new Clause([$base => ['!int']]);
}
}
} else {
$clauses[] = new Clause([$var => [$type]]);

View File

@ -112,6 +112,30 @@ class IssetTest extends PHPUnit_Framework_TestCase
$this->assertEquals('mixed', (string) $context->vars_in_scope['$foo[\'a\']']);
}
/**
* @return void
*/
public function testIssetKeyedOffsetOrFalse()
{
$file_checker = new FileChecker(
'somefile.php',
$this->project_checker,
self::$parser->parse('<?php
/** @return void */
function takesString(string $str) {}
$bar = rand(0, 1) ? ["foo" => "bar"] : false;
if (isset($bar["foo"])) {
takesString($bar["foo"]);
}
')
);
$context = new Context();
$context->vars_in_scope['$foo'] = \Psalm\Type::getArray();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/