From 19eceb3623541565edf0cb8655e66ef9fd2fa881 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 13 Sep 2018 11:19:13 -0400
Subject: [PATCH] Simplify search backend code with null coalescing.

---
 .../BrowZine/Response/RecordCollection.php    |  6 ++--
 .../src/VuFindSearch/Backend/EDS/Backend.php  | 32 +++++++------------
 .../Backend/EDS/Response/RecordCollection.php | 17 ++--------
 .../EDS/Response/RecordCollectionFactory.php  | 15 +++------
 .../EIT/Response/XML/RecordCollection.php     |  4 +--
 .../LibGuides/Response/RecordCollection.php   |  6 ++--
 .../VuFindSearch/Backend/Pazpar2/Backend.php  |  2 +-
 .../Primo/Response/RecordCollection.php       |  9 ++----
 .../Backend/Solr/Response/Json/Record.php     |  2 +-
 .../Solr/Response/Json/RecordCollection.php   | 12 +++----
 .../Summon/Response/RecordCollection.php      | 26 +++++----------
 .../Backend/WorldCat/Connector.php            |  2 +-
 .../src/VuFindSearch/ParamBag.php             |  2 +-
 .../Response/AbstractRecordCollection.php     |  2 +-
 14 files changed, 44 insertions(+), 93 deletions(-)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollection.php
index 533b90b89d6..0e2dad1b952 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/BrowZine/Response/RecordCollection.php
@@ -68,8 +68,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        return isset($this->response['recordCount'])
-            ? $this->response['recordCount'] : 0;
+        return $this->response['recordCount'] ?? 0;
     }
 
     /**
@@ -90,7 +89,6 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getOffset()
     {
-        return isset($this->response['offset'])
-            ? $this->response['offset'] : 0;
+        return $this->response['offset'] ?? 0;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
index e22d9da2312..a0d643fbbd3 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
@@ -70,21 +70,21 @@ class Backend extends AbstractBackend
      *
      * @var string
      */
-    protected $userName = null;
+    protected $userName;
 
     /**
      * Password for EBSCO EDS API account if using UID Authentication
      *
      * @var string
      */
-    protected $password = null;
+    protected $password;
 
     /**
      * Profile for EBSCO EDS API account (may be overridden)
      *
      * @var string
      */
-    protected $profile = null;
+    protected $profile;
 
     /**
      * Default profile for EBSCO EDS API account (taken from initial config and
@@ -92,21 +92,21 @@ class Backend extends AbstractBackend
      *
      * @var string
      */
-    protected $defaultProfile = null;
+    protected $defaultProfile;
 
     /**
      * Whether or not to use IP Authentication for communication with the EDS API
      *
      * @var bool
      */
-    protected $ipAuth = false;
+    protected $ipAuth;
 
     /**
      * Organization EDS API requests are being made for
      *
      * @var string
      */
-    protected $orgId = null;
+    protected $orgId;
 
     /**
      * Vufind Authentication manager
@@ -158,21 +158,11 @@ class Backend extends AbstractBackend
         $this->isGuest = $isGuest;
 
         // Extract key values from configuration:
-        if (isset($config->EBSCO_Account->user_name)) {
-            $this->userName = $config->EBSCO_Account->user_name;
-        }
-        if (isset($config->EBSCO_Account->password)) {
-            $this->password = $config->EBSCO_Account->password;
-        }
-        if (isset($config->EBSCO_Account->ip_auth)) {
-            $this->ipAuth = $config->EBSCO_Account->ip_auth;
-        }
-        if (isset($config->EBSCO_Account->profile)) {
-            $this->profile = $config->EBSCO_Account->profile;
-        }
-        if (isset($config->EBSCO_Account->organization_id)) {
-            $this->orgId = $config->EBSCO_Account->organization_id;
-        }
+        $this->userName = $config->EBSCO_Account->user_name ?? null;
+        $this->password = $config->EBSCO_Account->password ?? null;
+        $this->ipAuth = $config->EBSCO_Account->ip_auth ?? false;
+        $this->profile = $config->EBSCO_Account->profile ?? null;
+        $this->orgId = $config->EBSCO_Account->organization_id ?? null;
 
         // Save default profile value, since profile property may be overriden:
         $this->defaultProfile = $this->profile;
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollection.php
index 1bcf50be8b4..778455ef588 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollection.php
@@ -68,14 +68,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        $totalHits = 0;
-        if (isset($this->response['SearchResult'])
-            && isset($this->response['SearchResult']['Statistics'])
-            && isset($this->response['SearchResult']['Statistics']['TotalHits'])
-        ) {
-            $totalHits = $this->response['SearchResult']['Statistics']['TotalHits'];
-        }
-        return $totalHits;
+        return $this->response['SearchResult']['Statistics']['TotalHits'] ?? 0;
     }
 
     /**
@@ -85,9 +78,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getRawFacets()
     {
-        return isset($this->response['SearchResult'])
-            && isset($this->response['SearchResult']['AvailableFacets'])
-            ? $this->response['SearchResult']['AvailableFacets'] : [];
+        return $this->response['SearchResult']['AvailableFacets'] ?? [];
     }
 
     /**
@@ -98,9 +89,7 @@ class RecordCollection extends AbstractRecordCollection
     public function getFacets()
     {
         $vufindFacetList = [];
-        $facets = isset($this->response['SearchResult'])
-            && isset($this->response['SearchResult']['AvailableFacets'])
-            ? $this->response['SearchResult']['AvailableFacets'] : [];
+        $facets = $this->response['SearchResult']['AvailableFacets'] ?? [];
         foreach ($facets as $facet) {
             $vufindFacet['displayName'] = $facet['Id'];
             $vufindFacet['displayText'] = $facet['Label'];
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php
index 17ecf8d0992..577e8558e53 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Response/RecordCollectionFactory.php
@@ -92,17 +92,10 @@ class RecordCollectionFactory implements RecordCollectionFactoryInterface
             );
         }
         $collection = new $this->collectionClass($response);
-        //obtain path to records
-        $records = [];
-        if (isset($response['SearchResult'])
-            && isset($response['SearchResult']['Data'])
-            && isset($response['SearchResult']['Data']['Records'])
-        ) {
-            // Format of the search response
-            $records = $response['SearchResult']['Data']['Records'];
-        } elseif (isset($response['Records'])) { // Format of the retrieve response
-            $records = $response['Records'];
-        }
+        // Obtain path to records -- first try format of the search response,
+        // then try format of the retrieve response, then give up with empty array.
+        $records = $response['SearchResult']['Data']['Records']
+            ?? $response['Records'] ?? [];
 
         foreach ($records as $record) {
             $collection->add(call_user_func($this->recordFactory, $record));
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollection.php
index 817462b1189..61afca4f70d 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EIT/Response/XML/RecordCollection.php
@@ -70,7 +70,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        return isset($this->response['total']) ? $this->response['total'] : 0;
+        return $this->response['total'] ?? 0;
     }
 
     /**
@@ -90,6 +90,6 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getOffset()
     {
-        return isset($this->response['offset']) ? $this->response['offset'] : 0;
+        return $this->response['offset'] ?? 0;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollection.php
index 247c6c04516..e95c61f4ea5 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Response/RecordCollection.php
@@ -68,8 +68,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        return isset($this->response['recordCount'])
-            ? $this->response['recordCount'] : 0;
+        return $this->response['recordCount'] ?? 0;
     }
 
     /**
@@ -90,7 +89,6 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getOffset()
     {
-        return isset($this->response['offset'])
-            ? $this->response['offset'] : 0;
+        return $this->response['offset'] ?? 0;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Backend.php b/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Backend.php
index 61f782743ca..9b84ab2c70a 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Backend.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Pazpar2/Backend.php
@@ -160,7 +160,7 @@ class Backend extends AbstractBackend
         );
         $response = $this->connector->show($showParams);
 
-        $hits = isset($response->hit) ? $response->hit : [];
+        $hits = $response->hit ?? [];
         $collection = $this->createRecordCollection(
             $hits, intval($response->merged), $offset
         );
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
index 7fa90f450c4..58ca3626496 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Response/RecordCollection.php
@@ -68,8 +68,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        return isset($this->response['recordCount'])
-            ? $this->response['recordCount'] : 0;
+        return $this->response['recordCount'] ?? 0;
     }
 
     /**
@@ -79,8 +78,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getFacets()
     {
-        return isset($this->response['facets'])
-            ? $this->response['facets'] : [];
+        return $this->response['facets'] ?? [];
     }
 
     /**
@@ -92,8 +90,7 @@ class RecordCollection extends AbstractRecordCollection
     {
         $page = isset($this->response['query']['pageNumber'])
             ? $this->response['query']['pageNumber'] - 1 : 0;
-        $size = isset($this->response['query']['pageSize'])
-            ? $this->response['query']['pageSize'] : 0;
+        $size = $this->response['query']['pageSize'] ?? 0;
         return $page * $size;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/Record.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/Record.php
index c7031a42fbb..08554072854 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/Record.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/Record.php
@@ -101,6 +101,6 @@ class Record implements RecordInterface
      */
     public function __get($name)
     {
-        return isset($this->fields[$name]) ? $this->fields[$name] : null;
+        return $this->fields[$name] ?? null;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php
index bebfe7a7bac..7ab8c7ae4e7 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/Response/Json/RecordCollection.php
@@ -135,8 +135,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getGroups()
     {
-        return isset($this->response['grouped'])
-            ? $this->response['grouped'] : [];
+        return $this->response['grouped'] ?? [];
     }
 
     /**
@@ -146,8 +145,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getHighlighting()
     {
-        return isset($this->response['highlighting'])
-            ? $this->response['highlighting'] : [];
+        return $this->response['highlighting'] ?? [];
     }
 
     /**
@@ -157,8 +155,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     protected function getSolrParameters()
     {
-        return isset($this->response['responseHeader']['params'])
-            ? $this->response['responseHeader']['params'] : [];
+        return $this->response['responseHeader']['params'] ?? [];
     }
 
     /**
@@ -179,7 +176,6 @@ class RecordCollection extends AbstractRecordCollection
      */
     protected function getRawSpellcheckSuggestions()
     {
-        return isset($this->response['spellcheck']['suggestions'])
-            ? $this->response['spellcheck']['suggestions'] : [];
+        return $this->response['spellcheck']['suggestions'] ?? [];
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollection.php
index 65d0598260f..4f2e0716361 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Summon/Response/RecordCollection.php
@@ -62,8 +62,7 @@ class RecordCollection extends AbstractRecordCollection
         // Determine the offset:
         $page = isset($this->response['query']['pageNumber'])
             ? $this->response['query']['pageNumber'] - 1 : 0;
-        $size = isset($this->response['query']['pageSize'])
-            ? $this->response['query']['pageSize'] : 0;
+        $size = $this->response['query']['pageSize'] ?? 0;
         $this->offset = $page * $size;
 
         $this->rewind();
@@ -76,8 +75,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTotal()
     {
-        return isset($this->response['recordCount'])
-            ? $this->response['recordCount'] : 0;
+        return $this->response['recordCount'] ?? 0;
     }
 
     /**
@@ -87,8 +85,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getFacets()
     {
-        return isset($this->response['facetFields'])
-            ? $this->response['facetFields'] : [];
+        return $this->response['facetFields'] ?? [];
     }
 
     /**
@@ -98,12 +95,8 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getSpellcheck()
     {
-        if (isset($this->response['didYouMeanSuggestions'])
-            && is_array($this->response['didYouMeanSuggestions'])
-        ) {
-            return $this->response['didYouMeanSuggestions'];
-        }
-        return [];
+        return is_array($this->response['didYouMeanSuggestions'] ?? null)
+            ? $this->response['didYouMeanSuggestions'] : [];
     }
 
     /**
@@ -113,8 +106,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getBestBets()
     {
-        return isset($this->response['recommendationLists']['bestBet'])
-            ? $this->response['recommendationLists']['bestBet'] : false;
+        return $this->response['recommendationLists']['bestBet'] ?? false;
     }
 
     /**
@@ -124,8 +116,7 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getDatabaseRecommendations()
     {
-        return isset($this->response['recommendationLists']['database'])
-            ? $this->response['recommendationLists']['database'] : false;
+        return $this->response['recommendationLists']['database'] ?? false;
     }
 
     /**
@@ -135,7 +126,6 @@ class RecordCollection extends AbstractRecordCollection
      */
     public function getTopicRecommendations()
     {
-        return isset($this->response['topicRecommendations'])
-            ? $this->response['topicRecommendations'] : false;
+        return $this->response['topicRecommendations'] ?? false;
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
index 13c9eed6c79..46d47bccaa7 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
@@ -156,7 +156,7 @@ class Connector extends \VuFindSearch\Backend\SRU\Connector
         $response = $this->call('POST', $params->getArrayCopy(), false);
 
         $xml = simplexml_load_string($response);
-        $docs = isset($xml->records->record) ? $xml->records->record : [];
+        $docs = $xml->records->record ?? [];
         $finalDocs = [];
         foreach ($docs as $doc) {
             $finalDocs[] = $doc->recordData->asXML();
diff --git a/module/VuFindSearch/src/VuFindSearch/ParamBag.php b/module/VuFindSearch/src/VuFindSearch/ParamBag.php
index 8aed52dcda7..8921caf2a12 100644
--- a/module/VuFindSearch/src/VuFindSearch/ParamBag.php
+++ b/module/VuFindSearch/src/VuFindSearch/ParamBag.php
@@ -73,7 +73,7 @@ class ParamBag implements \Countable
      */
     public function get($name)
     {
-        return isset($this->params[$name]) ? $this->params[$name] : null;
+        return $this->params[$name] ?? null;
     }
 
     /**
diff --git a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
index b8c48657f1b..45b97353382 100644
--- a/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
+++ b/module/VuFindSearch/src/VuFindSearch/Response/AbstractRecordCollection.php
@@ -94,7 +94,7 @@ abstract class AbstractRecordCollection implements RecordCollectionInterface
      */
     public function first()
     {
-        return isset($this->records[0]) ? $this->records[0] : null;
+        return $this->records[0] ?? null;
     }
 
     /**
-- 
GitLab