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

Patchwork Laravel Package

antecedent/patchwork

Patchwork is a PHP library for monkey patching: redefine functions, methods, and classes at runtime to ease testing and legacy refactors. Works with Composer and popular test tools, enabling stubs/mocks without changing existing code.

Deep Wiki
Context7

antecedent/patchwork is a PHP library for runtime code manipulation, enabling monkey patching and function/method interception without changing your source. It’s especially useful for testing legacy code, prototyping, or adding temporary behavior where dependency injection isn’t available.

By rewriting calls at load time, Patchwork lets you override built-ins and userland functions in a controlled way, making hard-to-test code testable.

  • Redefine functions and methods at runtime (monkey patching)
  • Intercept calls to inject behavior, stubs, or spies
  • Works well for legacy testing when refactors aren’t feasible
  • Supports patching global functions and static calls
  • Lightweight approach for quick experiments and debugging
Frequently asked questions about Patchwork
Can I use Patchwork to mock Laravel’s core functions like `Str::of()` or `Cache::remember()` in tests?
Patchwork can intercept Laravel’s facade methods (e.g., `Str::of()`) or static calls, but proceed with caution. Overriding core functions may destabilize Laravel’s assumptions or conflict with OPcache. Test thoroughly in isolation and avoid patching in production. For facades, consider Laravel’s built-in mocking tools first.
How do I install Patchwork in a Laravel project without breaking production?
Add Patchwork as a dev dependency via Composer (`composer require antecedent/patchwork --dev`) and scope patches to test environments using `.env` flags or CI checks. Avoid applying patches in production unless absolutely necessary, as runtime rewrites can interfere with OPcache and performance.
Will Patchwork work with Laravel’s service container or facade mocking?
Patchwork operates at a lower level than Laravel’s container or facade mocking, so it can complement or override those systems. For example, you can patch a static method while keeping container bindings intact. However, patches applied to the same method may conflict—prioritize one approach per test scenario.
Does Patchwork support PHP 8.2 and Laravel 10+?
Patchwork’s last release (2025) suggests compatibility with PHP 8.1+, but validate with Laravel 10+ and PHP 8.2+ in your environment. Check for conflicts with Laravel’s bootstrap/app.php or autoloader, as runtime rewrites may require adjustments like disabling OPcache in tests or using `opcache_reset()`.
How can I scope patches to specific test classes or methods in PHPUnit/Pest?
Apply patches in `setUp()` (PHPUnit) or `beforeEach()` (Pest) to ensure they’re isolated per test. Use a cleanup method (e.g., `tearDown()`) to revert patches. For Pest, leverage its `beforeAll`/`afterAll` hooks to manage patch lifecycle. Avoid global patches unless necessary.
Are there performance concerns with Patchwork in production?
Patchwork’s runtime rewrites bypass OPcache optimizations, which can degrade performance. In production, avoid patches unless critical, and consider disabling OPcache or using `opcache_reset()` sparingly. Benchmark patches in staging to assess overhead before deployment.
Can Patchwork patch third-party SDKs or static classes in Laravel?
Yes, Patchwork excels at targeting legacy or third-party code, including static classes or SDKs without source access. For example, you can stub a static `LegacyAuth::check()` method or intercept calls to an external API client. This is ideal for testing inherited codebases where refactoring isn’t feasible.
How do I handle static analysis warnings (Psalm/PHPStan) for patched methods?
Static analyzers may flag patched methods as undefined or unused. Mitigate this by adding `@method` or `@mixin` PHPDoc annotations to your test classes or using custom Psalm/PHPStan rules to ignore patched code. Document patches clearly in your test suite to avoid confusion.
What’s the difference between Patchwork and Laravel’s built-in mocking tools (Mockery, partial mocks)?
Patchwork enables runtime monkey patching of *any* function or method, including globals and statics, while Laravel’s mocking tools focus on container-bound classes. Use Patchwork for legacy code or third-party SDKs where DI isn’t an option, and Mockery for Laravel-specific dependencies.
How do I debug issues caused by Patchwork patches in Laravel?
Patchwork may obscure stack traces for patched code, making debugging tricky. Log patch applications explicitly and use `Xdebug` to step through rewritten methods. Avoid patching in production unless you have robust rollback mechanisms, and test patches in isolation first.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport