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

Incorrect RedundantCondition with null safe property access and null coalesce #10316

Open
tux-rampage opened this issue Oct 25, 2023 · 3 comments

Comments

@tux-rampage
Copy link
Contributor

When accessing properties/methods with the null safe operator, the result seems to be evaluated incorrectly.
The resulting type should be <Type>|null, but psalm evaluates only <Type>.

https://psalm.dev/r/14b17f9fa4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/14b17f9fa4
<?php

interface A {
    public function a(): string;
    public function copy(): self;
}

class B {
    public function __construct(public A|null $a = null) {}
    
    public function something(): string
    {
        $value = $this->a?->copy()->a() ?? '';
        return $value;
    }

    public function trace(): string
    {
        /** @psalm-trace $value */
        $value = $this->a?->copy()->a();
        return $value;
    }

}
Psalm output (using commit 147505c):

ERROR: RedundantCondition - 13:18 - Type string for $<tmp coalesce var>229 is never null

ERROR: TypeDoesNotContainNull - 13:44 - Cannot resolve types for $<tmp coalesce var>229 - string does not contain null

INFO: Trace - 20:9 - $value: string

@aleho
Copy link

aleho commented Dec 4, 2023

Maybe this is related (seen in Symfony):

https://psalm.dev/r/46409b9f5a

Copy link

I found these snippets:

https://psalm.dev/r/46409b9f5a
<?php

declare(strict_types = 1);

class Request {}
interface TokenInterface {}

class AuthenticationException {
    public function getPrevious(): ?\AuthenticationException
    {
        return null;
    }
    
    public function getToken(): ?\TokenInterface
    {
        return null;
    }
}

class Response {
    public function __construct(?AuthenticationException $e) {}
}

interface AuthenticationEntryPointInterface
{
    public function start(Request $request, AuthenticationException $authException = null): Response;
}

readonly class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
    public function start(Request $request, AuthenticationException $authException = null): Response
    {
        if (
            ($prev = $authException?->getPrevious())
            && $authException?->getToken()
        ) {
           return new Response($prev);
        }

        return new Response(null);
    }
}
Psalm output (using commit 0e43c44):

ERROR: TypeDoesNotContainNull - 35:16 - AuthenticationException does not contain null

ERROR: RedundantCondition - 35:16 - Type AuthenticationException for $authException is never null

ERROR: RedundantCondition - 34:13 - Type AuthenticationException for $authException is never null

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

2 participants