Skip to content

[access] refactor(files): split FileService validation, folder resolution and write into named steps - #1073

Open
rubenvdlinde wants to merge 2 commits into
developmentfrom
quality/i40-fileservice-steps
Open

rubenvdlinde wants to merge 2 commits into
developmentfrom
quality/i40-fileservice-steps

Conversation

@rubenvdlinde

Copy link
Copy Markdown
Contributor

What changed

FileService (the link-create file writer) reads as named steps:

  • getAllowedExtensions and setAllowedExtensions share one normaliseExtensions / normaliseExtension pair instead of two copies of the same normalisation loop.
  • assertValidFilename checks the length, then containsAny for the forbidden substrings, then the pattern; every refusal is the same exception as before.
  • resolveTargetFolder keeps the user-folder shortcut and hands subdirectories to folderAt (existingFolder, createFolder) with the same three storage-failure messages.
  • writeFile (signature unchanged) uses assertMayReplace and overwrite; the refuse-mode exception still passes through unwrapped and every other failure is still "Failed to write file".

Allowed and refused outcomes are unchanged, and every check still runs before storage is touched, in the original order (filename, directory, extension, user folder, target folder, write).

Characterisation tests

tests/Unit/Service/FileServiceGuardGoldenTest.php was committed first, against the old code, and passes before and after. It pins the allowed writes (longest allowed name, root and nested directories, existing and created subfolders, replace mode) and the refused ones (each filename check, each directory check, filename-before-directory precedence, extension off the list, refuse mode) with no storage access on a refusal, every storage-failure message, and the allow-list normalisation on read and on save. Mutation check: disabling one filename check on the old code turned the golden red; restored before committing.

What ran

  • vendor/bin/phpunit -c phpunit.xml --no-coverage tests/Unit: 2779 tests OK (13 skipped, as on development).
  • vendor/bin/phpcs and vendor/bin/phpstan analyse on lib/Service/FileService.php: clean.

Figures (touched file lib/Service/FileService.php)

before after
units over complexity 5 5 units, 125 lines 0
units over 15 lines of code 6 units, 158 lines 1 unit, 33 lines (createFile, untouched)
units over 2 parameters 3 units, 63 lines 3 units, 54 lines

App-wide: unit complexity 4.02 → 4.08, unit size 3.40 → 3.42, duplication 4.54 → 4.56.

Maintainability floor: compare against development holds.

AI-assisted: prepared with Claude Code (claude-opus-5-5); reviewed by a maintainer before merge.


Generated by Claude Code

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.

2 participants