feature #32221 [ErrorCatcher] Make IDEs and static analysis tools happy (fabpot)

This PR was squashed before being merged into the 4.4 branch (closes #32221).

Discussion
----------

[ErrorCatcher] Make IDEs and static analysis tools happy

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

f511bc5ff6 deprecated FlattenException::create()
7dd9dbf28d [ErrorHandler] made IDEs and static analysis tools happy
This commit is contained in:
Fabien Potencier 2019-06-27 20:20:54 +02:00
commit b92e4ed9d4
17 changed files with 69 additions and 65 deletions

View File

@ -35,7 +35,7 @@ class PreviewErrorController
public function previewErrorPageAction(Request $request, $code)
{
$exception = FlattenException::create(new \Exception('Something has intentionally gone wrong.'), $code);
$exception = FlattenException::createFromThrowable(new \Exception('Something has intentionally gone wrong.'), $code);
/*
* This Request mimics the parameters set by

View File

@ -26,7 +26,7 @@ class ExceptionControllerTest extends TestCase
$request = $this->createRequest('html');
$request->attributes->set('showException', false);
$exception = FlattenException::create(new \Exception(), 404);
$exception = FlattenException::createFromThrowable(new \Exception(), 404);
$controller = new ExceptionController($twig, /* "showException" defaults to --> */ true);
$response = $controller->showAction($request, $exception, null);
@ -40,7 +40,7 @@ class ExceptionControllerTest extends TestCase
$twig = $this->createTwigEnv(['@Twig/Exception/error.html.twig' => '<html></html>']);
$request = $this->createRequest('txt');
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);
$controller->showAction($request, $exception);
@ -54,7 +54,7 @@ class ExceptionControllerTest extends TestCase
$request = $this->createRequest('txt');
$request->attributes->set('showException', true);
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);
$controller->showAction($request, $exception);
@ -67,7 +67,7 @@ class ExceptionControllerTest extends TestCase
$twig = $this->createTwigEnv(['@Twig/Exception/error.json.twig' => '{}']);
$request = $this->createRequest('json');
$exception = FlattenException::create(new \Exception());
$exception = FlattenException::createFromThrowable(new \Exception());
$controller = new ExceptionController($twig, false);
$response = $controller->showAction($request, $exception);

View File

@ -20,4 +20,13 @@ use Symfony\Component\ErrorCatcher\Exception\FlattenException as BaseFlattenExce
*/
class FlattenException extends BaseFlattenException
{
/**
* @deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception::createFromThrowable() instead.
*/
public static function create(\Exception $exception, $statusCode = null, array $headers = []): self
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\Exception::createFromThrowable() instead.', __METHOD__), E_USER_DEPRECATED);
return parent::createFromThrowable($exception, $statusCode, $headers);
}
}

View File

@ -49,7 +49,7 @@ class ErrorRenderer
/**
* Renders an Exception and returns the Response content.
*
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
* @param \Throwable|FlattenException $exception A \Throwable or FlattenException instance
* @param string $format The request format (html, json, xml, etc.)
*
* @return string The Response content as a string
@ -62,8 +62,8 @@ class ErrorRenderer
throw new ErrorRendererNotFoundException(sprintf('No error renderer found for format "%s".', $format));
}
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
if ($exception instanceof \Throwable) {
$exception = FlattenException::createFromThrowable($exception);
}
return $this->renderers[$format]->render($exception);

View File

@ -150,7 +150,7 @@ EOF
} catch (\Exception $e) {
// something nasty happened and we cannot throw an exception anymore
if ($this->debug) {
$e = FlattenException::create($e);
$e = FlattenException::createFromThrowable($e);
$exceptionMessage = sprintf('Exception thrown when handling an exception (%s: %s)', $e->getClass(), $this->escapeHtml($e->getMessage()));
} else {
$exceptionMessage = 'Whoops, looks like something went wrong.';

View File

@ -36,11 +36,6 @@ class FlattenException
private $file;
private $line;
public static function create(\Exception $exception, $statusCode = null, array $headers = [])
{
return static::createFromThrowable($exception, $statusCode, $headers);
}
public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = []): self
{
$e = new static();

View File

@ -156,12 +156,12 @@ class ExceptionHandler
* This method uses plain PHP functions like header() and echo to output
* the response.
*
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
* @param \Throwable|FlattenException $exception A \Throwable or FlattenException instance
*/
public function sendPhpResponse($exception)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::create($exception);
if ($exception instanceof \Throwable) {
$exception = FlattenException::createFromThrowable($exception);
}
if (!headers_sent()) {

View File

@ -27,7 +27,7 @@ class ErrorRendererTest extends TestCase
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
$container->expects($this->once())->method('has')->with('foo')->willReturn(false);
$exception = FlattenException::create(new \Exception('Foo'));
$exception = FlattenException::createFromThrowable(new \Exception('Foo'));
(new ErrorRenderer($container))->render($exception, 'foo');
}
@ -48,7 +48,7 @@ class ErrorRendererTest extends TestCase
$errorRenderer = new ErrorRenderer($container);
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));
}
}

View File

@ -24,7 +24,7 @@ class ErrorRendererTest extends TestCase
*/
public function testErrorRendererNotFound()
{
$exception = FlattenException::create(new \Exception('foo'));
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
(new ErrorRenderer([]))->render($exception, 'foo');
}
@ -34,7 +34,7 @@ class ErrorRendererTest extends TestCase
*/
public function testInvalidErrorRenderer()
{
$exception = FlattenException::create(new \Exception('foo'));
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
(new ErrorRenderer([new \stdClass()]))->render($exception, 'foo');
}
@ -43,7 +43,7 @@ class ErrorRendererTest extends TestCase
$renderers = [new FooErrorRenderer()];
$errorRenderer = new ErrorRenderer($renderers);
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));
}
}

View File

@ -19,7 +19,7 @@ class HtmlErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '<!DOCTYPE html>%A<html>%A<head>%A<title>Internal Server Error</title>%A<h1 class="break-long-words exception-message">Foo</h1>%A<abbr title="RuntimeException">RuntimeException</abbr>%A';
$this->assertStringMatchesFormat($expected, (new HtmlErrorRenderer())->render($exception));

View File

@ -19,7 +19,7 @@ class JsonErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '{"title":"Internal Server Error","status":500,"detail":"Foo","exceptions":[{"message":"Foo","class":"RuntimeException","trace":';
$this->assertStringStartsWith($expected, (new JsonErrorRenderer())->render($exception));

View File

@ -19,7 +19,7 @@ class TxtErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '[title] Internal Server Error%A[status] 500%A[detail] Foo%A[1] RuntimeException: Foo%A';
$this->assertStringMatchesFormat($expected, (new TxtErrorRenderer())->render($exception));

View File

@ -19,7 +19,7 @@ class XmlErrorRendererTest extends TestCase
{
public function testRender()
{
$exception = FlattenException::create(new \RuntimeException('Foo'));
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
$expected = '<?xml version="1.0" encoding="UTF-8" ?>%A<problem xmlns="urn:ietf:rfc:7807">%A<title>Internal Server Error</title>%A<status>500</status>%A<detail>Foo</detail>%A';
$this->assertStringMatchesFormat($expected, (new XmlErrorRenderer())->render($exception));

View File

@ -34,10 +34,10 @@ class FlattenExceptionTest extends TestCase
{
public function testStatusCode()
{
$flattened = FlattenException::create(new \RuntimeException(), 403);
$flattened = FlattenException::createFromThrowable(new \RuntimeException(), 403);
$this->assertEquals('403', $flattened->getStatusCode());
$flattened = FlattenException::create(new \RuntimeException());
$flattened = FlattenException::createFromThrowable(new \RuntimeException());
$this->assertEquals('500', $flattened->getStatusCode());
$flattened = FlattenException::createFromThrowable(new \DivisionByZeroError(), 403);
@ -46,72 +46,72 @@ class FlattenExceptionTest extends TestCase
$flattened = FlattenException::createFromThrowable(new \DivisionByZeroError());
$this->assertEquals('500', $flattened->getStatusCode());
$flattened = FlattenException::create(new NotFoundHttpException());
$flattened = FlattenException::createFromThrowable(new NotFoundHttpException());
$this->assertEquals('404', $flattened->getStatusCode());
$flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
$flattened = FlattenException::createFromThrowable(new UnauthorizedHttpException('Basic realm="My Realm"'));
$this->assertEquals('401', $flattened->getStatusCode());
$flattened = FlattenException::create(new BadRequestHttpException());
$flattened = FlattenException::createFromThrowable(new BadRequestHttpException());
$this->assertEquals('400', $flattened->getStatusCode());
$flattened = FlattenException::create(new NotAcceptableHttpException());
$flattened = FlattenException::createFromThrowable(new NotAcceptableHttpException());
$this->assertEquals('406', $flattened->getStatusCode());
$flattened = FlattenException::create(new ConflictHttpException());
$flattened = FlattenException::createFromThrowable(new ConflictHttpException());
$this->assertEquals('409', $flattened->getStatusCode());
$flattened = FlattenException::create(new MethodNotAllowedHttpException(['POST']));
$flattened = FlattenException::createFromThrowable(new MethodNotAllowedHttpException(['POST']));
$this->assertEquals('405', $flattened->getStatusCode());
$flattened = FlattenException::create(new AccessDeniedHttpException());
$flattened = FlattenException::createFromThrowable(new AccessDeniedHttpException());
$this->assertEquals('403', $flattened->getStatusCode());
$flattened = FlattenException::create(new GoneHttpException());
$flattened = FlattenException::createFromThrowable(new GoneHttpException());
$this->assertEquals('410', $flattened->getStatusCode());
$flattened = FlattenException::create(new LengthRequiredHttpException());
$flattened = FlattenException::createFromThrowable(new LengthRequiredHttpException());
$this->assertEquals('411', $flattened->getStatusCode());
$flattened = FlattenException::create(new PreconditionFailedHttpException());
$flattened = FlattenException::createFromThrowable(new PreconditionFailedHttpException());
$this->assertEquals('412', $flattened->getStatusCode());
$flattened = FlattenException::create(new PreconditionRequiredHttpException());
$flattened = FlattenException::createFromThrowable(new PreconditionRequiredHttpException());
$this->assertEquals('428', $flattened->getStatusCode());
$flattened = FlattenException::create(new ServiceUnavailableHttpException());
$flattened = FlattenException::createFromThrowable(new ServiceUnavailableHttpException());
$this->assertEquals('503', $flattened->getStatusCode());
$flattened = FlattenException::create(new TooManyRequestsHttpException());
$flattened = FlattenException::createFromThrowable(new TooManyRequestsHttpException());
$this->assertEquals('429', $flattened->getStatusCode());
$flattened = FlattenException::create(new UnsupportedMediaTypeHttpException());
$flattened = FlattenException::createFromThrowable(new UnsupportedMediaTypeHttpException());
$this->assertEquals('415', $flattened->getStatusCode());
if (class_exists(SuspiciousOperationException::class)) {
$flattened = FlattenException::create(new SuspiciousOperationException());
$flattened = FlattenException::createFromThrowable(new SuspiciousOperationException());
$this->assertEquals('400', $flattened->getStatusCode());
}
}
public function testHeadersForHttpException()
{
$flattened = FlattenException::create(new MethodNotAllowedHttpException(['POST']));
$flattened = FlattenException::createFromThrowable(new MethodNotAllowedHttpException(['POST']));
$this->assertEquals(['Allow' => 'POST'], $flattened->getHeaders());
$flattened = FlattenException::create(new UnauthorizedHttpException('Basic realm="My Realm"'));
$flattened = FlattenException::createFromThrowable(new UnauthorizedHttpException('Basic realm="My Realm"'));
$this->assertEquals(['WWW-Authenticate' => 'Basic realm="My Realm"'], $flattened->getHeaders());
$flattened = FlattenException::create(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
$flattened = FlattenException::createFromThrowable(new ServiceUnavailableHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
$this->assertEquals(['Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'], $flattened->getHeaders());
$flattened = FlattenException::create(new ServiceUnavailableHttpException(120));
$flattened = FlattenException::createFromThrowable(new ServiceUnavailableHttpException(120));
$this->assertEquals(['Retry-After' => 120], $flattened->getHeaders());
$flattened = FlattenException::create(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
$flattened = FlattenException::createFromThrowable(new TooManyRequestsHttpException('Fri, 31 Dec 1999 23:59:59 GMT'));
$this->assertEquals(['Retry-After' => 'Fri, 31 Dec 1999 23:59:59 GMT'], $flattened->getHeaders());
$flattened = FlattenException::create(new TooManyRequestsHttpException(120));
$flattened = FlattenException::createFromThrowable(new TooManyRequestsHttpException(120));
$this->assertEquals(['Retry-After' => 120], $flattened->getHeaders());
}
@ -133,7 +133,7 @@ class FlattenExceptionTest extends TestCase
public function testWrappedThrowable()
{
$exception = new FatalThrowableError(new \DivisionByZeroError('Ouch', 42));
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame('Ouch', $flattened->getMessage(), 'The message is copied from the original error.');
$this->assertSame(42, $flattened->getCode(), 'The code is copied from the original error.');
@ -169,7 +169,7 @@ class FlattenExceptionTest extends TestCase
{
$exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
$flattened = FlattenException::create($exception)->getPrevious();
$flattened = FlattenException::createFromThrowable($exception)->getPrevious();
$this->assertEquals($flattened->getMessage(), 'Oh noes!', 'The message is copied from the original exception.');
$this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
@ -223,7 +223,7 @@ class FlattenExceptionTest extends TestCase
$this->assertSame(
FlattenException::createFromThrowable($exception)->toArray(),
FlattenException::create($exception)->toArray()
FlattenException::createFromThrowable($exception)->toArray()
);
}
@ -262,7 +262,7 @@ class FlattenExceptionTest extends TestCase
NAN,
]);
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$trace = $flattened->getTrace();
$args = $trace[1]['args'];
$array = $args[0][1];
@ -303,7 +303,7 @@ class FlattenExceptionTest extends TestCase
$a = ['foo', [2, &$a]];
$exception = $this->createException($a);
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$trace = $flattened->getTrace();
$this->assertContains('*DEEP NESTED ARRAY*', serialize($trace));
}
@ -322,7 +322,7 @@ class FlattenExceptionTest extends TestCase
$a[21] = 'value1';
$exception = $this->createException($a);
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$trace = $flattened->getTrace();
$this->assertSame($trace[1]['args'][0], ['array', ['array', '*SKIPPED over 10000 entries*']]);
@ -335,12 +335,12 @@ class FlattenExceptionTest extends TestCase
public function testAnonymousClass()
{
$flattened = FlattenException::create(new class() extends \RuntimeException {
$flattened = FlattenException::createFromThrowable(new class() extends \RuntimeException {
});
$this->assertSame('RuntimeException@anonymous', $flattened->getClass());
$flattened = FlattenException::create(new \Exception(sprintf('Class "%s" blah.', \get_class(new class() extends \RuntimeException {
$flattened = FlattenException::createFromThrowable(new \Exception(sprintf('Class "%s" blah.', \get_class(new class() extends \RuntimeException {
}))));
$this->assertSame('Class "RuntimeException@anonymous" blah.', $flattened->getMessage());
@ -350,7 +350,7 @@ class FlattenExceptionTest extends TestCase
{
$exception = new \RuntimeException();
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
$this->assertSame($exception->__toString(), $flattened->getAsString());
@ -364,7 +364,7 @@ class FlattenExceptionTest extends TestCase
$exception = $test('foo123', 1, null, 1.5);
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
$this->assertSame($exception->__toString(), $flattened->getAsString());
@ -375,7 +375,7 @@ class FlattenExceptionTest extends TestCase
$exception = new \LogicException('This is message 1');
$exception = new \RuntimeException('This is messsage 2', 500, $exception);
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
$this->assertSame($exception->__toString(), $flattened->getAsString());

View File

@ -29,7 +29,7 @@ class ExceptionDataCollector extends DataCollector
{
if (null !== $exception) {
$this->data = [
'exception' => FlattenException::create($exception),
'exception' => FlattenException::createFromThrowable($exception),
];
}
}

View File

@ -42,7 +42,7 @@ class ExceptionListener implements EventSubscriberInterface
public function logKernelException(GetResponseForExceptionEvent $event)
{
$e = FlattenException::create($event->getException());
$e = FlattenException::createFromThrowable($event->getException());
$this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
}
@ -64,7 +64,7 @@ class ExceptionListener implements EventSubscriberInterface
try {
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
} catch (\Exception $e) {
$f = FlattenException::create($e);
$f = FlattenException::createFromThrowable($e);
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
@ -132,7 +132,7 @@ class ExceptionListener implements EventSubscriberInterface
{
$attributes = [
'_controller' => $this->controller,
'exception' => FlattenException::create($exception),
'exception' => FlattenException::createFromThrowable($exception),
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
];
$request = $request->duplicate(null, null, $attributes);

View File

@ -23,7 +23,7 @@ class ExceptionDataCollectorTest extends TestCase
{
$e = new \Exception('foo', 500);
$c = new ExceptionDataCollector();
$flattened = FlattenException::create($e);
$flattened = FlattenException::createFromThrowable($e);
$trace = $flattened->getTrace();
$this->assertFalse($c->hasException());