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

Anonymous class phpdoc is not parsed #7345

Closed
mvorisek opened this issue May 29, 2022 · 10 comments
Closed

Anonymous class phpdoc is not parsed #7345

mvorisek opened this issue May 29, 2022 · 10 comments
Labels

Comments

@mvorisek
Copy link
Contributor

mvorisek commented May 29, 2022

Bug report

#[\AllowDynamicProperties]
class Cl {}

/**
 * @property int $prop
 */
$a = new class() extends Cl {};

$a->prop = 10;
var_dump($a->prop);
var_dump((new \ReflectionClass($a))->getDocComment());

the php recognizes the phpdoc class comment correctly and all code that relies on phpdoc annotation too, so I submit it as a bug

https://3v4l.org/W4cjH

real usecase/testcase https://github.com/mvorisek/atk4-hintable-mirror/blob/24951ac3d1a0a4a9beb7a8ed6c5790ea76a4176e/tests/Data/HintableModelArrayTest.php#L62-L84

Code snippet that reproduces the problem

https://phpstan.org/r/02bc0432-1c81-4868-95ae-cd1d4b276578

Expected output

no error

@ondrejmirtes
Copy link
Member

  1. The PHPDoc needs to be above the anonymous class directly
  2. But it still doesn't work - it's more like a case of "@property does not work above anonymous classes".

@andypost
Copy link

I'm getting Attribute class AllowDynamicProperties does not exist. running on PHP 8.1 on Drupal https://www.drupal.org/pift-ci-job/2363989

@neclimdul
Copy link

might be a different error since its not on an anonymous class. here's a playground for it.
https://phpstan.org/r/021bc1c5-d27b-4bc8-8974-1a5116b49d9f

@phpstan-bot
Copy link
Contributor

@neclimdul After the latest commit in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-6: Attribute class AllowDynamicProperties does not exist.
+No errors

@mvorisek
Copy link
Contributor Author

mvorisek commented Dec 8, 2023

The PHPDoc needs to be above the anonymous class directly

php/php-src#12909 - phpdoc can be currently defined "more above than the anonymous class directly", but attributes must be declared "above the anonymous class directly only".

@ondrejmirtes
Copy link
Member

After closer inspection, this works as expected: https://phpstan.org/r/a670d04c-fd9f-4650-986d-30651e8dbf6f

@mvorisek
Copy link
Contributor Author

@ondrejmirtes as explained in the php-src issue and in https://3v4l.org/W4cjH, ReflectionClass::getDocComment() can read phpdoc also above variable assignment and phpstan should read the phpdoc too. Please reopen this issue.

@ondrejmirtes
Copy link
Member

@mvorisek No, the PHPDoc needs to be directly above the class for PHPStan to see it. I don't consider this a bug.

@mvorisek
Copy link
Contributor Author

PHP works some way. PHPstan should parse the phpdoc from exactly the same places as ReflectionClass::getDocComment() does. I belive this is PHPStan bug to be fixed in order to behave consistently.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants