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:
parent
a44dd4f6b0
commit
dcec735d11
@ -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 ' .
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user