empi89/php-amqp-stubs
Provides PHP stub files for AMQP-related extensions, improving IDE autocomplete, static analysis, and type hints in projects that use AMQP functions/classes without bundling the actual extension. Useful for CI, analysis, and editor support.
Installation
Add the package to your composer.json:
composer require empi89/php-amqp-stubs
Ensure pdezwart/php-amqp is also installed (required dependency):
composer require pdezwart/php-amqp
Purpose
This package provides stubs (type hints) for php-amqp to enable static analysis tools (e.g., PHPStan, Psalm) and IDE autocompletion without requiring the php-amqp extension to be installed on your development machine.
First Use Case
php-amqp stubs:
vendor/bin/phpstan analyse --level=5 src/
php-amqp classes (e.g., AMQPConnection, AMQPChannel).Development Without AMQP Extension
php-amqp extension isn’t available (e.g., GitHub Actions)..github/workflows/phpstan.yml:
jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install --prefer-dist
- run: vendor/bin/phpstan analyse --level=5
Laravel Integration
App\Jobs using php-amqp (e.g., AMQPQueue).
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
public function handle()
{
$connection = new AMQPStreamConnection('localhost', 5672, 'user', 'pass');
$channel = $connection->channel();
$channel->queue_declare('jobs', false, true, false, false);
$channel->basic_publish(new AMQPMessage('Hello AMQP!'), '', 'jobs');
}
AMQP configurations in config/amqp.php:
return [
'host' => env('AMQP_HOST', 'localhost'), // IDE will hint `string` type
'port' => env('AMQP_PORT', 5672), // IDE will hint `int` type
];
Testing
php-amqp in unit tests with stubs + Mockery:
$mockConnection = Mockery::mock('overload:PhpAmqpLib\Connection\AMQPStreamConnection');
$mockConnection->shouldReceive('channel')->andReturnSelf();
composer.json:
"autoload": {
"psr-4": {
"App\\": "src/"
},
"files": [
"vendor/empi89/php-amqp-stubs/stubs.php" // Explicitly include stubs
]
}
vendor/empi89/php-amqp-stubs as "Sources Root" in project settings.php.suggest.stubs in settings.json:
"php.suggest.stubs": [
"vendor/empi89/php-amqp-stubs/stubs.php"
]
Stub vs. Runtime Mismatch
AMQPConnection failing to connect) still occur if the php-amqp extension is missing.if (extension_loaded('amqp')) checks or feature flags:
if (!extension_loaded('amqp')) {
throw new RuntimeException('php-amqp extension is required.');
}
Outdated Stubs
php-amqp updates, causing false positives in static analysis.composer update empi89/php-amqp-stubs
IDE False Positives
__toString()) as undefined.@mixin annotations or suppress inspections for stub files.--error-format=github for actionable PR feedback:
vendor/bin/phpstan analyse --error-format=github
AMQPConnection::isConnected() to verify connections:
$connection = new AMQPStreamConnection('localhost', 5672);
if (!$connection->isConnected()) {
throw new \RuntimeException('AMQP connection failed.');
}
Custom Stubs
stubs.php file in your project:
// Custom stub for a non-standard AMQP method
class_alias('PhpAmqpLib\Connection\AMQPConnection', 'AMQPConnection');
class AMQPConnection {
public function customMethod() { /* ... */ }
}
composer.json under "autoload.files".CI-Specific Configs
php-amqp stubs in Docker-based CI to avoid extension dependencies:
# Dockerfile
RUN pecl install amqp && docker-php-ext-enable amqp
Or skip installation entirely and rely on stubs for static checks.Laravel Queue Drivers
php-amqp as a Laravel queue driver, stubs help validate:config/queue.php:
'connections' => [
'amqp' => [
'driver' => 'amqp', // IDE hints: driver must be 'amqp'
'host' => env('AMQP_HOST'),
'queue' => 'laravel',
'exchange_options' => ['name' => 'laravel', 'type' => 'direct'], // IDE hints: 'type' must be 'direct'|'fanout'|etc.
],
],
How can I help you explore Laravel packages today?