Phpinspectionsea: [FP] "Null pointer exception may occur here" after && check

Created on 11 Dec 2018  路  2Comments  路  Source: kalessil/phpinspectionsea

| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Issue type | Bug/False-Positive |
| Plugin | Php Inspections (EA Extended) v3.0.9.1 |
| Language level | PHP 7.2 or higher |

Current behaviour (description/screenshot:)

<?php

class A
{
    /** @var int */
    private $id = 100;

    public function getId(): int
    {
        return $this->id;
    }
}

class B
{
    /** @var A */
    private $a;

    public function getA(): ?A
    {
        return $this->a;
    }

    public function setA(A $a): void
    {
        $this->a = $a;
    }
}

class TestNpe
{
    public function test(B $b)
    {
        //                             FP NPE here
        return $b->getA() && $b->getA()->getId();
    }
}

Expected behaviour

There shouldn't be NPE warning because if the first part of condition $b->getA() will be false/null, the second $b->getA()->getId() will not be executed

Environment details

PhpStorm 2018.3.1
Build #PS-183.4588.67, built on December 5, 2018
JRE: 1.8.0_152-release-1343-b16 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

bug / false-positive fixed

Most helpful comment

Fixed!

All 2 comments

Thank you for reporting @zakirovir6, not sure yet how I going to fix it yet, but let's hope it'll be simple enough =)

Fixed!

Was this page helpful?
0 / 5 - 0 ratings