Skip to content

Crashing on arguments with default value self::class #4713

@e1himself

Description

@e1himself

Bug report

PHPStan crashes when trying to parse code with arguments having default value set to self::class.

<?php

class Service
{
	public static function createInstance(string $class = self::class): Service
	{
		return new $class();
	}
}

$service = Service::createInstance();

Internal error: Failed to find parent class of constant "class". Run PHPStan with --debug option and post the stack trace to: https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

This seems to be related to #4288, but not exactly.

Stack trace:

PHP Fatal error:  Uncaught LogicException: Failed to find parent class of constant "class". in phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php:192
Stack trace:
#0 phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php(146): PHPStan\BetterReflection\Reflection\ReflectionParameter->findParentClassDeclaringConstant('class')
#1 phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php(257): PHPStan\BetterReflection\Reflection\ReflectionParameter->parseDefaultValueNode()
#2 phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php(274): PHPStan\BetterReflection\Reflection\ReflectionParameter->getDefaultValue()
#3 phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php(322): in phar:///source/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionParameter.php on line 192

Code snippet that reproduces the problem

https://phpstan.org/r/1c85152d-6693-4420-9811-9301934276ac

Expected output

No errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions