Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #34771 Deprecate *Response::create() methods (fabpot)
This PR was merged into the 5.1-dev branch.

Discussion
----------

Deprecate *Response::create() methods

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a<!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | -

The `::create()` methods of the `Response` class hierarchy are not needed anymore in modern PHP versions as we can use parenthesis around the new operator. So `Response::create()->...()` is equivalent to `(new Response)->...()`.

Let's deprecate the static method. It's also good as the first parameter type changes depending on the sub-class.

Commits
-------

ebb13e7 Deprecate *Response::create() methods
  • Loading branch information
fabpot committed Dec 3, 2019
2 parents 9097230 + ebb13e7 commit 6c16390
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 5 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-5.1.md
Expand Up @@ -7,6 +7,13 @@ FrameworkBundle
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.

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

* Deprecate `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

Routing
-------

Expand Down
7 changes: 7 additions & 0 deletions UPGRADE-6.0.md
Expand Up @@ -7,6 +7,13 @@ FrameworkBundle
* Removed `MicroKernelTrait::configureRoutes()`.
* Made `MicroKernelTrait::configureRouting()` abstract.

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

* Removed `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

Routing
-------

Expand Down
Expand Up @@ -33,7 +33,7 @@ class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
public function onKernelException(ExceptionEvent $event)
{
if ($event->getThrowable() instanceof Danger) {
$event->setResponse(Response::create('It\'s dangerous to go alone. Take this ⚔'));
$event->setResponse(new Response('It\'s dangerous to go alone. Take this ⚔'));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
@@ -1,6 +1,13 @@
CHANGELOG
=========

5.1.0
-----

* Deprecate `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)

5.0.0
-----

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Expand Up @@ -63,9 +63,13 @@ public function __construct($data = null, int $status = 200, array $headers = []
* @param array $headers An array of response headers
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($data = null, int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($data, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/RedirectResponse.php
Expand Up @@ -53,9 +53,13 @@ public function __construct(string $url, int $status = 302, array $headers = [])
* @param string $url The URL to redirect to
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($url = '', int $status = 302, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($url, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -208,9 +208,13 @@ public function __construct(?string $content = '', int $status = 200, array $hea
* ->setSharedMaxAge(300);
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create(?string $content = '', int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($content, $status, $headers);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Expand Up @@ -47,9 +47,13 @@ public function __construct(callable $callback = null, int $status = 200, array
* @param callable|null $callback A valid PHP callback or null to set it later
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create($callback = null, int $status = 200, array $headers = [])
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 5.1; use __construct() instead.', __METHOD__), E_USER_DEPRECATED);

return new static($callback, $status, $headers);
}

Expand Down
35 changes: 31 additions & 4 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Expand Up @@ -90,6 +90,9 @@ public function testSetJson()
$this->assertEquals('true', $response->getContent());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = JsonResponse::create(['foo' => 'bar'], 204);
Expand All @@ -99,27 +102,39 @@ public function testCreate()
$this->assertEquals(204, $response->getStatusCode());
}

/**
* @group legacy
*/
public function testStaticCreateEmptyJsonObject()
{
$response = JsonResponse::create();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{}', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateJsonArray()
{
$response = JsonResponse::create([0, 1, 2, 3]);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('[0,1,2,3]', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateJsonObject()
{
$response = JsonResponse::create(['foo' => 'bar']);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
$this->assertSame('{"foo":"bar"}', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateWithSimpleTypes()
{
$response = JsonResponse::create('foo');
Expand All @@ -140,25 +155,37 @@ public function testStaticCreateWithSimpleTypes()
$this->assertSame('true', $response->getContent());
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomStatus()
{
$response = JsonResponse::create([], 202);
$this->assertSame(202, $response->getStatusCode());
}

/**
* @group legacy
*/
public function testStaticCreateAddsContentTypeHeader()
{
$response = JsonResponse::create();
$this->assertSame('application/json', $response->headers->get('Content-Type'));
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomHeaders()
{
$response = JsonResponse::create([], 200, ['ETag' => 'foo']);
$this->assertSame('application/json', $response->headers->get('Content-Type'));
$this->assertSame('foo', $response->headers->get('ETag'));
}

/**
* @group legacy
*/
public function testStaticCreateWithCustomContentType()
{
$headers = ['Content-Type' => 'application/vnd.acme.blog-v1+json'];
Expand All @@ -169,7 +196,7 @@ public function testStaticCreateWithCustomContentType()

public function testSetCallback()
{
$response = JsonResponse::create(['foo' => 'bar'])->setCallback('callback');
$response = (new JsonResponse(['foo' => 'bar']))->setCallback('callback');

$this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
$this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
Expand Down Expand Up @@ -217,7 +244,7 @@ public function testSetCallbackInvalidIdentifier()
public function testSetContent()
{
$this->expectException('InvalidArgumentException');
JsonResponse::create("\xB1\x31");
new JsonResponse("\xB1\x31");
}

public function testSetContentJsonSerializeError()
Expand All @@ -230,12 +257,12 @@ public function testSetContentJsonSerializeError()

$serializable = new JsonSerializableObject();

JsonResponse::create($serializable);
new JsonResponse($serializable);
}

public function testSetComplexCallback()
{
$response = JsonResponse::create(['foo' => 'bar']);
$response = new JsonResponse(['foo' => 'bar']);
$response->setCallback('ಠ_ಠ["foo"].bar[0]');

$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
Expand Down
Expand Up @@ -59,6 +59,9 @@ public function testSetTargetUrl()
$this->assertEquals('baz.beep', $response->getTargetUrl());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = RedirectResponse::create('foo', 301);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -20,6 +20,9 @@
*/
class ResponseTest extends ResponseTestCase
{
/**
* @group legacy
*/
public function testCreate()
{
$response = Response::create('foo', 301, ['Foo' => 'bar']);
Expand Down
Expand Up @@ -101,6 +101,9 @@ public function testGetContent()
$this->assertFalse($response->getContent());
}

/**
* @group legacy
*/
public function testCreate()
{
$response = StreamedResponse::create(function () {}, 204);
Expand Down

0 comments on commit 6c16390

Please sign in to comment.