From e147702d4022a0bdfe3cc71c9c9507474b01f0f0 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 20 Sep 2021 10:08:14 +0100
Subject: [PATCH] Issue #3232082 by daffie, longwave, catch: [Symfony 6] Add
 "Response" type hint to methods that implement the method
 Symfony\Component\HttpKernel\HttpKernelInterface::handle()

---
 core/lib/Drupal/Core/DrupalKernel.php         |  2 +-
 .../Core/StackMiddleware/KernelPreHandle.php  |  3 ++-
 .../StackMiddleware/NegotiationMiddleware.php |  3 ++-
 .../ReverseProxyMiddleware.php                |  3 ++-
 .../Drupal/Core/StackMiddleware/Session.php   |  3 ++-
 core/lib/Drupal/Core/Update/UpdateKernel.php  |  3 ++-
 core/modules/ban/src/BanMiddleware.php        |  2 +-
 .../src/StackMiddleware/PageCache.php         |  2 +-
 .../src/AcceptHeaderMiddleware.php            |  3 ++-
 .../src/MonkeysInTheControlRoom.php           |  3 ++-
 .../StackMiddleware/FormTestMiddleware.php    |  3 ++-
 .../src/HttpKernel/TestMiddleware.php         |  3 ++-
 .../Bootstrap/UncaughtExceptionTest.php       |  2 +-
 .../NegotiationMiddlewareTest.php             | 25 ++++++++++++++++---
 .../ReverseProxyMiddlewareTest.php            | 15 ++++++++++-
 15 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index a175e04b9486..04ce6ca97b03 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -690,7 +690,7 @@ public function terminate(Request $request, Response $response) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     // Ensure sane PHP environment variables.
     static::bootEnvironment();
 
diff --git a/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php b/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php
index 6d469e2507e9..5df27acc7142 100644
--- a/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php
+++ b/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\DrupalKernelInterface;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -41,7 +42,7 @@ public function __construct(HttpKernelInterface $http_kernel, DrupalKernelInterf
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     $this->drupalKernel->preHandle($request);
 
     return $this->httpKernel->handle($request, $type, $catch);
diff --git a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php
index f3b30e29a3df..ecc6dd61ea1c 100644
--- a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php
+++ b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php
@@ -3,6 +3,7 @@
 namespace Drupal\Core\StackMiddleware;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -37,7 +38,7 @@ public function __construct(HttpKernelInterface $app) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     // Register available mime types.
     foreach ($this->formats as $format => $mime_type) {
       $request->setFormat($format, $mime_type);
diff --git a/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php b/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php
index 647f44bbbc4c..69221e798919 100644
--- a/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php
+++ b/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Site\Settings;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -41,7 +42,7 @@ public function __construct(HttpKernelInterface $http_kernel, Settings $settings
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     // Initialize proxy settings.
     static::setSettingsOnRequest($request, $this->settings);
     return $this->httpKernel->handle($request, $type, $catch);
diff --git a/core/lib/Drupal/Core/StackMiddleware/Session.php b/core/lib/Drupal/Core/StackMiddleware/Session.php
index d520916d26ac..8c3eb5d74f0f 100644
--- a/core/lib/Drupal/Core/StackMiddleware/Session.php
+++ b/core/lib/Drupal/Core/StackMiddleware/Session.php
@@ -4,6 +4,7 @@
 
 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -47,7 +48,7 @@ public function __construct(HttpKernelInterface $http_kernel, $service_name = 's
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     if ($type === self::MASTER_REQUEST && PHP_SAPI !== 'cli') {
       $session = $this->container->get($this->sessionServiceName);
       $session->start();
diff --git a/core/lib/Drupal/Core/Update/UpdateKernel.php b/core/lib/Drupal/Core/Update/UpdateKernel.php
index edcee199607b..4a781ff477d2 100644
--- a/core/lib/Drupal/Core/Update/UpdateKernel.php
+++ b/core/lib/Drupal/Core/Update/UpdateKernel.php
@@ -9,6 +9,7 @@
 use Drupal\Core\Routing\RouteObjectInterface;
 use Symfony\Component\HttpFoundation\ParameterBag;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
 
 /**
@@ -54,7 +55,7 @@ protected function cacheDrupalContainer(array $container_definition) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     try {
       static::bootEnvironment();
 
diff --git a/core/modules/ban/src/BanMiddleware.php b/core/modules/ban/src/BanMiddleware.php
index eda1fa0eb418..dc5b8d4dae00 100644
--- a/core/modules/ban/src/BanMiddleware.php
+++ b/core/modules/ban/src/BanMiddleware.php
@@ -42,7 +42,7 @@ public function __construct(HttpKernelInterface $http_kernel, BanIpManagerInterf
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     $ip = $request->getClientIp();
     if ($this->banIpManager->isBanned($ip)) {
       return new Response(new FormattableMarkup('@ip has been banned', ['@ip' => $ip]), 403);
diff --git a/core/modules/page_cache/src/StackMiddleware/PageCache.php b/core/modules/page_cache/src/StackMiddleware/PageCache.php
index ba07414d63ea..2c425657fbcf 100644
--- a/core/modules/page_cache/src/StackMiddleware/PageCache.php
+++ b/core/modules/page_cache/src/StackMiddleware/PageCache.php
@@ -76,7 +76,7 @@ public function __construct(HttpKernelInterface $http_kernel, CacheBackendInterf
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     // Only allow page caching on master request.
     if ($type === static::MASTER_REQUEST && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) {
       $response = $this->lookup($request, $type, $catch);
diff --git a/core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php b/core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php
index f607a895accc..45f2bb7c5813 100644
--- a/core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php
+++ b/core/modules/system/tests/modules/accept_header_routing_test/src/AcceptHeaderMiddleware.php
@@ -3,6 +3,7 @@
 namespace Drupal\accept_header_routing_test;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -23,7 +24,7 @@ public function __construct(HttpKernelInterface $app) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     $mapping = [
       'application/json' => 'json',
       'application/hal+json' => 'hal_json',
diff --git a/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php b/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
index 4595a0559266..304d8ed93b6e 100644
--- a/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
+++ b/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Site\Settings;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -36,7 +37,7 @@ public function __construct(HttpKernelInterface $app, Settings $settings) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     if (\Drupal::state()->get('error_service_test.break_bare_html_renderer')) {
       // Let the bedlam begin.
       // 1) Force a container rebuild.
diff --git a/core/modules/system/tests/modules/form_test/src/StackMiddleware/FormTestMiddleware.php b/core/modules/system/tests/modules/form_test/src/StackMiddleware/FormTestMiddleware.php
index 6afaea9e6e3b..3db45b9c400c 100644
--- a/core/modules/system/tests/modules/form_test/src/StackMiddleware/FormTestMiddleware.php
+++ b/core/modules/system/tests/modules/form_test/src/StackMiddleware/FormTestMiddleware.php
@@ -3,6 +3,7 @@
 namespace Drupal\form_test\StackMiddleware;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -30,7 +31,7 @@ public function __construct(HttpKernelInterface $http_kernel) {
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     $response = $this->httpKernel->handle($request, $type, $catch);
     $response->headers->set('X-Form-Test-Stack-Middleware', 'invoked');
     return $response;
diff --git a/core/modules/system/tests/modules/httpkernel_test/src/HttpKernel/TestMiddleware.php b/core/modules/system/tests/modules/httpkernel_test/src/HttpKernel/TestMiddleware.php
index f9e9849ef360..5bf54c3d9d47 100644
--- a/core/modules/system/tests/modules/httpkernel_test/src/HttpKernel/TestMiddleware.php
+++ b/core/modules/system/tests/modules/httpkernel_test/src/HttpKernel/TestMiddleware.php
@@ -3,6 +3,7 @@
 namespace Drupal\httpkernel_test\HttpKernel;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -40,7 +41,7 @@ public function __construct(HttpKernelInterface $kernel, $optional_argument = NU
   /**
    * {@inheritdoc}
    */
-  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
+  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE): Response {
     $request->attributes->set('_hello', 'world');
     if ($request->attributes->has('_optional_argument')) {
       $request->attributes->set('_previous_optional_argument', $request->attributes->get('_optional_argument'));
diff --git a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
index 117190960dab..21b02065b22b 100644
--- a/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
+++ b/core/tests/Drupal/FunctionalTests/Bootstrap/UncaughtExceptionTest.php
@@ -265,7 +265,7 @@ public function testLoggerException() {
     $this->assertStringContainsString('Failed to log error', $errors[0], 'The error handling logs when an error could not be logged to the logger.');
 
     $expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php';
-    $expected_line = 61;
+    $expected_line = 62;
     $expected_entry = "Failed to log error: Exception: Deforestation in Drupal\\error_service_test\\MonkeysInTheControlRoom->handle() (line ${expected_line} of ${expected_path})";
     $this->assertStringContainsString($expected_entry, $errors[0], 'Original error logged to the PHP error log when an exception is thrown by a logger');
 
diff --git a/core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php b/core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php
index 6752ee145bcd..bd1137de8495 100644
--- a/core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php
+++ b/core/tests/Drupal/Tests/Core/StackMiddleware/NegotiationMiddlewareTest.php
@@ -11,6 +11,7 @@
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\HttpFoundation\ParameterBag;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
@@ -35,7 +36,7 @@ class NegotiationMiddlewareTest extends UnitTestCase {
   protected function setUp(): void {
     parent::setUp();
 
-    $this->app = $this->prophesize(HttpKernelInterface::class);
+    $this->app = $this->prophesize(MockedHttpKernelInterface::class);
     $this->contentNegotiation = new StubNegotiationMiddleware($this->app->reveal());
   }
 
@@ -121,6 +122,13 @@ public function testHandle() {
    * @covers ::registerFormat
    */
   public function testSetFormat() {
+    $app = $this->createMock(MockedHttpKernelInterface::class);
+    $app->expects($this->once())
+      ->method('handle')
+      ->will($this->returnValue($this->createMock(Response::class)));
+
+    $content_negotiation = new StubNegotiationMiddleware($app);
+
     $request = $this->prophesize(Request::class);
 
     // Default empty format list should not set any formats.
@@ -135,8 +143,8 @@ public function testSetFormat() {
     $request_mock->request = $request_data->reveal();
 
     // Trigger handle.
-    $this->contentNegotiation->registerFormat('david', 'geeky/david');
-    $this->contentNegotiation->handle($request_mock);
+    $content_negotiation->registerFormat('david', 'geeky/david');
+    $content_negotiation->handle($request_mock);
   }
 
 }
@@ -148,3 +156,14 @@ public function getContentType(Request $request) {
   }
 
 }
+
+/**
+ * Helper interface for the Symfony 6 version of the HttpKernelInterface.
+ *
+ * @todo Remove this interface when the Symfony 6 is in core.
+ */
+interface MockedHttpKernelInterface extends HttpKernelInterface {
+
+  public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = TRUE): Response;
+
+}
diff --git a/core/tests/Drupal/Tests/Core/StackMiddleware/ReverseProxyMiddlewareTest.php b/core/tests/Drupal/Tests/Core/StackMiddleware/ReverseProxyMiddlewareTest.php
index 01f220fef7f7..9823175f7112 100644
--- a/core/tests/Drupal/Tests/Core/StackMiddleware/ReverseProxyMiddlewareTest.php
+++ b/core/tests/Drupal/Tests/Core/StackMiddleware/ReverseProxyMiddlewareTest.php
@@ -6,6 +6,8 @@
 use Drupal\Core\StackMiddleware\ReverseProxyMiddleware;
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 /**
  * Unit test the reverse proxy stack middleware.
@@ -23,7 +25,7 @@ class ReverseProxyMiddlewareTest extends UnitTestCase {
    * {@inheritdoc}
    */
   protected function setUp(): void {
-    $this->mockHttpKernel = $this->createMock('Symfony\Component\HttpKernel\HttpKernelInterface');
+    $this->mockHttpKernel = $this->createMock(MockHttpKernelInterface::class);
   }
 
   /**
@@ -104,3 +106,14 @@ protected function trustedHeadersAreSet(Settings $settings, $expected_trusted_he
   }
 
 }
+
+/**
+ * Helper interface for the Symfony 6 version of the HttpKernelInterface.
+ *
+ * @todo Remove this interface when the Symfony 6 is in core.
+ */
+interface MockHttpKernelInterface extends HttpKernelInterface {
+
+  public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = TRUE): Response;
+
+}
-- 
GitLab