1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Report RawObjectIteration when array_walk is used on objects

This commit is contained in:
Bruce Weirdan 2021-08-20 01:43:49 +03:00
parent 6cff8b627f
commit b370ce92aa
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 33 additions and 0 deletions

View File

@ -403,6 +403,32 @@ class NamedFunctionCallHandler
}
}
}
if ($first_arg && $function_id === 'array_walk') {
$first_arg_type = $statements_analyzer->node_data->getType($first_arg->value);
if ($first_arg_type && $first_arg_type->hasObjectType()) {
if ($first_arg_type->isSingle()) {
if (IssueBuffer::accepts(
new \Psalm\Issue\RawObjectIteration(
'Possibly undesired iteration over object properties',
new CodeLocation($statements_analyzer, $function_name)
)
)) {
// fall through
}
} else {
if (IssueBuffer::accepts(
new \Psalm\Issue\PossibleRawObjectIteration(
'Possibly undesired iteration over object properties',
new CodeLocation($statements_analyzer, $function_name)
)
)) {
// fall through
}
}
}
}
}
private static function handleDependentTypeFunction(

View File

@ -2170,6 +2170,13 @@ class ArrayFunctionCallTest extends TestCase
}',
'error_message' => 'LessSpecificReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:32 - The type \'array{A: int|string}<string, string>\' is more general',
],
'arrayWalkOverObject' => [
'<?php
$o = new stdClass();
array_walk($o, "var_dump");
',
'error_message' => 'RawObjectIteration',
],
];
}
}