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

Fix #964 - warn about imposible offsets

This commit is contained in:
Matthew Brown 2018-08-31 20:02:36 -04:00
parent 3e69a333c9
commit d5b0d5a996
2 changed files with 21 additions and 3 deletions

View File

@ -380,10 +380,12 @@ class ArrayFetchChecker
$type->type_params[0] = $offset_type;
}
} elseif (!$type->type_params[0]->isEmpty()) {
if (!TypeChecker::isContainedBy(
if ((!TypeChecker::isContainedBy(
$project_checker->codebase,
$offset_type,
$type->type_params[0],
$type->type_params[0]->isMixed()
? new Type\Union([ new TInt, new TString ])
: $type->type_params[0],
true,
$offset_type->ignore_falsable_issues,
$has_scalar_match,
@ -391,7 +393,8 @@ class ArrayFetchChecker
$type_coerced_from_mixed,
$to_string_cast,
$type_coerced_from_scalar
) && !$type_coerced_from_scalar
) && !$type_coerced_from_scalar)
|| $to_string_cast
) {
$expected_offset_types[] = $type->type_params[0]->getId();
} else {

View File

@ -149,6 +149,14 @@ class ArrayAccessTest extends TestCase
'assertions' => [],
'error_levels' => ['MixedArgument'],
],
'mixedKeyMixedOffset' => [
'<?php
function example(array $x, $y) : void {
echo $x[$y];
}',
'assertions' => [],
'error_levels' => ['MixedArgument', 'MixedArrayOffset', 'MissingParamType'],
],
];
}
@ -250,6 +258,13 @@ class ArrayAccessTest extends TestCase
}',
'error_message' => 'InvalidArrayOffset',
],
'mixedKeyStdClassOffset' => [
'<?php
function example(array $y) : void {
echo $y[new stdClass()];
}',
'error_message' => 'InvalidArrayOffset',
],
];
}
}