Skip to content

[HttpFoundation] Deprecate extending some methods #19734

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
merged 1 commit into from
Sep 14, 2016
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
23 changes: 23 additions & 0 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ FrameworkBundle
* The service `serializer.mapping.cache.doctrine.apc` is deprecated. APCu should now
be automatically used when available.

HttpFoundation
---------------

* Extending the following methods of `Response`
is deprecated (these methods will be `final` in 4.0):

- `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`/`isServerError`
- `isOk`/`isForbidden`/`isNotFound`/`isRedirect`/`isEmpty`

Validator
---------

Expand Down
23 changes: 23 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ FrameworkBundle
* The `Controller::getUser()` method has been removed in favor of the ability
to typehint the security user object in the action.

HttpFoundation
---------------

* Extending the following methods of `Response`
is no longer possible (these methods are now `final`):

- `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`/`isServerError`
- `isOk`/`isForbidden`/`isNotFound`/`isRedirect`/`isEmpty`

HttpKernel
----------

Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ 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',
);
private static $deprecationsTriggered = array(
__CLASS__ => true,
BinaryFileResponse::class => true,
JsonResponse::class => true,
RedirectResponse::class => true,
StreamedResponse::class => true,
Copy link
Member

Choose a reason for hiding this comment

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

BinaryFileResponse missing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, thanks !

);

/**
* Constructor.
*
Expand All @@ -201,6 +225,23 @@ 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
50 changes: 50 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\HttpFoundation\Tests;

use Response\DefaultResponse;
use Response\ExtendedResponse;
use Symfony\Bridge\PhpUnit\ErrorAssert;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -843,6 +846,34 @@ public function testSettersAreChainable()
}
}

/**
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
*/
public function testNoDeprecations()
{
ErrorAssert::assertDeprecationsAreTriggered(array(), function () {
new DefaultResponse();
$this->getMock(Response::class);
});
}

/**
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
*/
public function testDeprecations()
{
$deprecationMessages = array();
foreach (array('getDate', 'setLastModified') as $method) {
$deprecationMessages[] = sprintf('Extending %s::%s() in Response\ExtendedResponse is deprecated', Response::class, $method);
}
ErrorAssert::assertDeprecationsAreTriggered($deprecationMessages, function () {
new ExtendedResponse();

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

public function validContentProvider()
{
return array(
Expand Down Expand Up @@ -891,3 +922,22 @@ public function __toString()
return 'Foo';
}
}

namespace Response;

use Symfony\Component\HttpFoundation\Response;

class DefaultResponse extends Response
{
}

class ExtendedResponse extends Response
{
public function setLastModified(\DateTime $date = null)
{
}

public function getDate()
{
}
}