kalessil / phpinspectionsea

A Static Code Analyzer for PHP (a PhpStorm/Idea Plugin)
https://plugins.jetbrains.com/plugin/7622?pr=phpStorm
Other
1.44k stars 118 forks source link

Unsupported string offset operations false-positive on array assignment #824

Closed fredricj closed 6 years ago

fredricj commented 6 years ago

The following (simplified) code triggers "Unsupported string offset operations: Provokes a PHP Fatal error (cannot use string offset as an array)":

$row = [1,2,3];
$plates = [];
foreach ($row as $t) {
    $header = "t";
    $plates[$header][] = $t; // Gives "Cannot use string offset as an array"
}
kalessil commented 6 years ago

Thank you for reporting @fredricj. For some reason, I can not reproduce. Would you please check this code fragment in your environment?

function local_scope_only() {
    $row = [1,2,3];
    $plates = [];
    foreach ($row as $t) {
        $header = "t";
        $plates[$header][] = $t; // Gives "Cannot use string offset as an array"
    }
}
fredricj commented 6 years ago

Wrapping it in a function does not trigger the message but removing the function scope from your code fragment emits the message again

kalessil commented 6 years ago

Got it: I'll make adjustments. The inspection will skip global context and stick to functions and methods only.

kalessil commented 6 years ago

Fixed