From e00c523fb40bdafc55e0477dbe15f3e307438c95 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 8 Nov 2022 15:48:48 +0000
Subject: [PATCH] Issue #3317651 by mondrake, longwave, alexpott: Fix
 DataReferenceBase PHPStan L0 issues

---
 .../lib/Drupal/Core/TypedData/DataReferenceBase.php |  3 +++
 core/phpstan-baseline.neon                          |  5 -----
 .../Core/TypedData/TypedDataDefinitionTest.php      | 13 +++++++++++++
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/core/lib/Drupal/Core/TypedData/DataReferenceBase.php b/core/lib/Drupal/Core/TypedData/DataReferenceBase.php
index a41b01c65df6..2e0cbc524a33 100644
--- a/core/lib/Drupal/Core/TypedData/DataReferenceBase.php
+++ b/core/lib/Drupal/Core/TypedData/DataReferenceBase.php
@@ -54,6 +54,9 @@ public function setValue($value, $notify = TRUE) {
    * {@inheritdoc}
    */
   public function getString() {
+    if (!method_exists($this, 'getType')) {
+      throw new \BadMethodCallException(get_class($this) . '::getType() not implemented');
+    }
     return (string) $this->getType() . ':' . $this->getTargetIdentifier();
   }
 
diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index 0a143fbb4c73..4088afbe5fb0 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -820,11 +820,6 @@ parameters:
 			count: 1
 			path: lib/Drupal/Core/Theme/ThemeManager.php
 
-		-
-			message: "#^Call to an undefined method Drupal\\\\Core\\\\TypedData\\\\DataReferenceBase\\:\\:getType\\(\\)\\.$#"
-			count: 1
-			path: lib/Drupal/Core/TypedData/DataReferenceBase.php
-
 		-
 			message: "#^Method Drupal\\\\Core\\\\TypedData\\\\ListDataDefinition\\:\\:setDataType\\(\\) should return static\\(Drupal\\\\Core\\\\TypedData\\\\ListDataDefinition\\) but return statement is missing\\.$#"
 			count: 1
diff --git a/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataDefinitionTest.php b/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataDefinitionTest.php
index f1a62beefbd3..434d5578ecfc 100644
--- a/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataDefinitionTest.php
+++ b/core/tests/Drupal/KernelTests/Core/TypedData/TypedDataDefinitionTest.php
@@ -7,6 +7,7 @@
 use Drupal\Core\TypedData\DataDefinitionInterface;
 use Drupal\Core\TypedData\DataReferenceDefinition;
 use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
+use Drupal\Core\TypedData\DataReferenceInterface;
 use Drupal\Core\TypedData\ListDataDefinition;
 use Drupal\Core\TypedData\ListDataDefinitionInterface;
 use Drupal\Core\TypedData\MapDataDefinition;
@@ -99,4 +100,16 @@ public function testDataReferences() {
     $this->assertEquals(serialize($language_reference_definition2), serialize($language_reference_definition));
   }
 
+  /**
+   * Tests getString() throws exception when getType() is not implemented.
+   */
+  public function testNotImplementedGetType() {
+    $language_reference_definition = DataReferenceDefinition::create('language');
+    $language_reference = $this->typedDataManager->create($language_reference_definition);
+    $this->assertInstanceOf(DataReferenceInterface::class, $language_reference);
+    $this->expectException(\BadMethodCallException::class);
+    $this->expectExceptionMessageMatches('/getType\(\) not implemented/');
+    $language_reference->getString();
+  }
+
 }
-- 
GitLab