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 loses the type of typed static properties #11180

Open
MauricioFauth opened this issue Dec 9, 2024 · 7 comments
Open

Psalm loses the type of typed static properties #11180

MauricioFauth opened this issue Dec 9, 2024 · 7 comments

Comments

@MauricioFauth
Copy link
Contributor

Psalm does not know the type of a typed static property of another class. It assumes it's mixed when it's not.

https://psalm.dev/r/588d62ba8f

Maybe related to #7173.

Copy link

I found these snippets:

https://psalm.dev/r/588d62ba8f
<?php declare(strict_types=1);

final class StaticClass
{
    public static string $property = '';
    
    public function testMethod(): void {
        if (StaticClass::$property === '') {
            return;
        }
        /** @psalm-trace StaticClass::$property */
    }
}

final class TestClass
{
    public function testMethod(): void {
        if (StaticClass::$property === '') {
            return;
        }
        /** @psalm-trace StaticClass::$property */
    }
}

function test_function(): void {
    if (StaticClass::$property === '') {
        return;
    }
    /** @psalm-trace StaticClass::$property */
}
Psalm output (using commit 79ab7e2):

INFO: Trace - 29:0 - StaticClass::$property: mixed

INFO: Trace - 11:0 - StaticClass::$property: non-empty-string

INFO: Trace - 21:0 - StaticClass::$property: mixed

@MoonE
Copy link
Contributor

MoonE commented Dec 9, 2024

If I understand the explanation in #7173 correctly, psalm-trace only works with variables in the current scope.

So the type is known if you assign it to a variable.
https://psalm.dev/r/e195b49aa9

Do you experience an issue not involving psalm-trace?

Copy link

I found these snippets:

https://psalm.dev/r/e195b49aa9
<?php declare(strict_types=1);

final class StaticClass
{
    public static string $property = '';
    
    public function testMethod(): void {
        $x = StaticClass::$property;
        if ($x === '') {
            return;
        }
        /** @psalm-trace $x */
    }
}

final class TestClass
{
    public function testMethod(): void {
        $y = StaticClass::$property;
        if ($y === '') {
            return;
        }
        /** @psalm-trace $y*/
    }
}

function test_function(): void {
    $z = StaticClass::$property;
    if ($z === '') {
        return;
    }
    /** @psalm-trace $z */
}
Psalm output (using commit 79ab7e2):

INFO: Trace - 32:0 - $z: non-empty-string

INFO: Trace - 12:0 - $x: non-empty-string

INFO: Trace - 23:0 - $y: non-empty-string

@MoonE
Copy link
Contributor

MoonE commented Dec 9, 2024

This does indeed not work.
https://psalm.dev/r/9e05ac200d

Copy link

I found these snippets:

https://psalm.dev/r/9e05ac200d
<?php declare(strict_types=1);

final class StaticClass
{
    public static string $property = '';
    
    public function testMethod(): void {
        if (StaticClass::$property === '') {
            return;
        }
        $_x = StaticClass::$property;
        /** @psalm-trace $_x */
    }
}

final class TestClass
{
    public function testMethod(): void {
        if (StaticClass::$property === '') {
            return;
        }
        $_y = StaticClass::$property;
        /** @psalm-trace $_y*/
    }
}

function test_function(): void {
    if (StaticClass::$property === '') {
        return;
    }
    $_z = StaticClass::$property;
    /** @psalm-trace $_z */
}
Psalm output (using commit 79ab7e2):

INFO: Trace - 32:0 - $_z: mixed

INFO: Trace - 12:0 - $_x: non-empty-string

INFO: Trace - 23:0 - $_y: mixed

@MauricioFauth
Copy link
Contributor Author

https://psalm.dev/r/79e5a054b0

And also that a look at the Psalm baseline here: phpmyadmin/phpmyadmin#19440

Copy link

I found these snippets:

https://psalm.dev/r/79e5a054b0
<?php declare(strict_types=1);

final class StaticClass
{
    public static string $property = '';
}

final class TestClass
{
    public function foo(): void
    {
        if (StaticClass::$property === '') {
            return;
        }

        $this->bar(StaticClass::$property);
    }

    private function bar(string $value): void
    {
        echo $value;
    }
}
Psalm output (using commit 79ab7e2):

INFO: MixedArgument - 16:20 - Argument 1 of TestClass::bar cannot be mixed, expecting string

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