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.
composer require symfony/deprecation-contracts
// app/Services/OldService.php
public function legacyMethod()
{
trigger_deprecation('my/package', '1.0.0', 'Method "%s" is deprecated. Use "%s" instead.', 'legacyMethod()', 'newMethod()');
// ... existing logic
}
E_USER_DEPRECATED notices. Add to bootstrap/app.php:
use Symfony\Component\ErrorHandler\ErrorHandler;
ErrorHandler::register();
trigger_deprecation() at the start of deprecated methods to alert callers before logic executes:
public function oldAuthenticate(Request $request)
{
trigger_deprecation('laravel/framework', '10.0', 'Use "%s" instead.', 'auth:old');
return $this->newAuthenticate($request);
}
trigger_deprecation() in a reusable trait or helper:
// app/Traits/DeprecationHelper.php
trait DeprecationHelper
{
protected function deprecate(string $package, string $version, string $old, string $new): void
{
trigger_deprecation($package, $version, 'Using "%s" is deprecated. Use "%s".', $old, $new);
}
}
Usage:
class OldController
{
use DeprecationHelper;
public function index()
{
$this->deprecate('my/package', '2.0', 'OldController', 'NewController');
// ...
}
}
// tests/DeprecationTest.php
public function test_no_new_deprecations()
{
$this->expectDeprecationCount(0); // Fail if any deprecations triggered
$this->get('/legacy-route');
}
Use symfony/error-handler to track counts during tests.ErrorHandler::enable().deprecations.log) by extending Laravel’s logging:
// config/logging.php
'channels' => [
'deprecations' => [
'driver' => 'single',
'path' => storage_path('logs/deprecations.log'),
'level' => 'debug',
],
],
Configure the error handler to log E_USER_DEPRECATED to this channel.DEPRECATIONS.md file listing all active deprecations, with links to their first occurrence in code. Update this file via a script that parses trigger_deprecation() calls.ErrorHandler::register(), deprecations vanish. Always test with:
php artisan tinker --env=local
trigger_deprecation('test', '1.0', 'Test notice');
(Should output a deprecation notice.)'1.2.3', not 'v1.2' or 'upcoming'). Downstream tools (e.g., dependency updaters) rely on semantic versions.trigger_deprecation() anywhere (even in tests) disables all deprecations. Avoid in shared autoloaded files.trigger_deprecation() calls in the same method may flood logs. Group related deprecations into a single call with a composite message.if (!function_exists('trigger_deprecation')) {
function trigger_deprecation(string $package, string $version, string $message, ...$args): void {
$msg = sprintf('DEPRECATION [' . $package . ' ' . $version . ']: ' . $message, ...$args);
trigger_error($msg, E_USER_DEPRECATED);
}
}
// app/Providers/AppServiceProvider.php
public function boot()
{
\Log::listen(function ($level, $message, array $context) {
if (str_contains($message, 'DEPRECATION')) {
\Log::channel('deprecations')->info($message, $context);
}
});
}
trigger_deprecation(
'my/package', '1.0',
'Method "%s" will be removed in v2.0. See docs: %s',
'oldMethod()', 'https://example.com/migration-guide'
);
Illuminate\Support\Facades\DeprecatesFunctions (if available) to auto-generate deprecation stubs during upgrades.php -d opcache.enable=0 -n -r 'trigger_deprecation("test", "1.0", "test");'
How can I help you explore Laravel packages today?