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

InvalidConstantAssignmentValue with no docblock type #9376

Closed
edsrzf opened this issue Feb 22, 2023 · 1 comment · Fixed by #9381
Closed

InvalidConstantAssignmentValue with no docblock type #9376

edsrzf opened this issue Feb 22, 2023 · 1 comment · Fixed by #9381

Comments

@edsrzf
Copy link
Contributor

edsrzf commented Feb 22, 2023

When a constant contains a long string, it's possible to receive an InvalidConstantAssignmentValue even though there's no docblock-defined type for the constant:

https://psalm.dev/r/8f0c1f98d6

I believe there are a few things happening here:

  • The long string constant hits the max string length (literal-string becomes non-empty-string after 1000 chars #8075), so the assigned type of the constant is non-empty-string rather than literal-string.
  • However, for some reason, the stored constant type is still literal-string. Maybe it's constructing its type through a different code path that doesn't respect the max string length?
  • ClassConstAnalyzer is type-checking the constant, which kinda seems like a waste of effort since a constant without a docblock type can never result in a type error. (I think?)
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8f0c1f98d6
<?php

class A {
    public const T = 'TEXT';
}

class B
{
    public const ARRAY = [
        'a' => A::T,
        'b' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    ];
}

echo strlen(B::ARRAY['b']) . "\n";
Psalm output (using commit b0e1904):

ERROR: InvalidConstantAssignmentValue - 9:18 - B::ARRAY with declared type array{a: 'TEXT', b: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...'} cannot be assigned type array{a: 'TEXT', b: non-empty-string}

weirdan added a commit to weirdan/psalm that referenced this issue Feb 23, 2023
Fixes vimeo#9376

Ensures:
* that we never have a literal string exceeding the length limit
* that we call string interpreter for all literal strings
weirdan added a commit to weirdan/psalm that referenced this issue Feb 23, 2023
Fixes vimeo#9376

Ensures:
* that we never have a literal string exceeding the length limit
* that we call string interpreter for all literal strings
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.

2 participants