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

Invalid error when using $this in a conditional return #11220

Open
ADmad opened this issue Jan 30, 2025 · 1 comment
Open

Invalid error when using $this in a conditional return #11220

ADmad opened this issue Jan 30, 2025 · 1 comment

Comments

@ADmad
Copy link
Contributor

ADmad commented Jan 30, 2025

https://psalm.dev/r/c389a23983

class Foo
{
    protected string $var = '';
    
    /**
     * @return ($var is null ? string : $this)
     */
    public function bar(?string $var = null): static|string
    {
        if ($var === null) {
            return $this->var;
        }
        
        $this->var = $var;
        
        return $this;
    }
    
    /**
     * @return $this
     */
    public function baz(): static
    {
        return $this;
    }
}
ERROR: [InvalidDocblock](https://psalm.dev/008) - 9:5 - Unexpected space in docblock for Foo::bar
Copy link

I found these snippets:

https://psalm.dev/r/c389a23983
<?php
class Foo
{
    protected string $var = '';
    
    /**
     * @return ($var is null ? string : $this)
     */
    public function bar(?string $var = null): static|string
    {
        if ($var === null) {
            return $this->var;
        }
        
        $this->var = $var;
        
        return $this;
    }
    
    /**
     * @return $this
     */
    public function baz(): static
    {
        return $this;
    }
}
Psalm output (using commit c7c5a69):

ERROR: InvalidDocblock - 9:5 - Unexpected space in docblock for Foo::bar

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

No branches or pull requests

2 participants