Skip to content

Conversation

@CybotTM
Copy link
Contributor

@CybotTM CybotTM commented Jan 4, 2026

Summary

Improves URL generation in AbstractUrlGenerator with fixes for empty filenames, known document handling, and code quality improvements.

Changes

1. Empty Filename Support (bugfix)

When filename is empty (same-page anchor reference):

  • Before: Returns .html#anchor or .html (invalid URLs)
  • After: Returns #anchor or # (correct same-page links)

2. Known Document Handling (bugfix)

Fixed the TODO hack for known documents:

  • Before: Prefixed with / then passed through canonicalUrl() - a workaround
  • After: Known documents are used directly (they're already canonical)
  • Removes trailing # from URLs without anchors (e.g., /page1.html instead of /page1.html#)

3. Code Quality Improvements (refactor)

  • Created InvalidUrlException for better error semantics (was generic Exception)
  • Added clarifying comment for email validation (used for mailto: link support)
  • Added edge case tests for empty string anchors
  • Added test for InvalidUrlException on absolute URLs
  • Removed unused test mock

Files Changed

Source:

  • packages/guides/src/Renderer/UrlGenerator/AbstractUrlGenerator.php
  • packages/guides/src/Renderer/UrlGenerator/Exception/InvalidUrlException.php (new)

Tests:

  • packages/guides/tests/unit/Renderer/UrlGenerator/RelativeUrlGeneratorTest.php
  • tests/Integration/tests/toctree/toctree-external-linktargets/expected/index.html
  • tests/Integration/tests-full/bootstrap/bootstrap-menu-external-linktargets/expected/index.html
  • tests/Integration/tests-full/bootstrap/bootstrap-menu-external-on-subpage/expected/index.html

Test Plan

  • Unit tests: 25 tests pass (was 11 + 2 skipped)
  • Integration tests: Updated expected files for correct URL format
  • PHPStan: No errors
  • PHPCS: No errors

@CybotTM CybotTM marked this pull request as draft January 4, 2026 20:15
@CybotTM CybotTM changed the title [BUGFIX] Support empty filenames in createFileUrl for same-page anchors [BUGFIX] Improve URL generation for empty filenames and known documents Jan 4, 2026
@CybotTM CybotTM marked this pull request as ready for review January 4, 2026 20:49
Empty filename with anchor should return '#anchor' (same-page link),
not '.html#anchor' which is invalid.

Use case: referencing an anchor on the current page without
specifying the document name.
- Handle empty string anchor same as null (no trailing #)
- Remove unused getCurrentFileName mock from test
- Add edge case tests for empty string anchor
- Rename test case for clarity (empty anchor → null anchor)
- Create InvalidUrlException for better error semantics
- Fix known document handling (remove /prefix hack, use directly)
- Add clarifying comment for email validation (mailto: support)
URLs to pages without anchors should not have a trailing #
@jaapio jaapio force-pushed the fix/empty-filename-url-generator branch from 8b01df0 to 4e8a6a1 Compare January 6, 2026 21:20
@jaapio jaapio enabled auto-merge January 6, 2026 21:21
@jaapio
Copy link
Member

jaapio commented Jan 6, 2026

Thanks this looks good to me.

@jaapio jaapio merged commit 11e48a4 into phpDocumentor:main Jan 6, 2026
58 checks passed
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