Skip to content

[Debug][DebugClassLoader] Discourage using the \Serializable interface #30987

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

Closed
Closed
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
5 changes: 3 additions & 2 deletions src/Symfony/Component/Debug/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ CHANGELOG

* made the `ErrorHandler` and `ExceptionHandler` classes final
* added `Exception\FlattenException::getAsString` and
`Exception\FlattenException::getTraceAsString` to increase compatibility to php
exception objects
`Exception\FlattenException::getTraceAsString` to increase compatibility to php exception objects
* added a deprecation for classes and interfaces that implement or extend the `\Serializable` interface
and that are not ready for the new serialization mechanism (currently restricted to Symfony core classes)

4.0.0
-----
Expand Down
26 changes: 21 additions & 5 deletions src/Symfony/Component/Debug/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,22 @@ public function checkAnnotations(\ReflectionClass $refl, $class)
if ($refl->isInterface() && false !== \strpos($doc, 'method') && preg_match_all('#\n \* @method\s+(static\s+)?+(?:[\w\|&\[\]\\\]+\s+)?(\w+(?:\s*\([^\)]*\))?)+(.+?([[:punct:]]\s*)?)?(?=\r?\n \*(?: @|/$|\r?\n))#', $doc, $notice, PREG_SET_ORDER)) {
foreach ($notice as $method) {
$static = '' !== $method[1];
$name = $method[2];
$realName = $name = $method[2];
$description = $method[3] ?? null;
if (false === strpos($name, '(')) {
if (false === $i = strpos($name, '(')) {
$name .= '()';
} else {
$realName = substr($name, 0, $i);
}

if (null !== $description) {
$description = trim($description);
if (!isset($method[4])) {
$description .= '.';
}
}
self::$method[$class][] = [$class, $name, $static, $description];

self::$method[$class][$realName] = [$class, $name, $static, $description];
}
}
}
Expand Down Expand Up @@ -295,12 +299,11 @@ public function checkAnnotations(\ReflectionClass $refl, $class)
} elseif (!$refl->isInterface()) {
$hasCall = $refl->hasMethod('__call');
$hasStaticCall = $refl->hasMethod('__callStatic');
foreach (self::$method[$use] as $method) {
foreach (self::$method[$use] as $realName => $method) {
list($interface, $name, $static, $description) = $method;
if ($static ? $hasStaticCall : $hasCall) {
continue;
}
$realName = substr($name, 0, strpos($name, '('));
if (!$refl->hasMethod($realName) || !($methodRefl = $refl->getMethod($realName))->isPublic() || ($static && !$methodRefl->isStatic()) || (!$static && $methodRefl->isStatic())) {
$deprecations[] = sprintf('Class "%s" should implement method "%s::%s"%s', $class, ($static ? 'static ' : '').$interface, $name, null == $description ? '.' : ': '.$description);
}
Expand All @@ -313,6 +316,19 @@ public function checkAnnotations(\ReflectionClass $refl, $class)
return $deprecations;
}

if (is_subclass_of($class, 'Serializable')) {
foreach (['serialize', 'unserialize'] as $methodName) {
if ($class !== $refl->getMethod($methodName)->class || isset(self::$method[$class]['__'.$methodName])) {
continue;
}

if ($refl->hasMethod('__'.$methodName) ? $class !== $refl->getMethod('__'.$methodName)->class : 0 === strpos($class, 'Symfony\\')) {
$deprecations[] = sprintf('Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "%s".', $class);
break;
}
}
}

// Inherit @final, @internal and @param annotations for methods
self::$finalMethods[$class] = [];
self::$internalMethods[$class] = [];
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsAClassImplementingSerializable;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsAnInterfaceThatExtendsSerializable;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializable;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializableWithTheNewMechanism;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializableWithTheNewMechanismThroughVirtualMethods;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ImplementsAnInterfaceThatExtendsSerializable;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ImplementsSerializable;
use Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ImplementsSerializableWithTheNewMechanism;

class DebugClassLoaderTest extends TestCase
{
Expand Down Expand Up @@ -361,6 +369,40 @@ public function testEvaluatedCode()
{
$this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\DefinitionInEvaluatedCode', true));
}

/**
* @runInSeparateProcess
*
* @dataProvider implementsSerializableProvider
*/
public function testImplementsSerializable(array $expectedDeprecations, string $class)
{
$deprecations = [];
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
$e = error_reporting(E_USER_DEPRECATED);

class_exists($class, true);

error_reporting($e);
restore_error_handler();

$this->assertSame($expectedDeprecations, $deprecations);
}

public function implementsSerializableProvider(): array
{
return [
[['Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ImplementsSerializable".'], ImplementsSerializable::class],
[[], ImplementsSerializableWithTheNewMechanism::class],
[['Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ImplementsSerializable".'], ExtendsAClassImplementingSerializable::class],
[['Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializable".'], ImplementsAnInterfaceThatExtendsSerializable::class],
[['Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializable".'], ExtendsSerializable::class],
[[], ExtendsSerializableWithTheNewMechanism::class],
[[], ExtendsSerializableWithTheNewMechanismThroughVirtualMethods::class],
[['Implementing "Serializable" without also implementing the "__serialize" and "__unserialize" methods is deprecated in "Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable\ExtendsSerializable".'], ExtendsAnInterfaceThatExtendsSerializable::class],
[[], 'SymfonyFoo\\'.__NAMESPACE__.'\ImplementsSerializableOutsideOfSymfony'], // We don't trigger if the root namespace is not Symfony
];
}
}

class ClassLoader
Expand Down Expand Up @@ -443,6 +485,11 @@ public function ownAbstractBaseMethod() { }
} elseif ('Test\\'.__NAMESPACE__.'\ExtendsVirtualMagicCall' === $class) {
eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsVirtualMagicCall extends \\'.__NAMESPACE__.'\Fixtures\VirtualClassMagicCall implements \\'.__NAMESPACE__.'\Fixtures\VirtualInterface {
}');
} elseif ('SymfonyFoo\\'.__NAMESPACE__.'\ImplementsSerializableOutsideOfSymfony' === $class) {
eval('namespace SymfonyFoo\\'.__NAMESPACE__.'; class ImplementsSerializableOutsideOfSymfony implements \Serializable {
public function serialize() { }
public function unserialize($serialized) { }
}');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

class ExtendsAClassImplementingSerializable extends ImplementsSerializable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

interface ExtendsAnInterfaceThatExtendsSerializable extends ExtendsSerializable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

/**
* @method void __unserialize(array $data)
*/
interface ExtendsSerializable extends \Serializable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

interface ExtendsSerializableWithTheNewMechanism extends \Serializable
{
public function __serialize(): array;

public function __unserialize(array $data): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

/**
* @method array __serialize(): array
* @method void __unserialize(array $data): void
*/
interface ExtendsSerializableWithTheNewMechanismThroughVirtualMethods extends \Serializable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

class ImplementsAnInterfaceThatExtendsSerializable implements ExtendsSerializable
{
/**
* {@inheritdoc}
*/
public function serialize()
{
return 'foo';
}

public function __unserialize(array $data): void
{
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

class ImplementsSerializable implements \Serializable
{
public function __serialize(): array
{
}

/**
* {@inheritdoc}
*/
public function serialize()
{
return 'foo';
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures\DiscourageSerializable;

class ImplementsSerializableWithTheNewMechanism implements \Serializable
{
public function __serialize(): array
{
}

/**
* {@inheritdoc}
*/
public function serialize()
{
return 'foo';
}

public function __unserialize(array $data): void
{
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
}
}