phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

False positives when using the null-safe operator `?->` in an if-condition

Open windaishi opened this issue 1 year ago • 6 comments

Subject Details
Plugin Php Inspections (EA Extended) 5.0.0.0
Language level e.g. PHP 8.2 (check project settings or composer.json)

Current behaviour

The following code is reported with the following two issues:

Line 5: [EA] Null pointer exception may occur here. (on $shippingMethodConfig) Line 6: [EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions. (on `else)

/** @var ShippingMethodConfigEntity|null $shippingMethodConfig */
$shippingMethodConfig;

if ($shippingMethodConfig?->getAddressConfiguration()->useDefaultAddress() === false) {
    $senderAddress = $shippingMethodConfig->getAddressConfiguration()->getAddress();
} else {
    $senderAddress = $commonConfig->getSenderAddress();
}

Expected behaviour

No errors are reported. Neither is the if driven by an inverted condition (because we are comparing against bool?) nor can shippingMethodConfig be null here.

Environment details

image

windaishi avatar Sep 09 '24 09:09 windaishi

Thank you for reporting @windaishi!

We have multiple reports for this case and will include a fix into next release.

ea-inspections-team avatar Sep 18 '24 15:09 ea-inspections-team

We are pushing fix for Line 6: [EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions. (on else)` false-positive, which greatly improves accuracy if the inspection. Thank you for reporting it @windaishi!

We are unsure how to proceed with Line 5: [EA] Null pointer exception may occur here. (on $shippingMethodConfig), @kalessil could you please share your thoughts about the false-positive.

ea-inspections-team avatar Sep 19 '24 19:09 ea-inspections-team

@ea-inspections-team: agree with @windaishi, it's a false-positive.

From a types-resolving perspective, the code is correct (assignment in if-body will not inject null into the result type). I'm in support of fixing this false positive.

kalessil avatar Sep 20 '24 07:09 kalessil

@kalessil: thank you. Fixing this is not a trivial task and we have to plan the fix for 2024.3 (not 2024.2).

ea-inspections-team avatar Sep 25 '24 15:09 ea-inspections-team

Image

Php Inspections (EA Extended) 5.3.0

PhpStorm 2025.2 Build #PS-252.23892.419, built on August 1, 2025 Source revision: 42cc6c7831493

leadorub avatar Aug 07 '25 05:08 leadorub

Yup, still occurs. If someone wants to disable this specific inspection: Php Inspections (EA Extended) → Probable bugs → Null reference

probably only makes sense though if something else also checks this, e.g. PhpStan.

ghbob avatar Sep 28 '25 16:09 ghbob