Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
windaishi opened this issue Sep 9, 2024 · 4 comments
Open

Comments

@windaishi
Copy link

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
@ea-inspections-team
Copy link
Collaborator

Thank you for reporting @windaishi!

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

ea-inspections-team added a commit that referenced this issue Sep 19, 2024
@ea-inspections-team
Copy link
Collaborator

ea-inspections-team commented Sep 19, 2024

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.

@kalessil
Copy link
Owner

kalessil commented Sep 20, 2024

@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.

@ea-inspections-team
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants