phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

`get_debug_type` produces wrong type for anonymous classes with parent

Open mvorisek opened this issue 1 year ago • 1 comments

Bug report

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

/cc @patrickkusebauch, related with https://github.com/phpstan/phpstan-src/pull/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'

mvorisek avatar Aug 27 '24 07:08 mvorisek

Can investigate no earlier than Friday this week.

patrickkusebauch avatar Aug 27 '24 07:08 patrickkusebauch

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 avatar Aug 30 '24 17:08 patrickkusebauch

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

patrickkusebauch avatar Aug 30 '24 17:08 patrickkusebauch

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[bot] avatar Oct 02 '24 01:10 github-actions[bot]