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

False positive null pointer exception #1946

Open Simbiat opened 4 months ago

Simbiat commented 4 months ago
Subject Details
Plugin Php Inspections (EA Extended)
Language level e.g. PHP 8.3

Current behaviour

Have a DOMNode element and code like this

$node?->parentNode->removeChild($node)

Get null pointer exception warning image

Expected behaviour

No warning, since there is is a null safe operator ?-> used for the same object.

Environment details

PhpStorm 2024.1.1
Build #PS-241.15989.102, built on April 23, 2024
Licensed to simbiat.ru / Dmitry Kustov
Subscription is active until March 31, 2025.
For non-commercial open source development only.
Runtime version: 17.0.10+1-b1207.14 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
GC: G1 Young Generation, G1 Old Generation
Memory: 5120M
Cores: 16
Registry:
  debugger.new.tool.window.layout=true
  run.processes.with.pty=TRUE
  ide.experimental.ui=true
  terminal.new.ui=true
Non-Bundled Plugins:
  com.intellij.ml.llm (241.15989.156)
  com.kalessil.phpStorm.phpInspectionsEA (5.0.0.0)
  org.intellij.qodana (241.15989.121)
jtheuerkauf commented 3 months ago

Similar example in a match statement:

function foo (?SomeEnum $bar) {
    match ($bar) {
        null => 'invalid',
        SomeEnum::SomeCase => $bar->value,
    };
}

Accessing $bar->value gets an inspection flag. However, the null case is evaluated first, making it impossible for $bar to be null in the second case.

image

PhpStorm 2024.2 EAP
Build #PS-242.10180.24, built on May 14, 2024
Runtime version: 21.0.3+13-b446.1 amd64 (JCEF 122.1.9)
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
GC: G1 Young Generation, G1 Concurrent GC, G1 Old Generation
Memory: 4096M
Cores: 12
Registry:
  debugger.new.tool.window.layout=true
  run.processes.with.pty=TRUE
  ide.experimental.ui=true
janhanacek1 commented 2 months ago

I have similiar problem in ternary operator usage...

image

PhpStorm 2024.1.4 Build #PS-241.18034.69, built on June 21, 2024 Runtime version: 17.0.11+1-b1207.24 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.

chriscpty commented 1 month ago

i think this can be generalised as a false positive whenever the check for null is done indirectly (so not something along the lines of $foo !== null or empty($foo)). If the null/empty check instead checks for one of $foos attributes (through ?-> or similar), the inspection doesn't pick up on it.

I think @jtheuerkauf 's match error is a separate issue?