Skip to content

Implement function discovery cache (fixes #5) - #11

Merged
ezkimo merged 1 commit into
mainfrom
issue-5-cache-function-discovery-in-functionloader
Oct 19, 2026
Merged

ezkimo merged 1 commit into
mainfrom
issue-5-cache-function-discovery-in-functionloader

Conversation

@ezkimo

@ezkimo ezkimo commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Fixes #5

Description

Caches the function class discovery in FunctionLoader so repeated load() calls skip filesystem scans and is_subclass_of() checks.

  • Static cache (array<string, list<class-string<XPathFunctionInterface>>>) keyed by directory + "\0" + namespace, so separate loader instances with the same configuration share the result. The "\0" separator cannot occur in paths or namespaces, so keys are unambiguous.
  • New FunctionLoader::clearCache() to reset the cache in tests or when the functions directory changes at runtime.
  • The directory scan moved unchanged into protected discover(); the class is no longer final so tests can count discovery calls — the same trade-off ExpressionNormalizer already makes (issue Cache normalization results in ExpressionNormalizer #3).
  • load() / discover() now carry precise @return list<class-string<...>> docblocks, verified by Psalm.
  • README documents the caching behaviour and clearCache().

Tests

New tests/Compiler/FunctionLoaderCacheTest.php (8 tests) with autoloadable fixtures under tests/Fixtures/FunctionLoader/Example/:

  • repeated load() on the same loader runs discovery only once
  • separate loaders with the same directory/namespace share the cache
  • different directory or namespace is discovered separately (cache key parts)
  • clearCache() forces re-discovery
  • black-box check: a file added to the scanned directory stays invisible until clearCache(), then appears
  • filter regression: only *Function.php files implementing XPathFunctionInterface are discovered (excludes NotAFunction.php,
    IgnoredHelper.php, notes.md)
  • sanity check: the real Compiler/Functions directory still loads CountFunction
  • setUp()/tearDown() reset the static cache so tests are order-independent

Compatibility / Risks

Low, as anticipated in the issue:

  • load() signature and return type unchanged; public API stays backward compatible.
  • Behaviour change: files added/removed from the functions directory mid-process are only picked up after clearCache() — irrelevant for CL runs, now documented in the README.

Verification

  • composer test → 65/65 passing
  • composer psalm → no errors

@ezkimo
ezkimo merged commit 23d71d5 into main Oct 19, 2026
1 check 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.

Cache function discovery in FunctionLoader

1 participant