class_id is passed as object instead of int to Formatter::fullClassName

Created on 8 May 2023, almost 2 years ago
Updated 10 May 2023, almost 2 years ago

Problem/Motivation

Another contrib parsing issue - a few different places in advagg . $class_id is passed as a docblock object instead of as NULL/[INT] which eventually leads to an object being used in a cache ID which fails hard.

Error: Object of class Drupal\api\Entity\DocBlock could not be converted to string in Drupal\Core\Entity\EntityStorageBase->buildCacheId() (line 142 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

Drupal\Core\Entity\EntityStorageBase->getFromStaticCache(Array) (Line: 282)
Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 262)
Drupal\Core\Entity\EntityStorageBase->load(Object) (Line: 488)
Drupal\Core\Entity\EntityBase::load(Object) (Line: 658)
Drupal\api\Formatter::fullClassname('this', '', Array, Object) (Line: 1815)
Drupal\api\Formatter::linkName('this', Object, ''', ''', '493', Object, 'this', , , '', '', 'function')
call_user_func_array('\Drupal\api\Formatter::linkName', Array) (Line: 1302)
Drupal\api\Formatter::processPattern [rest of long error trimmed]

Steps to reproduce

Add advagg 7.x-1.x (similar path is available in all versions and has the same problem)
api/advagg/advagg_js_compress!jsminplus.inc/property/JSTokenizer::keywords/7.x-1.x

Proposed resolution

This change *resolves* the issue, but I'm not convinced it is actually the best resolution and it is possible this should be caught earlier.

diff --git a/src/Formatter.php b/src/Formatter.php
index a24d4ef..822972b 100644
--- a/src/Formatter.php
+++ b/src/Formatter.php
@@ -1807,6 +1812,9 @@ class Formatter {
     // Get some information we will need in several places below.
     $name_info = self::getNamespaceInfo($docblock_id);
     // NOTE FOR D9: special case with $class_id.
+    if (is_object($class_id)) {
+      $class_id = $class_id->id();
+    }
     $namespaced_name = self::fullClassname($name, $name_info['namespace'], $name_info['use_alias'], $class_id);
     $core_compatibility = ($branch) ?
       $branch->getCoreCompatibility() :

Remaining tasks

Review/determine if there is a better code update location.

Longer term/bigger change, might be helpful to enforce type hints in some cases to provide more useful error message at the point of the problem, not once it is into the cache process.

🐛 Bug report
Status

Fixed

Version

2.0

Component

Parser

Created by

🇨🇦Canada nickdickinsonwilde Victoria, BC (T'So-uke lands)

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024