1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Also add better message for MixedArgumentTypeCoercion

This commit is contained in:
Matt Brown 2021-03-28 11:32:38 -04:00
parent 54ac13b190
commit 93743d1465
3 changed files with 36 additions and 2 deletions

View File

@ -934,12 +934,30 @@ class ArgumentAnalyzer
if ($union_comparison_results->type_coerced && !$input_type->hasMixed()) {
if ($union_comparison_results->type_coerced_from_mixed) {
$origin_locations = [];
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($input_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}
$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;
if ($origin_location && $origin_location->getHash() === $arg_location->getHash()) {
$origin_location = null;
}
if (IssueBuffer::accepts(
new MixedArgumentTypeCoercion(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
', parent type ' . $input_type->getId() . ' provided',
$arg_location,
$cased_method_id
$cased_method_id,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
)) {

View File

@ -41,7 +41,7 @@ class ArrayPointerAdjustmentReturnTypeProvider implements \Psalm\Plugin\EventHan
$value_type = null;
$definitely_has_items = false;
while ($atomic_type = array_shift($atomic_types)) {
while ($atomic_type = \array_shift($atomic_types)) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$atomic_types = \array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
continue;

View File

@ -3298,6 +3298,22 @@ class UnusedVariableTest extends TestCase
}',
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:42 - Unable to determine the type that $key is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:3:34'
],
'warnAboutMixedArgumentTypeCoercionSource' => [
'<?php
/** @param array<string> $arr */
function takesArrayOfString(array $arr) : void {
foreach ($arr as $a) {
echo $a;
}
}
/** @param mixed $a */
function takesArray($a) : void {
$arr = [$a];
takesArrayOfString($arr);
}',
'error_message' => 'MixedArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:44 - Argument 1 of takesArrayOfString expects array<array-key, string>, parent type array{mixed} provided. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:10:41'
],
];
}
}