Yii2 IDE Helper ¶
为 Yii2 框架提供 PhpStorm 智能代码补全支持,灵感来源于 barryvdh/laravel-ide-helper
功能特性 ¶
- ✅ 为 Yii2 组件生成完整的 PHPDoc 类型提示
- ✅ 为 ActiveRecord 模型生成属性和方法文档
- ✅ 为 ActiveQuery 生成查询构建器提示
- ✅ 生成 PhpStorm Meta 文件支持高级 IDE 特性(DI 容器、类型推断)
- ✅ 支持自定义配置路径
- ✅ 完整的 CLI 命令支持
- ✅ 支持多数据库(MySQL、PostgreSQL、SQLite)
安装 ¶
要求 ¶
- PHP >= 7.4
- Yii2 >= 2.0.43
- PhpStorm 2018.2 或更高版本
使用 Composer 安装 ¶
composer require --dev chinaphp/yii2-ide-helper
配置 ¶
在 console/config/main-local.php 中添加以下配置(仅本地开发环境需要):
<?php
$config = [
'controllerMap' => [
'ide-helper' => 'Chinaphp\Yii2IdeHelper\Console\Controller',
],
'components' => [
'ide-helper' => [
'class' => 'Chinaphp\Yii2IdeHelper\Config\ConfigProvider',
'output_dir' => dirname(__DIR__),
'filename' => '_ide_helper.php',
'meta_filename' => '.phpstorm.meta.php',
'config_paths' => [
'@app/config/web.php',
'@app/config/console.php',
],
],
],
],
];
使用 ¶
生成组件类型提示 ¶
php yii ide-helper/generate
这将生成 _ide_helper.php 文件,包含所有 Yii2 组件的类型提示。
生成的内容:
- Yii 组件的
@property注解(如$db,$cache,$session) - 组件 getter 方法的
@method注解(如getDb(),getCache()) - 完整的命名空间和类定义
生成 ActiveRecord 模型类型提示 ¶
php yii ide-helper/models
这将扫描 ActiveRecord 模型并生成属性和关系类型提示。
生成的内容:
- 数据库字段的
@property注解(包含类型和默认值) - 关系方法的
@property-read注解 - 魔术查询方法的
@method注解(where*(),orWhere*(),andWhere*()) - 自定义 ActiveQuery 类的完整实现
生成 PhpStorm Meta 文件 ¶
php yii ide-helper/meta
这将生成 .phpstorm.meta.php 文件,为依赖注入容器提供类型推断。
生成的内容:
- DI 容器绑定(
Yii::$container->get()类型推断) - 组件属性映射(
Yii::$app->get()类型提示) - ActiveRecord 模式(
find(),hasMany(),hasOne()返回类型) - 对象创建模式(
Yii::createObject()类型推断)
查看帮助 ¶
php yii help
PhpStorm Meta 文件详解 ¶
.phpstorm.meta.php 文件为 PhpStorm 提供高级类型推断能力,以下是它支持的功能:
1. DI 容器类型推断 ¶
override(\Yii::$app->get('db'), type(\yii\db\Connection));
这个配置告诉 PhpStorm,Yii::$app->get('db') 的返回类型是 \yii\db\Connection。
使用示例:
`php
$db = Yii::$app->get('db');
$result = $db->createCommand('SELECT * FROM users')->queryAll(); // 有完整的类型提示
`
2. 组件属性类型提示 ¶
override(\Yii::$app->db, type(\yii\db\Connection));
这个配置告诉 PhpStorm,Yii::$app->db 属性的类型是 \yii\db\Connection。
使用示例:
`php
$result = Yii::$app->db->createCommand('SELECT * FROM users')->queryAll(); // 有完整的类型提示
`
3. ActiveRecord 查询类型 ¶
override(\yii\db\ActiveRecord::find(), type(\yii\db\ActiveQuery));
这个配置告诉 PhpStorm,ActiveRecord::find() 返回 ActiveQuery 类型。
使用示例:
`php
$query = User::find(); // PhpStorm 知道返回的是 ActiveQuery
$query->where(['status' => 1]); // 有完整的类型提示
`
4. 自定义绑定 ¶
你可以在代码中添加自定义绑定:
$generator = new MetaGenerator($config);
$generator->addBinding('custom', ['Custom\Class']);
$generator->save();
配置文件示例:
`php
// console/config/main.php
'components' => [
'ide-helper' => [
'class' => 'Chinaphp\Yii2IdeHelper\Config\ConfigProvider',
'output_dir' => dirname(__DIR__),
'filename' => '_ide_helper',
'meta_filename' => '.phpstorm.meta.php',
'config_paths' => [
'@app/config/web.php',
'@app/config/console.php',
],
],
],
`
PhpStorm 配置 ¶
- 在 PhpStorm 中打开项目
- 导航到 Settings > PHP > Include paths
- 添加
_ide_helper.php文件路径 - 如果有
.phpstorm.meta.php,确保它在项目根目录 - 重新索引项目(File > Invalidate Caches / Restart)
生成文件示例 ¶
_ide_helper.php ¶
namespace Yii {
class App {
public static $app;
}
}
namespace {
class Yii extends \Yii\BaseYii {
/**
* @var \yii\db\Connection
*/
public $db;
/**
* @var \yii\caching\Cache
*/
public $cache;
}
}
_ide_helper_models.php ¶
namespace app\models {
/**
* @property int $id
* @property string $title
* @property-read \app\models\User $user
* @property-read \app\models\Comment[] $comments
*/
class Post extends \yii\db\ActiveRecord {
}
}
.phpstorm.meta.php ¶
<?php
namespace PHPSTORM_META {
// DI 容器绑定
override(\Yii::$app->get('db'), type(\yii\db\Connection));
override(\Yii::$app->db, type(\yii\db\Connection));
override(\Yii::$app->get('cache'), type(\yii\caching\FileCache));
override(\Yii::$app->cache, type(\yii\caching\FileCache));
// ActiveRecord 类型推断
override(\yii\db\ActiveRecord::find(), type(\yii\db\ActiveQuery));
override(\yii\db\ActiveRecord::hasMany(), type(\yii\db\ActiveQuery));
override(\yii\db\ActiveRecord::hasOne(), type(\yii\db\ActiveQuery));
}
Meta 文件优势:
- ✅ 为
Yii::$app->get()提供准确的类型推断 - ✅ 为
Yii::$app->component属性提供类型提示 - ✅ 为 ActiveRecord 查询方法提供类型安全
- ✅ 支持依赖注入容器的类型推断
- ✅ 增强代码自动补全和重构能力
测试 ¶
运行测试套件:
composer test
运行代码规范检查:
composer lint
贡献 ¶
欢迎提交 Pull Request!
许可证 ¶
MIT License
致谢 ¶
本项目灵感来源于 barryvdh/laravel-ide-helper
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.