Skip to content

Code smells of #283 (3/3): HtaccessHelper reads and writes the file once (C15) - #325

Merged
codeling merged 3 commits into
ccr-140f02b2-828xlp-c14from
ccr-140f02b2-828xlp-c15
Nov 20, 2026
Merged

codeling merged 3 commits into
ccr-140f02b2-828xlp-c14from
ccr-140f02b2-828xlp-c15

Conversation

@codeling

Copy link
Copy Markdown
Owner

Part 3 of 3 for #283. Stacked on #324 (which is stacked on #323): the base is that PR's branch, so retarget to main once the ones below have merged.

Problem

HtaccessHelper found its block section with two copies of the BEGIN/END marker state machine: extract() to read it, insert() to replace it. Each change read and scanned the file twice.

Change

  • load() reads the file once into before / section / after (plus found and unterminated).
  • store() writes it back once, with the new section where the old one was, or at the top followed by an empty line if there was none.
  • parseSection() is now a pure function of the section's lines, with no file access. Every change reads the file once and writes it once.
  • extract() and insert() are gone.

Behaviour

I recorded what the old code does for 14 file shapes (missing, empty, no or one section, no trailing newline, CRLF, legacy form, hand-written lines, unterminated BEGIN, other tools' markers, and so on) times 9 operations (deny, undeny, upgrade, 403 message, remove addresses, reads), 126 cases in all, then compared the new code. 117 are byte for byte identical, including return values and log messages.

The nine that differ are all the file with two BFStop sections. A change used to write the merged section once for each old section, so the file kept two copies. It now gets one, at the first section's position, with the addresses of both and the lines between and around them kept. A file like that is not meant to exist, and testSeveralSectionsBecomeOne covers it. A stray # END line outside a section is now left as an ordinary line; the old code replaced it with a section.

Testing

The comparison above ran outside Joomla, with stubs for Log, Factory and Uri. HtaccessHelperTest and the Apache job need a Joomla tree (JOOMLA_ROOT) that this environment doesn't have, so CI is the first run of those. php -l is clean; phpstan is not run here for the same reason.


Generated by Claude Code

…e it back once (issue #283, C15)

The marker state machines of extract() and insert() are one parser, load(),
and one writer, store(). Every change reads the file once instead of twice.
The output is byte for byte that of the old code for a file with none or one
section, with the same results and log messages; a file with several sections
now gets one, not the merged section once for each.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFJFMNpwNryanqvRcQvqDc
…hich is gone

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFJFMNpwNryanqvRcQvqDc
@codeling
codeling added this pull request to stack #326 November 20, 2026 22:02
@codeling
codeling merged commit f274b23 into main Nov 20, 2026
20 checks passed
@codeling
codeling deleted the ccr-140f02b2-828xlp-c15 branch November 20, 2026 22:06
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