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

Error not reported for list<key-of<properties-of<self>>> #11254

Open
zmitic opened this issue Feb 6, 2025 · 4 comments
Open

Error not reported for list<key-of<properties-of<self>>> #11254

zmitic opened this issue Feb 6, 2025 · 4 comments

Comments

@zmitic
Copy link

zmitic commented Feb 6, 2025

Based on documentation, the following code should report problems for attributeNames method but it doesn't: https://psalm.dev/r/bbd2bcc7b2

Use-case: this type is perfect for Doctrine PreUpdate event when we need to do something, but only when specified properties are updated and not all.

Copy link

I found these snippets:

https://psalm.dev/r/bbd2bcc7b2
<?php

class A 
{
  public string $foo = 'foo!';
  public int $bar = 42;

  /**
   * @return list<key-of<properties-of<self>>>
   */
  public function attributeNames(): array 
  {
    return ['not_existing_property_1', 'not_existing_property_2'];
  }
}

$a = new A();
$b = $a->attributeNames();
/** @psalm-trace $b */
Psalm output (using commit c105211):

INFO: Trace - 19:23 - $b: list<string>

@danog
Copy link
Collaborator

danog commented Feb 6, 2025

The class is non-final, making it final fixes the issue (though since here we're explicitly specifying self, that should not be an issue...)

@zmitic
Copy link
Author

zmitic commented Feb 6, 2025

Thanks, this helped a lot. I can't make the class final because of Doctrine, but I can make phpdoc version of it and now it works: https://psalm.dev/r/91ef7f5c23

Copy link

I found these snippets:

https://psalm.dev/r/91ef7f5c23
<?php

/** @final */
class A 
{
  public string $foo = 'foo!';
  public int $bar = 42;

  /**
   * @return list<key-of<properties-of<self>>>
   */
  public function attributeNames(): array 
  {
    return ['not_existing_property_1', 'not_existing_property_2'];
  }
}

$a = new A();
$b = $a->attributeNames();
/** @psalm-trace $b */
Psalm output (using commit d161a53):

INFO: Trace - 20:23 - $b: list<'bar'|'foo'>

ERROR: InvalidReturnStatement - 14:12 - The inferred type 'list{'not_existing_property_1', 'not_existing_property_2'}' does not match the declared return type 'list<'bar'|'foo'>' for A::attributeNames

ERROR: InvalidReturnType - 10:14 - The declared return type 'list<'bar'|'foo'>' for A::attributeNames is incorrect, got 'list{'not_existing_property_1', 'not_existing_property_2'}'

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