symfony/deprecation-contracts
Provides the global trigger_deprecation() helper to emit standardized, silenced deprecation notices with package name and version. Works with custom error handlers (e.g., Symfony ErrorHandler) to catch and log deprecations in dev and production.
Install via Composer:
composer require symfony/deprecation-contracts
First Use Case: Deprecate a legacy method in a Laravel service:
// app/Services/OldUserService.php
public function getUserByLegacyId($id)
{
trigger_deprecation('my/package', '1.0.0', 'Method "%s" is deprecated. Use "%s" instead.', __METHOD__, 'UserService::findById()');
return User::where('legacy_id', $id)->first();
}
Verify Setup: Ensure Symfony’s ErrorHandler is configured (Laravel includes it by default). Test in development:
php artisan serve
Check browser console or logs for deprecation notices.
trigger_deprecation() at the start of deprecated methods to notify callers before logic executes.
public function oldAuthenticate(Request $request)
{
trigger_deprecation('my/app', '2.0', 'Use "%s" instead.', 'auth()->attempt()');
return Auth::attempt($request->only('email', 'password'));
}
// app/Http/Middleware/DeprecationLogger.php
public function handle($request, Closure $next)
{
if ($request->is('legacy/*')) {
trigger_deprecation('my/app', '3.0', 'Route "%s" is deprecated.', $request->path());
}
return $next($request);
}
// app/Traits/DeprecationHelper.php
trait DeprecationHelper
{
protected function warnDeprecation(string $oldMethod, string $newMethod): void
{
trigger_deprecation('my/app', '1.0.0', 'Method "%s" is deprecated. Use "%s".', $oldMethod, $newMethod);
}
}
Usage:
class OldService {
use DeprecationHelper;
public function deprecatedMethod() {
$this->warnDeprecation(__METHOD__, 'NewService::replacement()');
// ... legacy logic
}
}
// tests/DeprecationBudgetTest.php
public function test_no_new_deprecations()
{
$this->withoutExceptionHandling();
$this->get('/legacy-endpoint');
$this->assertFalse(DeprecationTracker::hasNewDeprecations());
}
Use a custom DeprecationTracker class to aggregate notices.// bootstrap/app.php
if (app()->environment('local')) {
\Symfony\Component\ErrorHandler\ErrorHandler::register();
}
// config/logging.php
'channels' => [
'deprecations' => [
'driver' => 'single',
'path' => storage_path('logs/deprecations.log'),
'level' => 'debug',
],
];
Configure ErrorHandler to use this channel:
\Symfony\Component\ErrorHandler\ErrorHandler::register(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
How can I help you explore Laravel packages today?