Skip to content

Fixed Drupal driver check.#532

Merged
AlexSkrypnyk merged 1 commit intomainfrom
feature/fix-drupal-driver-check
Mar 3, 2026
Merged

Fixed Drupal driver check.#532
AlexSkrypnyk merged 1 commit intomainfrom
feature/fix-drupal-driver-check

Conversation

@AlexSkrypnyk
Copy link
Member

@AlexSkrypnyk AlexSkrypnyk commented Mar 3, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation for Drupal-specific operations in media and paragraph handling to ensure they only run with a compatible Drupal driver, preventing runtime errors when a non-Drupal driver is in use.

@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2026

Walkthrough

Added runtime type guards requiring DrupalDriver for Drupal-specific methods in MediaTrait and ParagraphsTrait; replaced direct getCore() calls with driver-validated access and added the DrupalDriver imports. Methods now throw RuntimeException when the active driver is not DrupalDriver.

Changes

Cohort / File(s) Summary
Drupal trait guards
src/Drupal/MediaTrait.php, src/Drupal/ParagraphsTrait.php
Added DrupalDriver imports and runtime type checks that throw RuntimeException if the driver is not DrupalDriver; replaced direct $this->getDriver()->getCore() usage with a validated $driver = $this->getDriver(); $core = $driver->getCore(); pattern.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Fixed coding standards. #430 — Modifies the same methods (mediaExpandEntityFields, paragraphsExpandEntityFields) in MediaTrait and ParagraphsTrait; likely related changes.

Suggested labels

AUTOMERGE

Poem

🐰 I hop through code with cautious cheer,
A Drupal guard now stands right here,
Drivers checked before we call,
No surprise when things might fall.
Hooray — safe fields for one and all! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fixed Drupal driver check' accurately describes the main changes in the PR, which involve adding DrupalDriver type-checks and guards to restrict execution in two traits.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/fix-drupal-driver-check

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Drupal/MediaTrait.php (1)

228-240: ⚠️ Potential issue | 🔴 Critical

Type safety issue: DriverInterface does not guarantee getCore() method exists.

The code checks instanceof DriverInterface but then immediately calls $driver->getCore(), which only exists on the concrete \Drupal\Driver\DrupalDriver class. The interface check is insufficient—this will cause a fatal error at runtime if used with a driver that doesn't implement getCore().

Additionally, the validation pattern is duplicated in mediaExpandEntityFieldsFixtures (lines 260-266). Both issues should be addressed simultaneously:

  1. Change the type check to \Drupal\Driver\DrupalDriver instead of DriverInterface
  2. Extract the validation into a helper method to eliminate duplication
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Drupal/MediaTrait.php` around lines 228 - 240, The code uses instanceof
DriverInterface before calling getCore(), but DriverInterface doesn't guarantee
getCore(); replace the instanceof check with a concrete class check for
\Drupal\Driver\DrupalDriver (i.e., ensure $driver instanceof
\Drupal\Driver\DrupalDriver) and throw the same RuntimeException if not; to
remove duplication between mediaExpandEntityFields and
mediaExpandEntityFieldsFixtures extract the validation into a private helper
method (e.g., ensureDrupalDriverSupportsCore or validateDrupalDriver) that
accepts $driver, performs the instanceof \Drupal\Driver\DrupalDriver check and
returns the typed driver (or throws), then call that helper from both
mediaExpandEntityFields and mediaExpandEntityFieldsFixtures before calling
getCore()/Reflection.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/Drupal/MediaTrait.php`:
- Around line 228-240: The code uses instanceof DriverInterface before calling
getCore(), but DriverInterface doesn't guarantee getCore(); replace the
instanceof check with a concrete class check for \Drupal\Driver\DrupalDriver
(i.e., ensure $driver instanceof \Drupal\Driver\DrupalDriver) and throw the same
RuntimeException if not; to remove duplication between mediaExpandEntityFields
and mediaExpandEntityFieldsFixtures extract the validation into a private helper
method (e.g., ensureDrupalDriverSupportsCore or validateDrupalDriver) that
accepts $driver, performs the instanceof \Drupal\Driver\DrupalDriver check and
returns the typed driver (or throws), then call that helper from both
mediaExpandEntityFields and mediaExpandEntityFieldsFixtures before calling
getCore()/Reflection.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d072973 and 149c790.

📒 Files selected for processing (1)
  • src/Drupal/MediaTrait.php

@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.83%. Comparing base (d072973) to head (0b055b6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/Drupal/MediaTrait.php 75.00% 2 Missing ⚠️
src/Drupal/ParagraphsTrait.php 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #532      +/-   ##
==========================================
- Coverage   99.95%   99.83%   -0.13%     
==========================================
  Files          35       35              
  Lines        2356     2365       +9     
==========================================
+ Hits         2355     2361       +6     
- Misses          1        4       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexSkrypnyk AlexSkrypnyk force-pushed the feature/fix-drupal-driver-check branch from 149c790 to 0b055b6 Compare March 3, 2026 14:51
@AlexSkrypnyk AlexSkrypnyk enabled auto-merge (rebase) March 3, 2026 14:51
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/Drupal/MediaTrait.php`:
- Around line 228-235: Extract the duplicated driver/core guard into a single
helper on the trait (e.g., add a private method ensureDrupalDriver():
DrupalDriver or getDrupalCore(): CoreType) that performs $driver =
$this->getDriver(), asserts $driver instanceof DrupalDriver and throws the
RuntimeException if not, then returns the driver or core as needed; replace both
duplicated blocks that call $this->getDriver() and $driver->getCore() with a
single call to the new helper (use the helper name you add in MediaTrait) so all
Drupal-driver validation and core retrieval is centralized.

ℹ️ Review info

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 149c790 and 0b055b6.

📒 Files selected for processing (2)
  • src/Drupal/MediaTrait.php
  • src/Drupal/ParagraphsTrait.php

Comment on lines +228 to 235
$driver = $this->getDriver();

if (!$driver instanceof DrupalDriver) {
throw new \RuntimeException('The current driver does not support Drupal-specific operations. Ensure you are using a compatible Drupal driver.');
}

$core = $driver->getCore();

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Extract shared Drupal-driver/core guard to one helper.

The new guard is correct, but duplicated in two methods. Centralizing it will reduce maintenance drift.

♻️ Proposed refactor
 protected function mediaExpandEntityFields(string $entity_type, \StdClass $stub): void {
-    $driver = $this->getDriver();
-
-    if (!$driver instanceof DrupalDriver) {
-      throw new \RuntimeException('The current driver does not support Drupal-specific operations. Ensure you are using a compatible Drupal driver.');
-    }
-
-    $core = $driver->getCore();
+    $core = $this->mediaGetDrupalCore();

     $class = new \ReflectionClass($core::class);
     $method = $class->getMethod('expandEntityFields');

     $method->invokeArgs($core, func_get_args());
   }
@@
-    $driver = $this->getDriver();
-
-    if (!$driver instanceof DrupalDriver) {
-      throw new \RuntimeException('The current driver does not support Drupal-specific operations. Ensure you are using a compatible Drupal driver.');
-    }
-
-    $field_types = $driver->getCore()->getEntityFieldTypes('media', array_keys($fields));
+    $field_types = $this->mediaGetDrupalCore()->getEntityFieldTypes('media', array_keys($fields));
@@
   }
+
+  protected function mediaGetDrupalCore(): object {
+    $driver = $this->getDriver();
+    if (!$driver instanceof DrupalDriver) {
+      throw new \RuntimeException('The current driver does not support Drupal-specific operations. Ensure you are using a compatible Drupal driver.');
+    }
+    return $driver->getCore();
+  }

Also applies to: 260-267

🧰 Tools
🪛 PHPMD (2.15.0)

[error] 231-231: Missing class import via use statement (line '231', column '17'). (undefined)

(MissingImport)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Drupal/MediaTrait.php` around lines 228 - 235, Extract the duplicated
driver/core guard into a single helper on the trait (e.g., add a private method
ensureDrupalDriver(): DrupalDriver or getDrupalCore(): CoreType) that performs
$driver = $this->getDriver(), asserts $driver instanceof DrupalDriver and throws
the RuntimeException if not, then returns the driver or core as needed; replace
both duplicated blocks that call $this->getDriver() and $driver->getCore() with
a single call to the new helper (use the helper name you add in MediaTrait) so
all Drupal-driver validation and core retrieval is centralized.

@AlexSkrypnyk AlexSkrypnyk merged commit ca461bb into main Mar 3, 2026
5 checks passed
@AlexSkrypnyk AlexSkrypnyk deleted the feature/fix-drupal-driver-check branch March 3, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant