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

Initialization of private trait members is not detected by psalm #3897

Closed
danog opened this issue Jul 26, 2020 · 6 comments
Closed

Initialization of private trait members is not detected by psalm #3897

danog opened this issue Jul 26, 2020 · 6 comments
Labels

Comments

@danog
Copy link
Collaborator

danog commented Jul 26, 2020

Psalm cannot detect initialization of typed private trait properties, even when they are in fact indirectly initialized in the class constructor.

https://psalm.dev/r/d316318ce2

@weirdan
Copy link
Collaborator

weirdan commented Jul 26, 2020

@psalm-github-bot, resolve this: https://psalm.dev/r/d316318ce2

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d316318ce2
<?php

trait a
{
    private int $prop;
    public function m(int $prop): void {
        $this->prop = $prop;
    }
}

class b
{
    use a;
    public function __construct()
    {
        $this->m(1);
    }
}
Psalm output (using commit 1d077cc):

ERROR: PropertyNotSetInConstructor - 11:7 - Property b::$prop is not defined in constructor of b and in any private or final methods called in the constructor

@weirdan
Copy link
Collaborator

weirdan commented Jul 26, 2020

Just like with normal setters they need to be defined final: https://psalm.dev/r/18680465d4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/18680465d4
<?php

trait a
{
    private int $prop;
    public final function m(int $prop): void {
        $this->prop = $prop;
    }
}

class b
{
    use a;
    public function __construct()
    {
        $this->m(1);
    }
}
Psalm output (using commit 1d077cc):

No issues!

@weirdan weirdan closed this as completed Jul 26, 2020
@danog
Copy link
Collaborator Author

danog commented Jul 26, 2020

The same issue is present if the method is aliased: https://psalm.dev/r/d8caf723b2

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d8caf723b2
<?php

trait a
{
    private int $prop;
    public final function m(int $prop): void {
        $this->prop = $prop;
    }
}

class b
{
    use a {
        m as final pony;
    }
    public function __construct()
    {
        $this->pony(1);
    }
}
Psalm output (using commit eddd7b8):

ERROR: PropertyNotSetInConstructor - 11:7 - Property b::$prop is not defined in constructor of b and in any private or final methods called in the constructor

@weirdan weirdan reopened this Jul 26, 2020
@weirdan weirdan added the bug label Jul 26, 2020
@muglug muglug closed this as completed in 23f5d66 Jul 26, 2020
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