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

Psalm emits InaccessibleProperty and RedundantPropertyInitializationCheck for checks and mutating code in the "__wakeup" #10182

Open
andrew-demb opened this issue Sep 5, 2023 Discussed in #10180 · 1 comment

Comments

@andrew-demb
Copy link
Contributor

Discussed in #10180

Originally posted by andrew-demb September 5, 2023
https://psalm.dev/r/0b9d93cacf

<?php

/**
 * @psalm-immutable
 */
class Some
{
    // it was a `?string $locale` in past
    private string $locale;
    
    public function __construct(
        string $locale,
    ) {
        $this->locale = $locale;
    }

    public function __wakeup(): void
    {
    	// it was a `?string $locale` in past
        if (false === isset($this->locale)) {
            $this->locale = '';
        }
    }
}
Psalm output (using commit [37cc4fd](https://github.com/vimeo/psalm/commit/37cc4fd)): 

ERROR: [RedundantPropertyInitializationCheck](https://psalm.dev/261) - 20:13 - Property $this->locale with type string should already be set in the constructor

ERROR: [InaccessibleProperty](https://psalm.dev/054) - 21:13 - Some::$locale is marked readonly

What's the proper way to handle it?
Suppress both issues or psalm may allow such a checks and updates for magic method like a __wakeup?

As of #10180 (comment) - the current issue requests for a feature in Psalm - don't emit such errors for code like above in the __wakeup.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/0b9d93cacf
<?php

/**
 * @psalm-immutable
 */
class Some
{
    // it was a `?string $locale` in past
    private string $locale;
    
    public function __construct(
        string $locale,
    ) {
        $this->locale = $locale;
    }

    public function __wakeup(): void
    {
    	// it was a `?string $locale` in past
        if (false === isset($this->locale)) {
            $this->locale = '';
        }
    }
}
Psalm output (using commit 37cc4fd):

ERROR: RedundantPropertyInitializationCheck - 20:13 - Property $this->locale with type string should already be set in the constructor

ERROR: InaccessibleProperty - 21:13 - Some::$locale is marked readonly

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