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

Unexpected PropertyNotSetInConstructor when using trait with properties #5062

Open
zmitic opened this issue Jan 20, 2021 · 1 comment
Open
Labels

Comments

@zmitic
Copy link

zmitic commented Jan 20, 2021

https://psalm.dev/r/c4eeed072f

2 cases; one that has a call to parent constructor, one that doesn't override constructor at all. Psalm doesn't recognize set property, attempts with final public also failed.

Explanation:

ID trait is used by all entities and must be initialized so I can't suppress it.

I fixed the above inheritance problem by copying the code but surely would prefer to use trait.

@psalm-github-bot
Copy link

I found these snippets:

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

trait IdTrait
{
    private int $id;
    
    private function initializeId(): void
    {
        $this->id = 5;
    }
}

class BaseUser
{
    use IdTrait;
    
    public function __construct()
    {
        $this->initializeId();
    }
}

class Admin extends BaseUser
{    
}

class Client extends BaseUser
{
    public function __construct()
    {
        parent::__construct();
    }
}
Psalm output (using commit 9e05254):

ERROR: PropertyNotSetInConstructor - 23:7 - Property Admin::$id is not defined in constructor of Admin and in any private or final methods called in the constructor

ERROR: PropertyNotSetInConstructor - 27:7 - Property Client::$id is not defined in constructor of Client and in any private or final methods called in the constructor

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