jetbrains/phpstorm-stubs
Syntactically correct PHP stub files for core and common extensions, providing function/class signatures, constants, and comprehensive PHPDoc for accurate IDE completion, inspections, type inference, and documentation in PhpStorm and other tools.
Install via Composer (dev dependency):
composer require --dev jetbrains/phpstorm-stubs
Configure PhpStorm:
Settings > Languages & Frameworks > PHP.PHP Runtime, click Advanced Settings.jetbrains/phpstorm-stubs repository (e.g., ./vendor/jetbrains/phpstorm-stubs/stubs).Invalidate Caches:
File > Invalidate Caches / Restart to refresh IDE indexing.Verify:
array_* or str_*) and Laravel classes (e.g., Illuminate\Support\Collection).Collection or Facade classes.Collection::where(), Facade::call(), etc.@return types (e.g., Collection<int, Model> instead of mixed).| Task | Command/Action |
|---|---|
| Install stubs | composer require --dev jetbrains/phpstorm-stubs |
| Clone repo (for updates) | git clone https://github.com/JetBrains/phpstorm-stubs.git ./stubs |
| Configure PhpStorm path | Set stubs path in Settings > PHP > PHP Runtime > Advanced |
| Invalidate caches | File > Invalidate Caches / Restart |
| Test autocompletion | Type Collection:: and verify method suggestions. |
| Validate PHPStan alignment | Run phpstan analyze --level=5 and compare with IDE hints. |
Project Setup:
composer.json:
{
"require-dev": {
"jetbrains/phpstorm-stubs": "^2024.2"
}
}
composer update to install.PhpStorm Configuration:
Settings > PHP > PHP Runtime > Advanced > Default stubs path.~/phpstorm-stubs) for all projects.Version Management:
2024.2) to avoid breaking changes:
composer require jetbrains/phpstorm-stubs:2024.2 --dev
CI/CD Integration:
# .github/workflows/pre-commit.yml
- name: Check PHPDoc
run: |
php -r "require 'vendor/jetbrains/phpstorm-stubs/stubs/stdlib.php';"
# Custom script to lint docblocks against stubs
phpstan with --level=5 to ensure IDE and static analysis align.Standard Extensions (e.g., Redis, PDO, DOM):
Redis methods like Redis::connect() will show correct signatures.Non-Standard Extensions (e.g., MongoDB, GMP):
Custom Extensions:
php -r 'reflection_function("your_function");' and add to stubs/ext-<extension>/.MyExtension:
// stubs/ext-myextension/MyExtension.php
namespace MyExtension;
/**
* @method static string myCustomFunction(array $input)
* @return string
*/
class MyExtension {}
PHPStan/Psalm Alignment:
@return types in stubs match your static analysis tool’s expectations.array<int, string> instead of array for stricter typing.Laravel-Specific Stubs:
Illuminate\Support\Collection include modern PHP types (e.g., Collection<int, Model>).Collection macros:
// stubs/framework/Illuminate/Support/Collection.php
/**
* @method static Collection<int, Model> customMacro(array $data)
* @return Collection<int, Model>
*/
Testing Stub Accuracy:
phpstan analyze --level=5 and compare results with IDE hints.php -r 'var_dump(reflection_function("array_map"));' to verify stubs match runtime behavior.Stub Version Mismatch:
2024.2 for PhpStorm 2024.2).Non-Standard Extension Gaps:
MongoDB or GMP may lack @throws or @param tags.IDE Caching Issues:
File > Invalidate Caches) after updating stubs.PHPDoc Inconsistencies:
@return mixed where @return array<int, T> is expected.stubs/overrides/.Composer Autoloader Conflicts:
composer dump-autoload is run after installing stubs.Verify Stub Loading:
php -r 'var_dump(class_exists("stdClass"));' to test basic stub functionality.Inspect Stub Files:
vendor/jetbrains/phpstorm-stubs/stubs/ and manually check files like:
stdlib.php (core PHP functions)ext-redis.php (Redis extension)framework/Illuminate/Support/Collection.php (Laravel)Compare with Runtime:
php -r 'print_r(reflection_function("array_map"));' to compare stubs with actual runtime behavior.Enable Debug Logging:
Settings > PHP > Debug) to diagnose stub-related issues.Custom Stub Overrides:
stubs/overrides/ (e.g., stubs/overrides/ext-redis.php).Redis::connect():
/**
* @param string $host
* @param int $port
* @param float $timeout
* @return Redis
* @throws RedisException
*/
function connect(string $host, int $port = 6379, float $timeout = 0.0): Redis {}
Contributing New Stubs:
docker compose run --rm test_runner php tests/run-stubs-parser.php to validate new stubs.Generating Stubs for Custom Extensions:
php -r 'print_r(reflection_class("MyCustomClass"));' > stubs/ext-mycustom/MyCustomClass.php
How can I help you explore Laravel packages today?