From b69a33ede23a45bd32f0b3f6c23d008a567232d0 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 20 Oct 2021 10:23:57 +0100
Subject: [PATCH] Issue #3161223 by andypost, longwave, Hardik_Patel_12,
 alexpott, jungle, dww: Use PHP 7.0 "spaceship" operator for user sort
 functions where possible

---
 core/lib/Drupal/Component/Utility/SortArray.php          | 6 +-----
 core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php  | 2 +-
 core/lib/Drupal/Core/Language/Language.php               | 2 +-
 core/lib/Drupal/Core/Routing/RouteProvider.php           | 2 +-
 core/modules/filter/src/FilterPluginCollection.php       | 2 +-
 .../src/Plugin/HelpSection/HelpTopicSection.php          | 2 +-
 core/modules/image/src/ImageEffectPluginCollection.php   | 8 +-------
 .../language/src/HttpKernel/PathProcessorLanguage.php    | 9 +--------
 core/modules/layout_builder/src/Section.php              | 2 +-
 core/modules/search/src/Entity/SearchPage.php            | 2 +-
 core/modules/shortcut/src/Entity/Shortcut.php            | 2 +-
 core/modules/tour/src/Entity/Tour.php                    | 5 +----
 core/modules/views/src/ViewsData.php                     | 7 ++-----
 core/modules/views/src/ViewsDataHelper.php               | 8 ++------
 core/modules/views/tests/src/Functional/ViewTestBase.php | 5 +----
 .../views/tests/src/Kernel/ViewsKernelTestBase.php       | 5 +----
 core/modules/views/views.views.inc                       | 2 +-
 core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php  | 5 +----
 18 files changed, 20 insertions(+), 56 deletions(-)

diff --git a/core/lib/Drupal/Component/Utility/SortArray.php b/core/lib/Drupal/Component/Utility/SortArray.php
index 2ef6223f73d2..6c1c91802493 100644
--- a/core/lib/Drupal/Component/Utility/SortArray.php
+++ b/core/lib/Drupal/Component/Utility/SortArray.php
@@ -122,11 +122,7 @@ public static function sortByKeyInt($a, $b, $key) {
     $a_weight = (is_array($a) && isset($a[$key])) ? $a[$key] : 0;
     $b_weight = (is_array($b) && isset($b[$key])) ? $b[$key] : 0;
 
-    if ($a_weight == $b_weight) {
-      return 0;
-    }
-
-    return ($a_weight < $b_weight) ? -1 : 1;
+    return $a_weight <=> $b_weight;
   }
 
 }
diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
index 9b60c70daeb7..72a90545e32b 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
@@ -235,7 +235,7 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
       $b_label = $b->label() ?? '';
       return strnatcasecmp($a_label, $b_label);
     }
-    return ($a_weight < $b_weight) ? -1 : 1;
+    return $a_weight <=> $b_weight;
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Language/Language.php b/core/lib/Drupal/Core/Language/Language.php
index 5c4068d0505d..b1efc6bb0781 100644
--- a/core/lib/Drupal/Core/Language/Language.php
+++ b/core/lib/Drupal/Core/Language/Language.php
@@ -160,7 +160,7 @@ public static function sort(&$languages) {
         }
         return strnatcasecmp($a_name, $b_name);
       }
-      return ($a_weight < $b_weight) ? -1 : 1;
+      return $a_weight <=> $b_weight;
     });
   }
 
diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php
index 1bd1cc238067..24c6f6ea0575 100644
--- a/core/lib/Drupal/Core/Routing/RouteProvider.php
+++ b/core/lib/Drupal/Core/Routing/RouteProvider.php
@@ -392,7 +392,7 @@ protected function routeProviderRouteCompare(array $a, array $b) {
     }
     // Reverse sort from highest to lowest fit. PHP should cast to int, but
     // the explicit cast makes this sort more robust against unexpected input.
-    return (int) $a['fit'] < (int) $b['fit'] ? 1 : -1;
+    return (int) $b['fit'] <=> (int) $a['fit'];
   }
 
   /**
diff --git a/core/modules/filter/src/FilterPluginCollection.php b/core/modules/filter/src/FilterPluginCollection.php
index 81ebddf4728f..31386ed78634 100644
--- a/core/modules/filter/src/FilterPluginCollection.php
+++ b/core/modules/filter/src/FilterPluginCollection.php
@@ -94,7 +94,7 @@ public function sortHelper($aID, $bID) {
       return !empty($a->status) ? -1 : 1;
     }
     if ($a->weight != $b->weight) {
-      return $a->weight < $b->weight ? -1 : 1;
+      return $a->weight <=> $b->weight;
     }
     if ($a->provider != $b->provider) {
       return strnatcasecmp($a->provider, $b->provider);
diff --git a/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php
index c604d650ae46..566aa1e3c964 100644
--- a/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php
+++ b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php
@@ -183,7 +183,7 @@ protected function getPlugins() {
         $a_label = (string) $a->getLabel();
         $b_label = (string) $b->getLabel();
         if ($a_label === $b_label) {
-          return $a->getPluginId() < $b->getPluginId() ? -1 : 1;
+          return $a->getPluginId() <=> $b->getPluginId();
         }
         return strnatcasecmp($a_label, $b_label);
       });
diff --git a/core/modules/image/src/ImageEffectPluginCollection.php b/core/modules/image/src/ImageEffectPluginCollection.php
index 25ee6f2a268d..a32975f9368d 100644
--- a/core/modules/image/src/ImageEffectPluginCollection.php
+++ b/core/modules/image/src/ImageEffectPluginCollection.php
@@ -22,13 +22,7 @@ public function &get($instance_id) {
    * {@inheritdoc}
    */
   public function sortHelper($aID, $bID) {
-    $a_weight = $this->get($aID)->getWeight();
-    $b_weight = $this->get($bID)->getWeight();
-    if ($a_weight == $b_weight) {
-      return 0;
-    }
-
-    return ($a_weight < $b_weight) ? -1 : 1;
+    return $this->get($aID)->getWeight() <=> $this->get($bID)->getWeight();
   }
 
 }
diff --git a/core/modules/language/src/HttpKernel/PathProcessorLanguage.php b/core/modules/language/src/HttpKernel/PathProcessorLanguage.php
index 9feae277c7f8..512f22bde920 100644
--- a/core/modules/language/src/HttpKernel/PathProcessorLanguage.php
+++ b/core/modules/language/src/HttpKernel/PathProcessorLanguage.php
@@ -147,14 +147,7 @@ protected function initProcessors($scope) {
     // Sort the processors list, so that their functions are called in the
     // order specified by the weight of the methods.
     uksort($this->processors[$scope], function ($method_id_a, $method_id_b) use ($weights) {
-      $a_weight = $weights[$method_id_a];
-      $b_weight = $weights[$method_id_b];
-
-      if ($a_weight == $b_weight) {
-        return 0;
-      }
-
-      return ($a_weight < $b_weight) ? -1 : 1;
+      return $weights[$method_id_a] <=> $weights[$method_id_b];
     });
   }
 
diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php
index cd27437cb2f6..e87bb7c30551 100644
--- a/core/modules/layout_builder/src/Section.php
+++ b/core/modules/layout_builder/src/Section.php
@@ -258,7 +258,7 @@ public function getComponentsByRegion($region) {
       return $component->getRegion() === $region;
     });
     uasort($components, function (SectionComponent $a, SectionComponent $b) {
-      return $a->getWeight() > $b->getWeight() ? 1 : -1;
+      return $a->getWeight() <=> $b->getWeight();
     });
     return $components;
   }
diff --git a/core/modules/search/src/Entity/SearchPage.php b/core/modules/search/src/Entity/SearchPage.php
index 9d6e3980c869..91cc93960f9f 100644
--- a/core/modules/search/src/Entity/SearchPage.php
+++ b/core/modules/search/src/Entity/SearchPage.php
@@ -216,7 +216,7 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
     $a_status = (int) $a->status();
     $b_status = (int) $b->status();
     if ($a_status != $b_status) {
-      return ($a_status > $b_status) ? -1 : 1;
+      return $b_status <=> $a_status;
     }
     return parent::sort($a, $b);
   }
diff --git a/core/modules/shortcut/src/Entity/Shortcut.php b/core/modules/shortcut/src/Entity/Shortcut.php
index 0cca92e2d97a..7aeb4a6fa6ae 100644
--- a/core/modules/shortcut/src/Entity/Shortcut.php
+++ b/core/modules/shortcut/src/Entity/Shortcut.php
@@ -185,7 +185,7 @@ public static function sort(ShortcutInterface $a, ShortcutInterface $b) {
     if ($a_weight == $b_weight) {
       return strnatcasecmp($a->getTitle(), $b->getTitle());
     }
-    return ($a_weight < $b_weight) ? -1 : 1;
+    return $a_weight <=> $b_weight;
   }
 
 }
diff --git a/core/modules/tour/src/Entity/Tour.php b/core/modules/tour/src/Entity/Tour.php
index feca0585b010..032817a91293 100644
--- a/core/modules/tour/src/Entity/Tour.php
+++ b/core/modules/tour/src/Entity/Tour.php
@@ -123,10 +123,7 @@ public function getTips() {
       $tips[] = $this->getTip($id);
     }
     uasort($tips, function ($a, $b) {
-      if ($a->getWeight() == $b->getWeight()) {
-        return 0;
-      }
-      return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
+      return $a->getWeight() <=> $b->getWeight();
     });
 
     \Drupal::moduleHandler()->alter('tour_tips', $tips, $this);
diff --git a/core/modules/views/src/ViewsData.php b/core/modules/views/src/ViewsData.php
index af44e691bacf..bcbab7bce30b 100644
--- a/core/modules/views/src/ViewsData.php
+++ b/core/modules/views/src/ViewsData.php
@@ -304,12 +304,9 @@ public function fetchBaseTables() {
     // Sorts by the 'weight' and then by 'title' element.
     uasort($tables, function ($a, $b) {
       if ($a['weight'] != $b['weight']) {
-        return $a['weight'] < $b['weight'] ? -1 : 1;
+        return $a['weight'] <=> $b['weight'];
       }
-      if ($a['title'] != $b['title']) {
-        return $a['title'] < $b['title'] ? -1 : 1;
-      }
-      return 0;
+      return $a['title'] <=> $b['title'];
     });
 
     return $tables;
diff --git a/core/modules/views/src/ViewsDataHelper.php b/core/modules/views/src/ViewsDataHelper.php
index da4f7f1c3b57..c977882d9b47 100644
--- a/core/modules/views/src/ViewsDataHelper.php
+++ b/core/modules/views/src/ViewsDataHelper.php
@@ -176,16 +176,12 @@ protected static function fetchedFieldSort($a, $b) {
     $a_group = mb_strtolower($a['group']);
     $b_group = mb_strtolower($b['group']);
     if ($a_group != $b_group) {
-      return $a_group < $b_group ? -1 : 1;
+      return $a_group <=> $b_group;
     }
 
     $a_title = mb_strtolower($a['title']);
     $b_title = mb_strtolower($b['title']);
-    if ($a_title != $b_title) {
-      return $a_title < $b_title ? -1 : 1;
-    }
-
-    return 0;
+    return $a_title <=> $b_title;
   }
 
 }
diff --git a/core/modules/views/tests/src/Functional/ViewTestBase.php b/core/modules/views/tests/src/Functional/ViewTestBase.php
index c5fc8d75e223..beebadba2fe5 100644
--- a/core/modules/views/tests/src/Functional/ViewTestBase.php
+++ b/core/modules/views/tests/src/Functional/ViewTestBase.php
@@ -80,10 +80,7 @@ protected function enableViewsTestModule() {
   protected function orderResultSet($result_set, $column, $reverse = FALSE) {
     $order = $reverse ? -1 : 1;
     usort($result_set, function ($a, $b) use ($column, $order) {
-      if ($a[$column] == $b[$column]) {
-        return 0;
-      }
-      return $order * (($a[$column] < $b[$column]) ? -1 : 1);
+      return $order * ($a[$column] <=> $b[$column]);
     });
     return $result_set;
   }
diff --git a/core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php b/core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php
index 6d76bbd1501e..e3af4551378b 100644
--- a/core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php
+++ b/core/modules/views/tests/src/Kernel/ViewsKernelTestBase.php
@@ -108,10 +108,7 @@ protected function setUpFixtures() {
   protected function orderResultSet($result_set, $column, $reverse = FALSE) {
     $order = $reverse ? -1 : 1;
     usort($result_set, function ($a, $b) use ($column, $order) {
-      if ($a[$column] == $b[$column]) {
-        return 0;
-      }
-      return $order * (($a[$column] < $b[$column]) ? -1 : 1);
+      return $order * ($a[$column] <=> $b[$column]);
     });
     return $result_set;
   }
diff --git a/core/modules/views/views.views.inc b/core/modules/views/views.views.inc
index b7e9f8c392f9..7c236e68c4cb 100644
--- a/core/modules/views/views.views.inc
+++ b/core/modules/views/views.views.inc
@@ -280,7 +280,7 @@ function views_entity_field_label($entity_type, $field_name) {
     if ($label_counter[$a] === $label_counter[$b]) {
       return strcmp($a, $b);
     }
-    return $label_counter[$a] > $label_counter[$b] ? -1 : 1;
+    return $label_counter[$b] <=> $label_counter[$a];
   });
   $label_counter = array_keys($label_counter);
   return [$label_counter[0], $all_labels];
diff --git a/core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php b/core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php
index 38ed4e67e09f..695f478b21c9 100644
--- a/core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php
+++ b/core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php
@@ -52,10 +52,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
 
     // Sort the displays.
     uasort($displays, function ($display1, $display2) {
-      if ($display1['position'] != $display2['position']) {
-        return $display1['position'] < $display2['position'] ? -1 : 1;
-      }
-      return 0;
+      return $display1['position'] <=> $display2['position'];
     });
 
     $form['displays'] = [
-- 
GitLab