From 7de8fc1fda16072510b415ef62cebd78bad3a211 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 10 Jul 2021 23:37:45 +0100
Subject: [PATCH] Issue #3215836 by guilhermevp, imalabya, swatichouhan012,
 AJV009, joachim, quietone: add a constant to represent uncountable sources
 for SourcePluginBase::count()

---
 .../migrate/src/Plugin/MigrateSourceInterface.php  |  5 +++++
 .../src/Plugin/migrate/source/SourcePluginBase.php | 14 ++++++++------
 .../migrate/tests/src/Unit/MigrateSourceTest.php   |  5 +++--
 .../src/Plugin/migrate/source/ContentEntity.php    |  3 ++-
 4 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/core/modules/migrate/src/Plugin/MigrateSourceInterface.php b/core/modules/migrate/src/Plugin/MigrateSourceInterface.php
index 1afa032c8ea5..f33dc68cbc80 100644
--- a/core/modules/migrate/src/Plugin/MigrateSourceInterface.php
+++ b/core/modules/migrate/src/Plugin/MigrateSourceInterface.php
@@ -17,6 +17,11 @@
  */
 interface MigrateSourceInterface extends \Countable, \Iterator, PluginInspectionInterface {
 
+  /**
+   * Indicates that the source is not countable.
+   */
+  const NOT_COUNTABLE = -1;
+
   /**
    * Returns available fields on the source.
    *
diff --git a/core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php b/core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php
index 3825a01ce119..ffcfe894d5a7 100644
--- a/core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php
+++ b/core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php
@@ -85,10 +85,11 @@
  * @endcode
  *
  * In this example, skip_count is true which means count() will not attempt to
- * count the available source records, but just always return -1 instead. The
- * high_water_property defines which field marks the last imported row of the
- * migration. This will get converted into a SQL condition that looks like
- * 'n.changed' or 'changed' if no alias.
+ * count the available source records, but just always return
+ * MigrateSourceInterface::NOT_COUNTABLE instead. The high_water_property
+ * defines which field marks the last imported row of the migration. This will
+ * get converted into a SQL condition that looks like 'n.changed' or 'changed'
+ * if no alias.
  *
  * Example:
  *
@@ -473,7 +474,8 @@ public function getCurrentIds() {
    * Gets the source count.
    *
    * Return a count of available source records, from the cache if appropriate.
-   * Returns -1 if the source is not countable.
+   * Returns MigrateSourceInterface::NOT_COUNTABLE if the source is not
+   * countable.
    *
    * @param bool $refresh
    *   (optional) Whether or not to refresh the count. Defaults to FALSE. Not
@@ -486,7 +488,7 @@ public function getCurrentIds() {
    */
   public function count($refresh = FALSE) {
     if ($this->skipCount) {
-      return -1;
+      return MigrateSourceInterface::NOT_COUNTABLE;
     }
 
     // Return the cached count if we are caching counts and a refresh is not
diff --git a/core/modules/migrate/tests/src/Unit/MigrateSourceTest.php b/core/modules/migrate/tests/src/Unit/MigrateSourceTest.php
index f7579e658bc0..0484ac0d2eaf 100644
--- a/core/modules/migrate/tests/src/Unit/MigrateSourceTest.php
+++ b/core/modules/migrate/tests/src/Unit/MigrateSourceTest.php
@@ -17,6 +17,7 @@
 use Drupal\migrate\MigrateSkipRowException;
 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
 use Drupal\migrate\Plugin\MigrateIdMapInterface;
+use Drupal\migrate\Plugin\MigrateSourceInterface;
 use Drupal\migrate\Row;
 
 /**
@@ -186,14 +187,14 @@ public function testCount() {
 
     // Test the skip argument.
     $source = $this->getSource(['skip_count' => TRUE]);
-    $this->assertEquals(-1, $source->count());
+    $this->assertEquals(MigrateSourceInterface::NOT_COUNTABLE, $source->count());
 
     $this->migrationConfiguration['id'] = 'test_migration';
     $migration = $this->getMigration();
     $source = new StubSourceGeneratorPlugin([], '', [], $migration);
 
     // Test the skipCount property's default value.
-    $this->assertEquals(-1, $source->count());
+    $this->assertEquals(MigrateSourceInterface::NOT_COUNTABLE, $source->count());
 
     // Test the count value using a generator.
     $source = new StubSourceGeneratorPlugin(['skip_count' => FALSE], '', [], $migration);
diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php
index aaca7788da75..28f20a7bcb4f 100644
--- a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php
+++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php
@@ -11,6 +11,7 @@
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\migrate\EntityFieldDefinitionTrait;
 use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
+use Drupal\migrate\Plugin\MigrateSourceInterface;
 use Drupal\migrate\Plugin\MigrationInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -248,7 +249,7 @@ public function count($refresh = FALSE) {
     }
     // @TODO: Determine a better way to retrieve a valid count for translations.
     // https://www.drupal.org/project/drupal/issues/2937166
-    return -1;
+    return MigrateSourceInterface::NOT_COUNTABLE;
   }
 
   /**
-- 
GitLab