Refactored 'FileTrait' to separate file source properties from entity fields.#545
Refactored 'FileTrait' to separate file source properties from entity fields.#545AlexSkrypnyk merged 1 commit intomainfrom
Conversation
WalkthroughRefactored 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 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
src/Drupal/FileTrait.php
| if (!empty($uri)) { | ||
| $destination = $uri; | ||
| $directory = dirname($destination); | ||
| $dir = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
find . -name "FileTrait.php" -path "*/src/Drupal/*" -type fRepository: 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.phpRepository: 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 3Repository: 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 2Repository: 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.
| 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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Summary
Test plan
Summary by CodeRabbit