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

Defining enum case with a class constant causes a crash #8978

Closed
jack-worman opened this issue Dec 21, 2022 · 4 comments · Fixed by #10655
Closed

Defining enum case with a class constant causes a crash #8978

jack-worman opened this issue Dec 21, 2022 · 4 comments · Fixed by #10655

Comments

@jack-worman
Copy link
Contributor

https://psalm.dev/r/224fdb8a5d

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/224fdb8a5d
<?php

enum AppEnvs: string
{
    case Development = Environments::DEV;
    case Production = Environments::PROD;
    case Testing = Environments::TEST;
}

class Environments {
    public const DEV = 'dev';
    public const PROD = 'prod';
    public const TEST = 'test';
}
Psalm encountered an internal error:

/vendor/vimeo/psalm/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php: Failed to infer case value for Development

@jack-worman
Copy link
Contributor Author

It seems to be because in SimpleTypeInferer::infer():

                if ($existing_class_constants === null
                    && $file_source instanceof StatementsAnalyzer
                ) {
                    try {
                        $foreign_class_constant = $codebase->classlikes->getClassConstantType(
                            $const_fq_class_name,
                            $stmt->name->name,
                            ReflectionProperty::IS_PRIVATE,
                            $file_source,
                        );

                        if ($foreign_class_constant) {
                            return $foreign_class_constant;
                        }

                        return null;
                    } catch (InvalidArgumentException | CircularReferenceException $e) {
                        return null;
                    }
                }
            }

$file_source instanceof StatementsAnalyzer returns false.

@weirdan
Copy link
Collaborator

weirdan commented Feb 24, 2023

$file_source instanceof StatementsAnalyzer returns false.

That's because we're trying to infer values in the scanning phase. When that fails, we need to store UnresolvedConstantComponent instead and resolve it later in the analysis phase (when all classlikes are already loaded) rather than throwing an exception.

@DmytroPolavis
Copy link

Hey, any update on this? Having a similar issue, and what is more frustrating is that directive does not work for a file with such error :(

weirdan added a commit to weirdan/psalm that referenced this issue Feb 5, 2024
Resolves a number of long-standing bugs ('Failed to infer case value ...')

Fixes vimeo#10374
Fixes vimeo#10560
Fixes vimeo#10643
Fixes vimeo#8978
weirdan added a commit to weirdan/psalm that referenced this issue Feb 5, 2024
Resolves a number of long-standing bugs ('Failed to infer case value ...')

Fixes vimeo#10374
Fixes vimeo#10560
Fixes vimeo#10643
Fixes vimeo#8978
@weirdan weirdan closed this as completed in 52eadab Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants