8000 Move deprecations to the DebugClassLoader · symfony/symfony@b2bb9c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b2bb9c3

Browse files
committed
Move deprecations to the DebugClassLoader
1 parent 9403988 commit b2bb9c3

File tree

7 files changed

+54
-31
lines changed

7 files changed

+54
-31
lines changed

src/Symfony/Component/Debug/DebugClassLoader.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DebugClassLoader
2828
private $isFinder;
2929
private static $caseCheck;
3030
private static $deprecated = array();
31+
private static $final = array();
3132
private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
3233
private static $darwinCache = array('/' => array('/', array()));
3334

@@ -165,9 +166,18 @@ public function loadClass($class)
165166

166167
if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
167168
@trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
168-
} elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
169+
}
170+
171+
if (preg_match('#\n \* @final( .*)?\r?\n \*(?: @|/$)#s', $refl->getDocComment())) {
172+
self::$final[$name] = true;
173+
}
174+
175+
if (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
169176
self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
170177
} else {
178+
// Trigger deprecations for deprecated and final parents
179+
// only if the class is not deprecated itself
180+
171181
if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
172182
$len = 0;
173183
$ns = '';
@@ -187,6 +197,9 @@ public function loadClass($class)
187197
if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
188198
@trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
189199
}
200+
if ($parent && isset(self::$final[$parent]) && strncmp($ns, $parent, $len)) {
201+
@trigger_error(sprintf('Extending the class %s in %s is deprecated and may break in the next major release.', $parent, $name), E_USER_DEPRECATED);
202+
}
190203

191204
$parentInterfaces = array();
192205
$deprecatedInterfaces = array();

src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,28 @@ class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
267267

268268
$this->assertSame($xError, $lastError);
269269
}
270+
271+
public function testExtendedFinalClass()
272+
{
273+
set_error_handler(function () { return false; });
274+
$e = error_reporting(0);
275+
trigger_error('', E_USER_NOTICE);
276+
277+
class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
278+
279+
error_reporting($e);
280+
restore_error_handler();
281+
282+
$lastError = error_get_last();
283+
unset($lastError['file'], $lastError['line']);
284+
285+
$xError = array(
286+
'type' => E_USER_DEPRECATED,
287+
'message' => sprintf('Extending the class %s in Test\Symfony\Component\Debug\Tests\ExtendsFinalClass is deprecated and may break in the next major release.', Fixtures\FinalClass::class),
288+
);
289+
290+
$this->assertSame($xError, $lastError);
291+
}
270292
}
271293

272294
class ClassLoader
@@ -275,11 +297,6 @@ public function loadClass($class)
275297
{
276298
}
277299

278-
public function getClassMap()
279-
{
280-
return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
281-
}
282-
283300
public function findFile($class)
284301
{
285302
$fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
@@ -300,6 +317,8 @@ public function findFile($class)
300317
return $fixtureDir.'notPsr0Bis.php';
301318
} elseif (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
302319
return $fixtureDir.'DeprecatedInterface.php';
320+
} elseif (__NAMESPACE__.'\Fixtures\FinalClass' === $class) {
321+
return $fixtureDir.'FinalClass.php';
303322
} elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
304323
eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
305324
} elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
@@ -310,6 +329,8 @@ public function findFile($class)
310329
eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
311330
} elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
312331
eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
332+
} elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
333+
eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
313334
}
314335
}
315336
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Debug\Tests\Fixtures;
4+
5+
/**
6+
* @final
7+
*/
8+
class FinalClass
9+
{
10+
}

src/Symfony/Component/Serializer/Encoder/ChainDecoder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @author Jordi Boggiano <j.boggiano@seld.be>
2020
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2121
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
22+
*
23+
* @final
2224
*/
2325
class ChainDecoder implements DecoderInterface
2426
{
@@ -28,10 +30,6 @@ class ChainDecoder implements DecoderInterface
2830
public function __construct(array $decoders = array())
2931
{
3032
$this->decoders = $decoders;
31-
32-
if (__CLASS__ !== get_class($this)) {
33-
@trigger_error(sprintf('Extending %s is deprecated since 3.3 and won\'t be supported in 4.0 as it will be final.', __CLASS__), E_USER_DEPRECATED);
34-
}
3533
}
3634

3735
/**

src/Symfony/Component/Serializer/Encoder/ChainEncoder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @author Jordi Boggiano <j.boggiano@seld.be>
2020
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2121
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
22+
*
23+
* @final
2224
*/
2325
class ChainEncoder implements EncoderInterface
2426
{
@@ -28,10 +30,6 @@ class ChainEncoder implements EncoderInterface
2830
public function __construct(array $encoders = array())
2931
{
3032
$this->encoders = $encoders;
31-
32-
if (__CLASS__ !== get_class($this)) {
33-
@trigger_error(sprintf('Extending %s is deprecated since 3.3 and won\'t be supported in 4.0 as it will be final.', __CLASS__), E_USER_DEPRECATED);
34-
}
3533
}
3634

3735
/**

src/Symfony/Component/Serializer/Tests/Encoder/ChainDecoderTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,4 @@ public function testDecodeUnsupportedFormat()
7474
{
7575
$this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
7676
}
77-
78-
/**
79-
* @group legacy
80-
* @expectedDeprecation Extending Symfony\Component\Serializer\Encoder\ChainDecoder is deprecated %s.
81-
*/
82-
public function testExtendDeprecation()
83-
{
84-
new ExtendedChainDecoder();
85-
}
86-
}
87-
88-
class ExtendedChainDecoder extends ChainDecoder
89-
{
9077
}

src/Symfony/Component/Serializer/Tests/Encoder/ChainEncoderTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,3 @@ public function supportsEncoding($format)
127127
return true;
128128
}
129129
}
130-
131-
class ExtendedChainEncoder extends ChainEncoder
132-
{
133-
}

0 commit comments

Comments
 (0)
0