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

PHP 8 - False positive possibly null with constructor populated properties #4615

Closed
tux-rampage opened this issue Nov 19, 2020 · 1 comment
Closed
Labels

Comments

@tux-rampage
Copy link
Contributor

It might be an edge case, but the following snippet incorrectly yields an error:

https://psalm.dev/r/4f6b90fe88

Accessing the populated property in the constructor is perfectly valid:

https://3v4l.org/AIDLL

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/4f6b90fe88
<?php

class Foo {}

class Bar {
	public function __construct(Foo $foo) {}
}

class Baz
{
    private Bar $bar;
    
    public function __construct(private Foo $foo, ?Bar $bar = null)
    {
        $this->bar = $bar ?? new Bar($this->foo);
    }
}

new Baz(new Foo());
Psalm output (using commit 95de6cf):

ERROR: UninitializedProperty - 15:38 - Cannot use uninitialized property $this->foo

ERROR: PossiblyNullArgument - 15:38 - Argument 1 of Bar::__construct cannot be null, possibly null value provided

@weirdan weirdan added the bug label Nov 19, 2020
@muglug muglug closed this as completed in 1fc1cae Nov 22, 2020
danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants