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 doesn't work with const array when key is enum value #9644

Closed
AndreyThompson opened this issue Apr 12, 2023 · 3 comments
Closed

Psalm doesn't work with const array when key is enum value #9644

AndreyThompson opened this issue Apr 12, 2023 · 3 comments
Labels

Comments

@AndreyThompson
Copy link

I have const array in class and I use enum's value as key.
Psalm is going crazy with "Constant TranscodingCreateRequestFactory::MAP is not defined"
https://psalm.dev/r/ed2358aa53

But if change array key to simple string - everything works
https://psalm.dev/r/992a7c1602

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ed2358aa53
<?php
enum TranscodeTypeEnum: string
{
    case Case1 = 'case 1';
}

class myClass {}

class TranscodingCreateRequestFactory
{
    private const MAP = [
        TranscodeTypeEnum::Case1->value => myClass::class,
    ];

    public function createObjectByEnumValue(string $value) : object
    {
        if (isset(self::MAP[$value])) {
            $classname = self::MAP[$value];

            return new $classname();
        }

        throw new RuntimeException('Unknown class for ' . $value);
    }
}
Psalm output (using commit 71bb951):

ERROR: UndefinedConstant - 17:19 - Constant TranscodingCreateRequestFactory::MAP is not defined

ERROR: UndefinedConstant - 18:26 - Constant TranscodingCreateRequestFactory::MAP is not defined

INFO: MixedAssignment - 18:13 - Unable to determine the type that $classname is being assigned to

INFO: MixedMethodCall - 20:20 - Cannot call constructor on an unknown class
https://psalm.dev/r/992a7c1602
<?php
enum TranscodeTypeEnum: string
{
    case Case1 = 'case 1';
}

class myClass {}

class TranscodingCreateRequestFactory
{
    private const MAP = [
        'case 1' => myClass::class,
    ];

    public function createObjectByEnumValue(string $value) : object
    {
        if (isset(self::MAP[$value])) {
            $classname = self::MAP[$value];

            return new $classname();
        }

        throw new RuntimeException('Unknown class for ' . $value);
    }
}
Psalm output (using commit 71bb951):

No issues!

@orklah
Copy link
Collaborator

orklah commented Apr 12, 2023

Indeed, Psalm seems to have failed silently here

@orklah orklah added the bug label Apr 12, 2023
@ygottschalk
Copy link
Contributor

Issue is tracked here: #9373
PR with fix is here: #9393

@orklah orklah closed this as completed Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants