From c2f6edd656fad4a0df595404f4cfe199a53fe334 Mon Sep 17 00:00:00 2001
From: Chris McCafferty <cilefen@gmail.com>
Date: Thu, 6 Apr 2017 21:17:01 -0400
Subject: [PATCH] Issue #2867493 by xjm, alexpott, connectedrobots, webchick:
 Error: Call to a member function getTotalCount() on boolean in
 statistics_get()

---
 .../statistics/src/StatisticsStorageInterface.php    |  9 +++++++--
 .../statistics/src/Tests/StatisticsLoggingTest.php   | 12 ++++++++++++
 core/modules/statistics/statistics.module            |  7 +++++++
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php
index ccb51e44bc56..ad6d6d5dceb0 100644
--- a/core/modules/statistics/src/StatisticsStorageInterface.php
+++ b/core/modules/statistics/src/StatisticsStorageInterface.php
@@ -27,7 +27,10 @@ public function recordView($id);
    * @param array $ids
    *   An array of IDs of entities to fetch the views for.
    *
-   * @return array \Drupal\statistics\StatisticsViewsResult
+   * @return \Drupal\statistics\StatisticsViewsResult[]
+   *   An array of value objects representing the number of times each entity
+   *   has been viewed. The array is keyed by entity ID. If an ID does not
+   *   exist, it will not be present in the array.
    */
   public function fetchViews($ids);
 
@@ -37,7 +40,9 @@ public function fetchViews($ids);
    * @param int $id
    *   The ID of the entity to fetch the views for.
    *
-   * @return \Drupal\statistics\StatisticsViewsResult
+   * @return \Drupal\statistics\StatisticsViewsResult|false
+   *   If the entity exists, a value object representing the number of times if
+   *   has been viewed. If it does not exist, FALSE is returned.
    */
   public function fetchView($id);
 
diff --git a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
index 152c95ae60dd..6f2f66766a12 100644
--- a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
+++ b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php
@@ -3,6 +3,7 @@
 namespace Drupal\statistics\Tests;
 
 use Drupal\simpletest\WebTestBase;
+use Drupal\node\Entity\Node;
 
 /**
  * Tests request logging for cached and uncached pages.
@@ -125,6 +126,17 @@ public function testLogging() {
     $this->client->post($base_root . $stats_path, ['form_params' => $post]);
     $node_counter = statistics_get($this->node->id());
     $this->assertIdentical($node_counter['totalcount'], '1');
+
+    // Try fetching statistics for an invalid node ID and verify it returns
+    // FALSE.
+    $node_id = 10000000000000000;
+    $node = Node::load($node_id);
+    $this->assertNull($node);
+
+    // This is a test specifically for the deprecated statistics_get() function
+    // and so should remain unconverted until that function is removed.
+    $result = statistics_get($node_id);
+    $this->assertIdentical($result, FALSE);
   }
 
 }
diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module
index a7f1e10601ea..4d7f42a33c72 100644
--- a/core/modules/statistics/statistics.module
+++ b/core/modules/statistics/statistics.module
@@ -10,6 +10,7 @@
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Url;
 use Drupal\node\NodeInterface;
+use Drupal\statistics\StatisticsViewsResult;
 
 /**
  * Implements hook_help().
@@ -125,6 +126,12 @@ function statistics_get($id) {
   if ($id > 0) {
     /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
     $statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
+
+    // For backwards compatibility, return FALSE if an invalid node ID was
+    // passed in.
+    if (!($statistics instanceof StatisticsViewsResult)) {
+      return FALSE;
+    }
     return [
       'totalcount' => $statistics->getTotalCount(),
       'daycount' => $statistics->getDayCount(),
-- 
GitLab