Skip to content

[Debug] Support @final on methods #21465

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
wants to merge 2 commits into from
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
40 changes: 34 additions & 6 deletions src/Symfony/Component/Debug/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DebugClassLoader
private $isFinder;
private static $caseCheck;
private static $final = array();
private static $finalMethods = array();
private static $deprecated = array();
private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
private static $darwinCache = array('/' => array('/', array()));
Expand Down Expand Up @@ -164,13 +165,40 @@ public function loadClass($class)
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
}

if (preg_match('#\n \* @final(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
self::$final[$name] = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
}

$parent = get_parent_class($class);
if ($parent && isset(self::$final[$parent])) {
@trigger_error(sprintf('The %s class is considered final%s. It may change without further notice as of its next major version. You should not extend it from %s.', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);

// Not an interface nor a trait
if (class_exists($name, false)) {
if (preg_match('#\n \* @final(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
self::$final[$name] = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
}

if ($parent && isset(self::$final[$parent])) {
@trigger_error(sprintf('The %s class is considered final%s. It may change without further notice as of its next major version. You should not extend it from %s.', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
}

// Inherit @final annotations
self::$finalMethods[$name] = $parent && isset(self::$finalMethods[$parent]) ? self::$finalMethods[$parent] : array();

foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
if ($method->class !== $name) {
continue;
}

if ($parent && isset(self::$finalMethods[$parent][$method->name])) {
@trigger_error(sprintf('%s It may change without further notice as of its next major version. You should not extend it from %s.', self::$finalMethods[$parent][$method->name], $name), E_USER_DEPRECATED);
}

$doc = $method->getDocComment();
if (false === $doc || false === strpos($doc, '@final')) {
continue;
}

if (preg_match('#\n\s+\* @final(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) {
$message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]) : '';
self::$finalMethods[$name][$method->name] = sprintf('The %s::%s() method is considered final%s.', $name, $method->name, $message);
}
}
}

if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);

$this->assertSame($xError, $lastError);
}

public function testExtendedFinalMethod()
{
set_error_handler(function () { return false; });
$e = error_reporting(0);
trigger_error('', E_USER_NOTICE);

class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);

error_reporting($e);
restore_error_handler();

$lastError = error_get_last();
unset($lastError['file'], $lastError['line']);

$xError = array(
'type' => E_USER_DEPRECATED,
'message' => 'The Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod() method is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod.',
Copy link
Member

@nicolas-grekas nicolas-grekas Jan 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to turn this into an @expectedDeprecation annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just followed the rest of the file, I guess it was done to not add the legacy group.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed!

);

$this->assertSame($xError, $lastError);
}
}

class ClassLoader
Expand Down Expand Up @@ -324,6 +346,10 @@ public function findFile($class)
return $fixtureDir.'DeprecatedInterface.php';
} elseif (__NAMESPACE__.'\Fixtures\FinalClass' === $class) {
return $fixtureDir.'FinalClass.php';
} elseif (__NAMESPACE__.'\Fixtures\FinalMethod' === $class) {
return $fixtureDir.'FinalMethod.php';
} elseif (__NAMESPACE__.'\Fixtures\ExtendedFinalMethod' === $class) {
return $fixtureDir.'ExtendedFinalMethod.php';
} elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
} elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Debug/Tests/Fixtures/ExtendedFinalMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures;

class ExtendedFinalMethod extends FinalMethod
{
/**
* {@inheritdoc}
*/
public function finalMethod()
{
}

public function anotherMethod()
{
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/Debug/Tests/Fixtures/FinalMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures;

class FinalMethod
{
/**
* @final since version 3.3.
*/
public function finalMethod()
{
}

public function anotherMethod()
{
}
}
45 changes: 0 additions & 45 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,6 @@ class Response
511 => 'Network Authentication Required', // RFC6585
);

private static $deprecatedMethods = array(
'setDate', 'getDate',
'setExpires', 'getExpires',
'setLastModified', 'getLastModified',
'setProtocolVersion', 'getProtocolVersion',
'setStatusCode', 'getStatusCode',
'setCharset', 'getCharset',
'setPrivate', 'setPublic',
'getAge', 'getMaxAge', 'setMaxAge', 'setSharedMaxAge',
'getTtl', 'setTtl', 'setClientTtl',
'getEtag', 'setEtag',
'hasVary', 'getVary', 'setVary',
'isInvalid', 'isSuccessful', 'isRedirection',
'isClientError', 'isOk', 'isForbidden',
'isNotFound', 'isRedirect', 'isEmpty',
'isCacheable', 'isFresh', 'isValidateable',
'mustRevalidate', 'setCache', 'setNotModified',
'isNotModified', 'isInformational', 'isServerError',
'closeOutputBuffers', 'ensureIEOverSSLCompatibility',
);
private static $deprecationsTriggered = array(
__CLASS__ => true,
BinaryFileResponse::class => true,
JsonResponse::class => true,
RedirectResponse::class => true,
StreamedResponse::class => true,
);

/**
* Constructor.
*
Expand All @@ -229,23 +201,6 @@ public function __construct($content = '', $status = 200, $headers = array())
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');

// Deprecations
$class = get_class($this);
if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) {
$class = get_parent_class($class);
}
if (isset(self::$deprecationsTriggered[$class])) {
return;
}

self::$deprecationsTriggered[$class] = true;
foreach (self::$deprecatedMethods as $method) {
$r = new \ReflectionMethod($class, $method);
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Extending %s::%s() in %s is deprecated since version 3.2 and won\'t be supported anymore in 4.0 as it will be final.', __CLASS__, $method, $class), E_USER_DEPRECATED);
}
}
}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,25 +843,6 @@ public function testSettersAreChainable()
}
}

public function testNoDeprecationsAreTriggered()
{
new DefaultResponse();
$this->getMockBuilder(Response::class)->getMock();
}

/**
* @group legacy
* @expectedDeprecation Extending Symfony\Component\HttpFoundation\Response::getDate() in Symfony\Component\HttpFoundation\Tests\ExtendedResponse is deprecated %s.
* @expectedDeprecation Extending Symfony\Component\HttpFoundation\Response::setLastModified() in Symfony\Component\HttpFoundation\Tests\ExtendedResponse is deprecated %s.
*/
public function testDeprecations()
{
new ExtendedResponse();

// Deprecations should not be triggered twice
new ExtendedResponse();
}

public function validContentProvider()
{
return array(
Expand Down