Phpinspectionsea: False Positive: Class property initialization flaws (PHP 7.4 nullable properties)

Created on 5 Aug 2019  路  10Comments  路  Source: kalessil/phpinspectionsea


| Subject | Details |
| :------------- | :---------------------------------------------------------------------------- |
| Plugin | Php Inspections (EA Ultimate) 3.0.2 |
| Language level | PHP 7.4beta |

Current behaviour

Given the PHP7.4 code:

<?php
declare(strict_types=1);

class Dummy {
    private ?string $name=null;

    public function getName() : ?string {
        return $this->name;
    }
}
$dummy=new Dummy();
print $dummy->getName() ?? '[no-name]';

Running the code will output "[no-name]" as expected.

However, if I follow PHP Inspections Class property initalization flaws inspection where 'Null assignment can be safely removed.Define null in annotations if it is importantand remove the=null, then run the code, PHP errors withPHP Fatal error: Uncaught Error: Typed property Dummy::$name must not be accessed before initialization`.

Removing that =null and then adding a constructor to initialize the property results in PHPInspections suggesting Written property is same as default one, consider removing this assignment (under Class property initialization flaws)

Expected behaviour

PHPInspections should not suggest removing =null on nullable properties.

(It should class undefined entries as not the same as null).

Environment details

PhpStorm 2019.2
Build #PS-192.5728.108, built on July 24, 2019
Runtime version: 11.0.3+12-b304.10 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 972M
Cores: 8
Registry: run.processes.with.pty=TRUE
Non-Bundled Plugins: GrepConsole, Key Promoter X, PHPUnit code coverage, String Manipulation, com.intellij.ideolog, BashSupport, com.kalessil.phpStorm.phpInspectionsUltimate, de.espend.idea.php.phpunit, de.espend.idea.php.toolbox, de.espend.idea.php.annotation, izhangzhihao.rainbow.brackets, org.psliwa.idea.composer, ru.adelf.idea.dotenv

bug / false-positive fixed

Most helpful comment

I've got version 3.0.16.1 and PHP language level set to 7.4 and the warning is still there. Am I the only one?

All 10 comments

PHPInspections should not suggest removing =null on nullable properties.

Only if php version of the project is >= 7.4

Fixed!

I've got version 3.0.16.1 and PHP language level set to 7.4 and the warning is still there. Am I the only one?

I was just about to create a similar issue.

Triggered by:

class a{
    public ?a $a;
    public function __construct(){
        $this->a = null;
    }
}

If I remove the initialization, PHP will throw an error later if I access $a later.

@kalessil Do you want us to create a new Issue for this?

EDIT: this is kinda weird, I can't always reproduce the issue with the code below. Sometime it highlights the line, sometimes not.

@funivan @kalessil
Using version 4.0.2, project is set to php >=7.4 in both setings and composer.json and I still get reported [EA] Null asignment can be safely removed. Define null in annotations if it's important. for property private ?int $foo = null;. It cannot be safely removed as accessing that variable without explicit call $this->foo = null; would cause Uncaught Error: Typed property Example::$foo must not be accessed before initialization
Here's the proof http://sandbox.onlinephpfunctions.com/code/74802083345cf08973dd9e266895f19db43e8e9d

Edit: found another duplicate #1455

@kalessil Same issue here. Could you re-open the issue?

Fixed in #1455 !

Great!

With PHPStorm 2020.1 (201.6668.153), Php Inspections (EA Extended) 4.0.3, PHP language level set to 7.4 the code

class A {
    private ?string $foo;
    private ?A $bar;
    private ?DateTime $baz;

    public function __construct()
    {
        $this->foo = null;
        $this->bar = null;
        $this->baz = null;
    }
}

still yields "[EA] Written value is same as default one, consider removing this assignment." for all 3 properies.

@MisatoTremor: please file a new issue, so I could track it properly. Thank you in advance.

Was this page helpful?
0 / 5 - 0 ratings