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.
Bootstrap/HandleExceptions). Overrides to core PHP functions (e.g., array_map, json_encode) could destabilize Laravel’s assumptions.registerShutdownFunction or spl_autoload_register could conflict with Laravel’s bootstrap/app.php or composer.json autoloading. A dedicated PatchworkServiceProvider would mitigate this.DB::connection()) fills gaps in Laravel’s mocking tools.opcache.reset() or disabling OPcache in test environments.Str::of(), Cache::remember)?.env flags or CI checks)?Mockery, partial mocks) or dependency injection suffice for 80% of use cases?brick/math, mockery) that offer safer alternatives?bootstrap/app.php autoloader.dev dependency to avoid production bloat.setUp() or beforeEach() to apply patches per test.LegacyAuth::check()).// tests/TestCase.php
use antecedent\Patchwork\Patchwork;
protected function setUp(): void {
parent::setUp();
Patchwork::when('LegacyService::process')
->thenReturn(fn($input) => 'mocked_' . $input);
}
PATCHES.md file with:
DB::raw() for legacy queries").bind() or extend() for consistency.Cache::get()) but avoid patching Laravel’s internal facade handlers.Event::listen()).Patchwork::priority() to ensure critical patches (e.g., security overrides) take effect first.tests/CreatesApplication or a dedicated PatchworkServiceProvider.// app/Providers/PatchworkServiceProvider.php
public function boot() {
if (app()->environment('testing')) {
Patchwork::when('App\Legacy\Helper::deprecatedMethod')
->then(fn(...$args) => throw new \RuntimeException('Deprecated!'));
}
}
PHPUNIT_PATCHWORK_ENABLED=true).// PATCH: LegacyAuth::validate() -> throws AuthException for testing
// Issue: LARAVEL-1234 | Expires: 2025-12-31
// Reason: Bypass legacy auth for migration tests.
// phpstan.neon
arguments:
level: 5
checkMissingIterableValueType: false
checkPropertyAssignmentInConstructor: false
Patchwork::dump() to inspect active patches.// app/Http/Middleware/LogPatches.php
public function handle($request, Closure $next) {
if (Patchwork::hasActivePatches()) {
\Log::warning('Active patches detected', ['patches' => Patchwork::listPatches()]);
}
return $next($request);
}
Auth::check() breaking login flows).laravel-debugbar to measure runtime overhead.Patchwork::clear() between tests.How can I help you explore Laravel packages today?