From cd6b58e555a71b942dd5532c7095439fa9d96350 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Wed, 29 Sep 2021 10:08:20 +0100
Subject: [PATCH] Issue #3239553 by andypost, alexpott, daffie, larowlan:
 \Symfony\Component\Routing\Route::getRequirement() causes deprecation errors
 on PHP 8.1 when it returns NULL

---
 core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php  | 2 +-
 core/modules/rest/src/Plugin/views/display/RestExport.php  | 7 ++++---
 .../src/Routing/AcceptHeaderMatcher.php                    | 2 +-
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
index b782d9af6271..beb988f6ebe3 100644
--- a/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
+++ b/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
@@ -25,7 +25,7 @@ public function filter(RouteCollection $collection, Request $request) {
     $format = $request->getContentType();
 
     foreach ($collection as $name => $route) {
-      $supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format')));
+      $supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format') ?? ''));
       if (empty($supported_formats)) {
         // No restriction on the route, so we move the route to the end of the
         // collection by re-adding it. That way generic routes sink down in the
diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php
index fb59cf562f9a..b3733e4ec87e 100644
--- a/core/modules/rest/src/Plugin/views/display/RestExport.php
+++ b/core/modules/rest/src/Plugin/views/display/RestExport.php
@@ -383,10 +383,11 @@ public function collectRoutes(RouteCollection $collection) {
    *   TRUE, when the view should override the given route.
    */
   protected function overrideApplies($view_path, Route $view_route, Route $route) {
-    $route_formats = explode('|', $route->getRequirement('_format'));
-    $view_route_formats = explode('|', $view_route->getRequirement('_format'));
+    $route_has_format = $route->hasRequirement('_format');
+    $route_formats = $route_has_format ? explode('|', $route->getRequirement('_format')) : [];
+    $view_route_formats = $view_route->hasRequirement('_format') ? explode('|', $view_route->getRequirement('_format')) : [];
     return $this->overrideAppliesPathAndMethod($view_path, $view_route, $route)
-      && (!$route->hasRequirement('_format') || array_intersect($route_formats, $view_route_formats) != []);
+      && (!$route_has_format || array_intersect($route_formats, $view_route_formats) != []);
   }
 
   /**
diff --git a/core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php b/core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php
index a90d4f4607c8..ee4c013b69eb 100644
--- a/core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php
+++ b/core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php
@@ -24,7 +24,7 @@ public function filter(RouteCollection $collection, Request $request) {
 
     foreach ($collection as $name => $route) {
       // _format could be a |-delimited list of supported formats.
-      $supported_formats = array_filter(explode('|', $route->getRequirement('_format')));
+      $supported_formats = array_filter(explode('|', $route->getRequirement('_format') ?? ''));
 
       if (empty($supported_formats)) {
         // No format restriction on the route, so it always matches. Move it to
-- 
GitLab