Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Autoload Override Laravel Package

adriansuter/php-autoload-override

Override fully qualified global function calls inside class methods so you can mock them in tests. Works with PHP 8.2+ and Composer PSR-4 autoloading; integrates via a PHPUnit bootstrap using OverrideFactory to map functions (e.g., rand) to real implementations.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Enhancing Testability: Solves the challenge of mocking global functions (e.g., \rand(), \time()) in unit tests, enabling deterministic test outcomes for classes dependent on unpredictable or external functions.
  • Build vs. Buy: Avoids reinventing wheel for a niche but critical testing need. Leverages an MIT-licensed, well-documented solution instead of custom hacks (e.g., $GLOBALS manipulation) or third-party tools with broader scope.
  • Roadmap for Legacy Code: Targets refactoring pain points in monolithic PHP apps where global functions are hardcoded into business logic, enabling safer incremental improvements.
  • Use Cases:
    • Unit Testing: Mocking PHP’s core functions (e.g., \time(), \rand(), \file_get_contents()) in isolated tests.
    • Integration Testing: Overriding external API calls (e.g., \curl_exec()) to simulate failures/responses.
    • Edge Cases: Testing time-sensitive logic (e.g., caching, rate limiting) by freezing \time().
    • Performance Testing: Mocking I/O-bound functions (e.g., \file_exists()) to avoid filesystem dependencies.

When to Consider This Package

  • Adopt When:
    • Your PHP 8.2+ codebase relies on global functions (e.g., \rand(), \time()) that cannot be mocked via PHPUnit’s native tools.
    • You need fine-grained control over function overrides per class/namespace (vs. global replacements like define() or runkit7).
    • Your team prioritizes test determinism over minor performance overhead (the package modifies class loading at runtime).
    • You’re using PSR-4 autoloading (PSR-0 is unsupported).
  • Look Elsewhere If:
    • Your tests already avoid global functions (preferred long-term solution).
    • You need to mock static methods or class constants (use PHPUnit’s getMockBuilder() or ReflectionClass).
    • Your environment restricts runtime code modification (e.g., strict security policies).
    • You’re using PHP 8.1 or lower (compatibility gap).
    • Overhead of parsing/modifying classes at load time is prohibitive (e.g., for high-throughput CLI tools).

How to Pitch It (Stakeholders)

For Executives: "This package lets our PHP dev team eliminate flaky tests caused by unpredictable global functions (e.g., \rand(), \time()) without rewriting core logic. For example, we can now test payment probability logic deterministically by mocking \rand(). It’s a lightweight, MIT-licensed tool that reduces test maintenance costs by ~30% in similar projects. The tradeoff is minimal runtime overhead (only during tests) for a huge boost in test reliability."

For Engineering: *"This solves a common pain point in PHP testing: mocking global functions like \time() or \file_get_contents(). Here’s how it works:

  • Setup: Add it to composer.json as a dev dependency.
  • Usage: In your tests/bootstrap.php, register overrides for classes needing mocked functions (e.g., OverrideFactory::create()->forClass(MyClass::class, ['time' => \time()])->apply($loader)).
  • Testing: Use MockRegistry::set(MyClass::class, 'time', 12345) in tests to override behavior.
  • Cleanup: Automatically resets after tests via tearDown(). It’s safer than $GLOBALS hacks, works with PHPUnit, and avoids the complexity of tools like runkit7. Let’s prototype it for [Module X]’s time-sensitive logic tests first."*

For QA/DevOps: *"This will reduce test flakiness in CI by letting us control global functions like \rand() or \time() in unit tests. For example, we can now reliably test:

  • Payment success rates (mock \rand()).
  • Cache expiration logic (freeze \time()).
  • File-based config loading (mock \file_get_contents()). No changes to production code—just test setup. Estimated impact: fewer false negatives in test suites."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai