kalessil / phpinspectionsea

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

[FP] "Null pointer exception may occur here" in collection`s ->isNotEmpty() condition #1897

Open PetrKaterinak opened 1 year ago

PetrKaterinak commented 1 year ago
Subject Details
Plugin Php Inspections (EA Extended) 5.0.0.0
Language level PHP 8.2

Current behaviour

if ($employee->salaries->isNotEmpty()) {
    //                                                        | false positive here
    $employeeData['hourSalary'] = $employee->salaries->first()->getHourCost();
}

As salaries are not empty, it always must have ->first() item, so null pointer exception never happens

Environment details

PHP Storm 2023.1.3

jdreesen commented 1 year ago

How should this bundle make the connection between the two calls? Not sure if this is possible (without help from the library itself: does the code have according Psalm/PHPStan annotations?).

Apart from that, I think we can only rely on the type information from PhpStorm here, so this should probably be fixed in PhpStorm.