Skip to content

[ErrorHandler][DebugClassLoader] Add mixed and static return types support #37586

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@
class DebugClassLoader
{
private const SPECIAL_RETURN_TYPES = [
'mixed' => 'mixed',
'void' => 'void',
'null' => 'null',
'resource' => 'resource',
'static' => 'object',
'$this' => 'object',
'boolean' => 'bool',
'true' => 'bool',
'false' => 'bool',
Expand All @@ -69,7 +66,13 @@ class DebugClassLoader
'string' => 'string',
'self' => 'self',
'parent' => 'parent',
];
] + (\PHP_VERSION_ID >= 80000 ? [
'$this' => 'static',
] : [
'mixed' => 'mixed',
'static' => 'object',
'$this' => 'object',
]);

private const BUILTIN_RETURN_TYPES = [
'void' => true,
Expand All @@ -83,7 +86,10 @@ class DebugClassLoader
'string' => true,
'self' => true,
'parent' => true,
];
] + (\PHP_VERSION_ID >= 80000 ? [
'mixed' => true,
'static' => true,
] : []);

private const MAGIC_METHODS = [
'__set' => 'void',
Expand Down Expand Up @@ -856,7 +862,7 @@ private function setReturnType(string $types, \ReflectionMethod $method, ?string
}
}

if ('void' === $normalizedType) {
if ('void' === $normalizedType || (\PHP_VERSION_ID >= 80000 && 'mixed' === $normalizedType)) {
$nullable = false;
} elseif (!isset(self::BUILTIN_RETURN_TYPES[$normalizedType]) && isset(self::SPECIAL_RETURN_TYPES[$normalizedType])) {
// ignore other special return types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class_exists('Test\\'.ReturnType::class, true);
error_reporting($e);
restore_error_handler();

$this->assertSame([
$this->assertSame(array_merge([
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeGrandParent::returnTypeGrandParent()" will return "string" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParentInterface::returnTypeParentInterface()" will return "string" as of its next major version. Doing the same in implementation "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeInterface::returnTypeInterface()" will return "string" as of its next major version. Doing the same in implementation "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
Expand All @@ -383,14 +383,21 @@ class_exists('Test\\'.ReturnType::class, true);
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::manyIterables()" will return "array" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::nullableReturnableTypeNormalization()" will return "object" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::nonNullableReturnableTypeNormalization()" will return "void" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::commonNonObjectReturnedTypeNormalization()" will return "object" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::bracketsNormalization()" will return "array" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::booleanNormalization()" will return "bool" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::callableNormalization1()" will return "callable" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::callableNormalization2()" will return "callable" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::otherTypeNormalization()" will return "\ArrayIterator" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::arrayWithLessThanSignNormalization()" will return "array" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
], $deprecations);
], \PHP_VERSION_ID >= 80000 ? [
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::this()" will return "static" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::mixed()" will return "mixed" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::nullableMixed()" will return "mixed" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::static()" will return "static" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
] : [
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::this()" will return "object" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
'Method "Symfony\Component\ErrorHandler\Tests\Fixtures\ReturnTypeParent::static()" will return "object" as of its next major version. Doing the same in child class "Test\Symfony\Component\ErrorHandler\Tests\ReturnType" will be required when upgrading.',
]), $deprecations);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ public function optOutThroughDoc() { }
public function manyIterables() { }
public function nullableReturnableTypeNormalization() { }
public function nonNullableReturnableTypeNormalization() { }
public function commonNonObjectReturnedTypeNormalization() { }
public function bracketsNormalization() { }
public function booleanNormalization() { }
public function callableNormalization1() { }
public function callableNormalization2() { }
public function otherTypeNormalization() { }
public function arrayWithLessThanSignNormalization() { }
public function this() { }
public function mixed() { }
public function nullableMixed() { }
public function static() { }
public function outsideMethod() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function realReturnTypeIsAlreadyThere()
abstract public function realReturnTypeIsAlreadyThereWithNull();

/**
* @return mixed
* @return resource
*/
public function oneCommonNonObjectReturnedType()
{
Expand Down Expand Up @@ -143,13 +143,6 @@ public function nonNullableReturnableTypeNormalization()
{
}

/**
* @return $this
*/
public function commonNonObjectReturnedTypeNormalization()
{
}

/**
* @return \ArrayIterator[]
*/
Expand Down Expand Up @@ -192,6 +185,34 @@ public function arrayWithLessThanSignNormalization()
{
}

/**
* @return $this
*/
public function this()
{
}

/**
* @return mixed
*/
public function mixed()
{
}

/**
* @return mixed|null
*/
public function nullableMixed()
{
}

/**
* @return static
*/
public function static()
{
}

/**
* @return int
*/
Expand Down