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

@var annotation is ignored for class constants #942

Closed
weirdan opened this issue Aug 14, 2018 · 8 comments
Closed

@var annotation is ignored for class constants #942

weirdan opened this issue Aug 14, 2018 · 8 comments

Comments

@weirdan
Copy link
Collaborator

weirdan commented Aug 14, 2018

https://getpsalm.org/r/03a2f11ca3

<?php
  /** @param void ...$_p*/
  function type(...$_p): void {}

  class C {
    /** @var int[] */
    const N = [];
  }

  type(C::N);

Expected: should complain about array<mixed,int> provided
Actual: complains about array<empty,empty> provided

There's at least one usage of @var on constants in Psalm's tests:

/**
* @var string[]
*/
protected const VALUES = [];

Additionally, psalm seems to ignore @var and const initialization contradiction:

<?php
class C {
  /** @var int */
  public const N = "aaaa"; // should produce issues, but doesn't
}
@vasily-kartashov
Copy link

Just as a side note, the words var and const clash semantically.

@sakshamsaxena
Copy link

Hey, @muglug. Is this still open ? Would like to start with this, looks interesting.

@muglug
Copy link
Collaborator

muglug commented Mar 18, 2019

@sakshamsaxena

Sure!

Requirement:
Psalm currently does a lot of inference of constant types. If you provide your own type it should override that inference.

Nice-to-have:
Any classes that override a parent's constant value should be the same type as defined by the parent. You'd check for that in ClassAnalyzer::analyze, probably.

@sakshamsaxena
Copy link

@muglug Thanks! I'll try it out 😄

@bugreportuser
Copy link
Contributor

When this is supported, it could be good for Psalm to only use the type for the rest of the analysis. That would resolve #2076.

@bdsl
Copy link
Contributor

bdsl commented Jan 16, 2020

I just ran into this issue - reviewing a PR from a colleague using keys and values from two existing class constant arrays. I suggested typing the new constant using key-of and value-of to make sure they are spelled right. May need to change it from a constant to a property because of this issue.

AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Dec 10, 2021
AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Dec 11, 2021
AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Dec 13, 2021
AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Jan 20, 2022
AndrolGenhald added a commit to AndrolGenhald/psalm that referenced this issue Jan 22, 2022
orklah added a commit that referenced this issue Jan 25, 2022
…-class-constants

Support type annotations for class consts (fixes #942).
@AndrolGenhald
Copy link
Collaborator

@orklah Can be closed, not sure why it didn't happen automatically with the commit message.

@orklah
Copy link
Collaborator

orklah commented Jan 25, 2022

It doesn't work in title and possibly not in commit message either. It has to be in the body of the PR I believe

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

No branches or pull requests

8 participants