Skip to content

Refactored 'FileTrait' to separate file source properties from entity fields.#545

Merged
AlexSkrypnyk merged 1 commit intomainfrom
feature/refactor-file-trait
Mar 18, 2026
Merged

Refactored 'FileTrait' to separate file source properties from entity fields.#545
AlexSkrypnyk merged 1 commit intomainfrom
feature/refactor-file-trait

Conversation

@AlexSkrypnyk
Copy link
Copy Markdown
Member

@AlexSkrypnyk AlexSkrypnyk commented Mar 18, 2026

Summary

  • Extracted 'path' and 'uri' from the table hash in 'fileCreateManaged()' before creating the entity stub, so the stub only contains real Drupal entity fields.
  • Added required column validation for 'path' (throws 'RuntimeException' if missing), following the same pattern as 'UserTrait::userCreateRoles()'.
  • Changed 'fileCreateManagedSingle()' and 'fileCreateEntity()' signatures to accept 'path' and 'uri' as explicit typed arguments instead of mixing them into the entity stub.
  • Removed the temporary unset/restore workaround for 'parseEntityFields()' validation.
  • Removed the 'in_array' skip for 'path' and 'uri' in the entity property loop since those properties are no longer on the stub.

Test plan

  • All unit tests pass.
  • All BDD tests pass (managed file creation with path, uri, subdirectory path, and additional entity fields).

Summary by CodeRabbit

  • Refactor
    • Enhanced file creation process with more explicit path and destination handling for improved flexibility.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 18, 2026

Walkthrough

Refactored managed file creation methods in FileTrait to accept explicit path and optional URI parameters instead of embedding them in the stub object. Method signatures updated for fileCreateManagedSingle and fileCreateEntity to handle path and URI as explicit parameters, shifting logic away from implicit stub property reliance.

Changes

Cohort / File(s) Summary
File Creation Refactoring
src/Drupal/FileTrait.php
Updated method signatures for fileCreateManagedSingle and fileCreateEntity to accept explicit string $path and ?string $uri parameters. Modified internal logic to use provided path/uri instead of relying on stub properties. Default destination set to public://basename($path) when URI not provided. Removed in-stub path/uri filtering. Updated docblocks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • skipper-vp

Poem

🐰 Files now hop with paths so clear,
No hidden URIs to fear,
Parameters explicit, logic refined,
A cleaner creation flow we find!

🚥 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 accurately reflects the main refactoring change: separating file source properties (path and uri) from entity fields in FileTrait.
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
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor-file-trait
📝 Coding Plan
  • Generate coding plan for human review comments

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

Copy link
Copy Markdown

@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/FileTrait.php`:
- Around line 150-153: The code in FileTrait uses PHP's native
dirname($destination) for Drupal stream-wrapper URIs which can be
wrapper-unsafe; replace that call with Drupal's filesystem dirname helper (i.e.
call \Drupal::service('file_system')->dirname($destination) or use the injected
FileSystemInterface->dirname) so $directory is computed via the Drupal file
system before calling prepareDirectory (the variables $destination and the
prepareDirectory call should be left intact, only swap native dirname for the
filesystem dirname helper).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e7587b2c-2c92-43a3-8eba-dd1fa9d97075

📥 Commits

Reviewing files that changed from the base of the PR and between 0eba556 and 83754b8.

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

Comment on lines +150 to 153
if (!empty($uri)) {
$destination = $uri;
$directory = dirname($destination);
$dir = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

find . -name "FileTrait.php" -path "*/src/Drupal/*" -type f

Repository: drevops/behat-steps

Length of output: 89


🏁 Script executed:

# Get the file size first
wc -l src/Drupal/FileTrait.php 2>/dev/null || echo "File not found at that path"

Repository: drevops/behat-steps

Length of output: 91


🏁 Script executed:

# Search for FileTrait.php in the repository
fd -t f "FileTrait.php"

Repository: drevops/behat-steps

Length of output: 87


🏁 Script executed:

sed -n '145,160p' src/Drupal/FileTrait.php

Repository: drevops/behat-steps

Length of output: 694


🏁 Script executed:

# Check if Drupal file_system service has a dirname method
grep -r "dirname" src/Drupal/FileTrait.php -B 3 -A 3

Repository: drevops/behat-steps

Length of output: 699


🏁 Script executed:

# Search for any use of file_system->dirname in the codebase
rg "file_system.*dirname|dirname.*file_system" -B 2 -A 2

Repository: drevops/behat-steps

Length of output: 426


Use Drupal filesystem dirname for stream-wrapper URIs.

Line 152 uses native dirname() on a Drupal URI. For values like public://..., this can be wrapper-unsafe depending on PHP path parsing. Use Drupal's file system dirname helper for consistency and correctness. The same trait demonstrates the correct approach elsewhere.

Proposed fix
-      $directory = dirname($destination);
+      $directory = \Drupal::service('file_system')->dirname($destination);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!empty($uri)) {
$destination = $uri;
$directory = dirname($destination);
$dir = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS);
if (!empty($uri)) {
$destination = $uri;
$directory = \Drupal::service('file_system')->dirname($destination);
$dir = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Drupal/FileTrait.php` around lines 150 - 153, The code in FileTrait uses
PHP's native dirname($destination) for Drupal stream-wrapper URIs which can be
wrapper-unsafe; replace that call with Drupal's filesystem dirname helper (i.e.
call \Drupal::service('file_system')->dirname($destination) or use the injected
FileSystemInterface->dirname) so $directory is computed via the Drupal file
system before calling prepareDirectory (the variables $destination and the
prepareDirectory call should be left intact, only swap native dirname for the
filesystem dirname helper).

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.79%. Comparing base (2a78c58) to head (83754b8).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/Drupal/FileTrait.php 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #545      +/-   ##
==========================================
- Coverage   99.83%   99.79%   -0.05%     
==========================================
  Files          35       35              
  Lines        2385     2382       -3     
==========================================
- Hits         2381     2377       -4     
- Misses          4        5       +1     

☔ 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 merged commit 9fc213d into main Mar 18, 2026
6 checks passed
@AlexSkrypnyk AlexSkrypnyk deleted the feature/refactor-file-trait branch March 18, 2026 08:33
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