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

RedundantPropertyInitializationCheck should never occur inside constructor #11124

Open
jnvsor opened this issue Oct 13, 2024 · 1 comment
Open

Comments

@jnvsor
Copy link
Contributor

jnvsor commented Oct 13, 2024

https://psalm.dev/r/b392c46642

Property [...] should already be set in the constructor

This error should never occur inside the constructor, but it does when checking if the value is set with isset or ??=

Copy link

I found these snippets:

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

class X {
    private string $x;
    
    /** @param string[] $a */
    public function __construct(array $a)
    {
        foreach ($a as $key => $val) {
            if ($key === 'x') {
                $this->x = $val;
            }
        }
        
        $this->x ??= '';
    }
}

class Y {
    private string $y;
    
    /** @param string[] $a */
    public function __construct(array $a)
    {
        $this->y ??= '';
    }
}

class Z {
    private string $z;
    
    /** @param string[] $a */
    public function __construct(array $a)
    {
        if (!isset($this->z)) {
	        $this->z = '';
        }
    }
}
Psalm output (using commit 03ee02c):

ERROR: RedundantPropertyInitializationCheck - 15:22 - Property $this->x with type string should already be set in the constructor

ERROR: RedundantPropertyInitializationCheck - 15:9 - Property $this->x with type string should already be set in the constructor

ERROR: RedundantPropertyInitializationCheck - 25:22 - Property $this->y with type string should already be set in the constructor

ERROR: RedundantPropertyInitializationCheck - 25:9 - Property $this->y with type string should already be set in the constructor

ERROR: RedundantPropertyInitializationCheck - 35:14 - Property $this->z with type string should already be set in the constructor

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