1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix generation of MixedArrayAccess issue

This commit is contained in:
Matthew Brown 2016-12-15 01:28:36 -05:00
parent a44dd4f6b0
commit dcec735d11
2 changed files with 50 additions and 3 deletions

View File

@ -784,6 +784,8 @@ class FetchChecker
return false;
}
$inferred_key_type = null;
if (isset($stmt->var->inferredType)) {
/** @var Type\Union */
$var_type = $stmt->var->inferredType;
@ -808,6 +810,12 @@ class FetchChecker
} else {
$key_type = $type->type_params[0];
}
if ($inferred_key_type) {
Type::combineUnionTypes($key_type, $type->type_params[0]);
} else {
$inferred_key_type = $type->type_params[0];
}
}
}
}
@ -950,6 +958,12 @@ class FetchChecker
$key_type = Type::getInt();
}
if (!$inferred_key_type) {
$inferred_key_type = Type::getInt();
} else {
$inferred_key_type = Type::combineUnionTypes($inferred_key_type, Type::getInt());
}
$stmt->inferredType = Type::getString();
} elseif ($type->isNull()) {
if (IssueBuffer::accepts(
@ -961,8 +975,7 @@ class FetchChecker
)) {
if (isset($stmt->inferredType)) {
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull());
}
else {
} else {
$stmt->inferredType = Type::getNull();
}
continue;
@ -1013,7 +1026,7 @@ class FetchChecker
if ($stmt->dim) {
if (isset($stmt->dim->inferredType) && $key_type && !$key_type->isEmpty()) {
foreach ($stmt->dim->inferredType->types as $at) {
if (($at->isMixed() || $at->isEmpty()) && !$key_type->isMixed()) {
if (($at->isMixed() || $at->isEmpty()) && !$inferred_key_type->isMixed()) {
if (IssueBuffer::accepts(
new MixedArrayOffset(
'Cannot access value on variable ' . $var_id . ' using mixed offset - expecting ' .

View File

@ -116,4 +116,38 @@ class ArrayAccessTest extends PHPUnit_Framework_TestCase
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MixedArrayAccess
*/
public function testMixedArrayAccess()
{
$context = new Context('somefile.php');
$stmts = self::$parser->parse('<?php
/** @var mixed */
$a = [];
echo $a[0];
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check(true, true, $context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MixedArrayOffset
*/
public function testMixedArrayOffset()
{
$context = new Context('somefile.php');
$stmts = self::$parser->parse('<?php
/** @var mixed */
$a = 5;
echo [1, 2, 3, 4][$a];
');
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
$file_checker->check(true, true, $context);
}
}