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

Problem with constructor helper in DTO object marked as psalm-immutable. #8711

Open
mj4444ru opened this issue Nov 15, 2022 · 1 comment
Open

Comments

@mj4444ru
Copy link

I'm using a trait in a DTO object marked as psalm-immutable.
With the help of the trait, I initialize the properties of the object, but calling the initializing property's mathod, I get an error:
psalm: UnusedMethodCall: The call to BaseData::importMetaProp is not used.
I also get the PropertyNotSetInConstructor error.

I would like to be able to mark a method as a "constructor". Such methods will not raise an UnusedMethodCall error, and these methods can only be called from a constructor or an object marked as "constructor".

https://psalm.dev/r/6a1fd47226

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6a1fd47226
<?php

/**
 * @psalm-immutable
 */
trait DTOTrait
{
    protected function importProps(object $data, array $meta): void
    {
        /** @var string|array|null $propName */
        foreach ($meta as $propName) {
            if (is_string($propName)) {
                $this->$propName = $data->$propName;
            } else {
                throw new Exception('Invalid $meta argument format.');
            }
        }
    }
}
    
/**
 * @psalm-immutable
 * @psalm-suppress PropertyNotSetInConstructor
 */
class BaseData
{
    use DTOTrait;

    public string $prop1;
    public string $prop1;

    public function __construct(object $data)
    {
        $this->importProps($data, [
            'prop1',
            'prop2',
        ]);
    }
}
Psalm output (using commit 2a29fd7):

ERROR: UnusedMethodCall - 34:16 - The call to BaseData::importProps is not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant