jetbrains/phpstorm-stubs
PhpStorm Stubs: syntactically correct PHP files with signatures, constants, and PHPDoc for PHP core and many extensions. Used by IDEs for completion, inspections, type inference, and documentation popups. Community-driven support for non-standard extensions.
Installation:
Add to your Laravel project’s composer.json under require-dev:
"require-dev": {
"jetbrains/phpstorm-stubs": "^2024.2"
}
Run composer update jetbrains/phpstorm-stubs.
PhpStorm Configuration:
vendor/jetbrains/phpstorm-stubs/stubs
First Use Case:
array_map, json_decode).Redis::command()).Leveraging Stubs for Framework Code:
@method annotations in custom classes to mirror Illuminate\Support\Collection stubs.
/**
* @method static self make(array $attributes)
*/
class CustomCollection extends Collection {}
bind()/singleton() methods for better IDE support:
/**
* @method $this bind(string $abstract, $concrete = null, bool $shared = false)
*/
class AppServiceProvider extends ServiceProvider {}
Extension-Specific Workflows:
RedisArray and RedisCluster methods:
$redis->$ // Triggers completion for 'hget', 'lpush', etc.
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bindParam(1, $id, \PDO::PARAM_INT); // $id gets type hinting
Integration with Static Analysis:
array vs. array<string>).
// Without stubs: Psalm may warn about mixed return types.
// With stubs: IDE shows `@return array<int, string>` for `array_column()`.
Custom Stub Generation:
ext-mongodb), generate stubs using:
docker run --rm -v $(pwd):/stubs jetbrains/phpstorm-stubs:latest generate ext-mongodb
stubs/ext-mongodb/ and reference them in PhpStorm.Per-Project Configuration:
.idea/phpstorm.stubs.paths.xml:
<component name="ProjectStubPaths">
<stub-paths>
<stub-path>vendor/jetbrains/phpstorm-stubs/stubs</stub-path>
<stub-path>stubs/custom-extensions</stub-path>
</stub-paths>
</component>
COPY vendor/jetbrains/phpstorm-stubs/stubs /stubs
Configure PhpStorm to use /stubs as the stubs path.# .gitignore
/vendor/jetbrains/phpstorm-stubs/
Stub Version Mismatch:
array_unpack not found)."jetbrains/phpstorm-stubs": "2022.3" // For PHP 8.1
IDE Caching:
File > Invalidate Caches / Restart or toggle Settings > Appearance & Behavior > System Settings > Clear Cache.Custom Extensions:
php-amqplib) lack stubs.php-amqplib-stubs).False Positives in Static Analysis:
@return array vs. @return list<int>).@template or @mixin annotations to align stubs with static analysis tools:
/**
* @template TKey of array-key
* @template TValue
* @template-extends \ArrayObject<TKey, TValue>
*/
class ArrayObject {}
Performance in Large Projects:
.idea/phpstorm.stubs.exclude:
vendor/jetbrains/phpstorm-stubs/stubs/ext-curl
vendor/jetbrains/phpstorm-stubs/stubs/ext-soap
Missing Method Signatures:
stubs/ext-redis.php).Incorrect Type Hints:
json_decode()).PhpStorm Not Picking Up Stubs:
Custom Stub Templates:
@mixin:
/**
* @mixin \Illuminate\Support\Collection
*/
class ApiResourceCollection extends Collection {}
Stub Prioritization:
phpstorm.meta.php:
namespace PHPSTORM_META {
override(\App\Models\User::find(0), map([
'return' => '@return \App\Models\User'
]));
}
CI/CD Integration:
./vendor/bin/psalm --init --no-cache --output-format=json | jq '.errors[] | select(.message | contains("Stub mismatch"))'
Community Contributions:
Laravel-Specific:
/**
* @method static self whereIn(string $column, array $values)
*/
class User extends Model {}
Artisan commands for better autocompletion:
/**
* @method static void make(string $command, array $parameters = [])
*/
class Artisan {}
Performance:
Future-Proofing:
How can I help you explore Laravel packages today?