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

get_debug_type produces wrong type for anonymous classes with parent #11562

Closed
mvorisek opened this issue Aug 27, 2024 · 4 comments · Fixed by phpstan/phpstan-src#3374
Closed

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Aug 27, 2024

Bug report

The get_debug_type() produces currently a wrong constant string type.

/cc @patrickkusebauch, related with phpstan/phpstan-src#2910

phpstan-src test:

diff --git a/tests/PHPStan/Analyser/nsrt/get-debug-type.php b/tests/PHPStan/Analyser/nsrt/get-debug-type.php
index 975ea62..8ec1c48 100644
--- a/tests/PHPStan/Analyser/nsrt/get-debug-type.php
+++ b/tests/PHPStan/Analyser/nsrt/get-debug-type.php
@@ -18,6 +18,7 @@ function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrStr
 	$o = new \stdClass();
 	$A = new A();
 	$anonymous = new class {};
+	$anonymousWithParent = new class extends \stdClass {};
 
 	assertType("'bool'", get_debug_type($b));
 	assertType("'bool'", get_debug_type(true));
@@ -35,6 +36,7 @@ function doFoo(bool $b, int $i, float $f, $d, $r, string $s, array $a, $intOrStr
 	assertType("'int'|'string'", get_debug_type($intOrString));
 	assertType("'array'|'GetDebugType\\\\A'", get_debug_type($arrayOrObject));
 	assertType("'class@anonymous'", get_debug_type($anonymous));
+	assertType("'stdClass@anonymous'", get_debug_type($anonymousWithParent));
 }
 
 /**

Code snippet that reproduces the problem

https://phpstan.org/r/f8fea2c3-61e7-416a-844f-24adf0e53377

Expected output

Dumped type: 'stdClass@anonymous'

@patrickkusebauch
Copy link

Can investigate no earlier than Friday this week.

@patrickkusebauch
Copy link

patrickkusebauch commented Aug 30, 2024

Initial investigation findings:

  • My implementation was based on the PHP documentation page. The bug described here is not properly documented there. The examples of outputs of this function do not show the case where the anonymous class extends another class or implements an interface. I will follow up with an issue/PR to PHP to add it there.
  • Looked at the PHP RFC for the function. It is also missing this case.
  • Followed with the PR to PHP implementing the function. This PR includes a discussion on this topic, what the behavior in such cases should be, and tests covering such cases.
  • Will now work off of the merged commit to PHP source code to fix the bug in PHPStan.

@patrickkusebauch
Copy link

PR to PHPStan opened, issue to PHP docs opened, and PR to PHP docs opened.

Copy link

github-actions bot commented Oct 2, 2024

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 Oct 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants