Skip to content

Commit

Permalink
bug #24921 [Debug] Remove false-positive deprecation from DebugClassL…
Browse files Browse the repository at this point in the history
…oader (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Debug] Remove false-positive deprecation from DebugClassLoader

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

A fully up to date 3.4 standard edition still triggers deprecation notices such as
> The "Twig_Extension::getName()" method is deprecated since 1.26 (to be removed in 2.0), not used anymore internally. You should not extend it from "Symfony\Bridge\Twig\Extension\LogoutUrlExtension".

This is a false positive: extending a deprecated method is OK, unless you call the parent, which is another BC layer that should itself trigger the deprecation.

Commits
-------

5edd413 [Debug] Remove false-positive deprecation from DebugClassLoader
  • Loading branch information
fabpot committed Nov 11, 2017
2 parents 054e07f + 5edd413 commit b7a74f7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
14 changes: 3 additions & 11 deletions src/Symfony/Component/Debug/DebugClassLoader.php
Expand Up @@ -31,7 +31,6 @@ class DebugClassLoader
private static $final = array();
private static $finalMethods = array();
private static $deprecated = array();
private static $deprecatedMethods = array();
private static $internal = array();
private static $internalMethods = array();
private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
Expand Down Expand Up @@ -204,12 +203,11 @@ public function loadClass($class)
}
}

// Inherit @final and @deprecated annotations for methods
// Inherit @final and @internal annotations for methods
self::$finalMethods[$name] = array();
self::$deprecatedMethods[$name] = array();
self::$internalMethods[$name] = array();
foreach ($parentAndTraits as $use) {
foreach (array('finalMethods', 'deprecatedMethods', 'internalMethods') as $property) {
foreach (array('finalMethods', 'internalMethods') as $property) {
if (isset(self::${$property}[$use])) {
self::${$property}[$name] = array_merge(self::${$property}[$name], self::${$property}[$use]);
}
Expand All @@ -233,12 +231,6 @@ public function loadClass($class)
}

foreach ($parentAndTraits as $use) {
if (isset(self::$deprecatedMethods[$use][$method->name])) {
list($declaringClass, $message) = self::$deprecatedMethods[$use][$method->name];
if (strncmp($ns, $declaringClass, $len)) {
@trigger_error(sprintf('The "%s::%s()" method is deprecated%s. You should not extend it from "%s".', $declaringClass, $method->name, $message, $name), E_USER_DEPRECATED);
}
}
if (isset(self::$internalMethods[$use][$method->name])) {
list($declaringClass, $message) = self::$internalMethods[$use][$method->name];
if (strncmp($ns, $declaringClass, $len)) {
Expand All @@ -252,7 +244,7 @@ public function loadClass($class)
continue;
}

foreach (array('final', 'deprecated', 'internal') as $annotation) {
foreach (array('final', 'internal') as $annotation) {
if (false !== strpos($doc, '@'.$annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) {
$message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
self::${$annotation.'Methods'}[$name][$method->name] = array($name, $message);
Expand Down
9 changes: 2 additions & 7 deletions src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php
Expand Up @@ -332,7 +332,7 @@ class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
$this->assertSame($xError, $lastError);
}

public function testExtendedDeprecatedMethod()
public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
{
set_error_handler(function () { return false; });
$e = error_reporting(0);
Expand All @@ -346,12 +346,7 @@ class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
$lastError = error_get_last();
unset($lastError['file'], $lastError['line']);

$xError = array(
'type' => E_USER_DEPRECATED,
'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\AnnotatedClass::deprecatedMethod()" method is deprecated since version 3.4. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsAnnotatedClass".',
);

$this->assertSame($xError, $lastError);
$this->assertSame(array('type' => E_USER_NOTICE, 'message' => ''), $lastError);
}

public function testInternalsUse()
Expand Down

0 comments on commit b7a74f7

Please sign in to comment.