openswoole/ide-helper
Generates IDE helper stubs for OpenSwoole, improving autocompletion, type hints, and static analysis in PhpStorm and other editors. Useful for Swoole-style async/server apps to navigate APIs faster and reduce guesswork.
Installation Add the package via Composer:
composer require openswoole/ide-helper
Run the helper to generate stubs:
vendor/bin/ide-helper generate
IDE Configuration
settings.json:
"intelephense.environment.phpVersion": "8.1.0", // Match your PHP version
"intelephense.environment.swoole": true
First Use Case Test autocompletion in a Swoole-based Laravel controller:
use Swoole\Http\Request;
use Swoole\Http\Response;
class SwooleController extends Controller
{
public function handle(Request $request, Response $response)
{
// Autocomplete should now work for $request and $response
$response->end('Hello, Swoole!');
}
}
Swoole Server Initialization
Use the helper to autocomplete Swoole server methods in bootstrap/app.php or a custom Swoole server file:
$server = new Swoole\Http\Server('127.0.0.1', 9501);
$server->on('request', function (Request $request, Response $response) {
// Autocomplete works here
});
Middleware and Coroutines Leverage stubs for Swoole-specific middleware or coroutine functions:
use Swoole\Coroutine;
class SwooleMiddleware
{
public function handle(Request $request, Response $response, callable $next)
{
go(function () use ($request) {
// Autocomplete for Coroutine functions
$data = yield Coroutine\System::exec('ls -la');
});
$next($request, $response);
}
}
Event-Driven Autocompletion
Use stubs for Swoole events (e.g., onWorkerStart, onTask):
$server->on('workerStart', function (Swoole\Server $server, int $workerId) {
// Autocomplete for $server and $workerId
});
Laravel-Swoole Hybrid Combine Laravel’s dependency injection with Swoole’s server:
$server = resolve(Swoole\Server::class);
$server->on('request', function (Request $request, Response $response) {
$user = auth()->user(); // Laravel autocompletion works too
});
Stub Generation Overwrite
ide-helper generate overwrites existing stubs. Backup or use --force cautiously.--force only when necessary or commit stubs to version control.IDE-Specific Quirks
intelephense.environment.swoole is set to true; otherwise, Swoole classes won’t autocomplete.Namespace Conflicts
Request/Response may conflict with Laravel’s HTTP classes.\Swoole\Http\Request) in ambiguous contexts.Dynamic Properties
$server->stats) won’t autocomplete.@property annotations in custom stubs or rely on PHPDoc comments.Stub Path Issues
vendor/openswoole/ide-helper/stubs.vendor/bin/ide-helper generate --debug to check for errors.IDE Cache
Ctrl+Shift+P > "PHP Intelephense: Restart Server").PHP Version Mismatch
ide-helper config matches your PHP version:
# ide-helper.php
return [
'autoload' => [
'namespaces' => [
'Swoole' => __DIR__ . '/vendor/openswoole/ide-helper/stubs',
],
],
'swoole' => [
'version' => '4.8.0', // Match your Swoole version
],
];
Custom Stubs Extend stubs for unsupported Swoole classes or methods:
// In a custom stub file (e.g., `stubs/Swoole/CustomClass.php`)
<?php
namespace Swoole;
/**
* @method static void customMethod()
*/
class CustomClass {}
Integration with Laravel Mix
Automate stub generation in package.json:
{
"scripts": {
"dev": "npm run development",
"ide-helper": "php vendor/bin/ide-helper generate"
}
}
Run with:
npm run ide-helper
CI/CD Pipeline Add stub generation to your CI pipeline (e.g., GitHub Actions):
- name: Generate IDE Helpers
run: vendor/bin/ide-helper generate
How can I help you explore Laravel packages today?