Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>php useNew to Visual Studio Code? Get it now.
php use

php use

icescrown

|
47 installs
| (0) | Free
Manage PHP use statements
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

php use

一个功能强大的 Visual Studio Code 扩展,用于智能管理 PHP 文件中的 use 语句,帮助开发者保持代码的整洁和高效。

✨ 功能特性

  • 删除未使用的 use 语句:自动检测并删除 PHP 文件中未使用的 use 语句
  • 导入未声明的类 / 函数:通过快捷键为代码中使用但未导入的类或函数添加 use 语句,函数自动生成 use function 形式
  • 工作区符号索引:后台自动索引工作区中的类和函数定义,导入时毫秒级响应,文件变化实时增量更新
  • 同名符号智能处理:多个同名定义时弹出候选列表区分选择;与已有 use 语句同名冲突时自动生成别名
  • 展开命名空间:将光标所在类的短类名展开为完整的命名空间路径
  • 自动排序 use 语句:添加新 use 语句后自动按字母顺序或自然顺序排序命名空间
  • 支持多种 use 语句格式:完美支持单个 use 语句和分组 use 语句(含 use function 分组)
  • 智能诊断:实时检测并高亮显示未使用的 use 语句
  • 支持 PHPDoc 类型检测:自动识别 PHPDoc 注释中的类引用(@param、@return、@throws、@var、@type)
  • 便捷操作方式:提供快捷键、右键菜单和快速修复选项
  • 个性化配置:支持多种可自定义的设置选项
  • 仅针对 PHP 文件:安全可靠,避免意外修改其他类型的文件

📋 系统要求

  • Visual Studio Code 1.100.0 或更高版本
  • 无需外部依赖,开箱即用

🚀 使用方法

删除未使用的 use 语句

  1. 在 VSCode 中打开一个 PHP 文件
  2. 使用以下任一方式触发操作:
    • 按下快捷键 Ctrl+Shift+R (Windows/Linux) 或 Cmd+Shift+R (Mac)
    • 在编辑器中右键点击,选择 "Remove Unused Imports"
    • 当检测到未使用的 use 语句时,点击灯泡图标并选择 "Remove unused imports"
  3. 扩展将自动删除所有未使用的 use 语句

导入未声明的类 / 函数

  1. 在 VSCode 中打开一个 PHP 文件
  2. 将光标放在要导入的类名或函数名上
  3. 使用快捷键 Ctrl+Alt+I (Windows/Linux) 或 Cmd+Alt+I (Mac)
  4. 如果有多个同名定义,选择你想要导入的符号(函数条目带 [function] 标记)
  5. 扩展将自动添加相应的 use 语句:
    • 类 → use App\Models\User;
    • 函数 → use function App\Helpers\format_price;
  6. 如果与已有 use 语句简单名冲突,会自动生成别名(如 use Vendor\Lib\User as LibUser;)

💡 符号查找基于工作区索引,插件激活时后台自动构建,文件变化实时更新,导入操作即时响应。

展开命名空间

  1. 在 VSCode 中打开一个 PHP 文件
  2. 将光标放在要展开的类名上(该类必须有对应的 use 语句)
  3. 使用快捷键 Ctrl+Alt+E (Windows/Linux) 或 Cmd+Alt+E (Mac)
  4. 扩展将自动将该类的短类名替换为完整的命名空间路径

📝 示例

删除未使用的 use 语句

使用前:

<?php

use App\Models\User;
use App\Models\Post;
use App\Models\Comment;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TestClass
{
    public function testMethod()
    {
        $user = new User();
        $data = DB::table('users')->get();
        
        return $user;
    }
}

使用后:

<?php

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TestClass
{
    public function testMethod()
    {
        $user = new User();
        $data = DB::table('users')->get();
        
        return $user;
    }
}

导入未声明的类

使用前:

<?php

class TestClass
{
    public function testMethod()
    {
        $user = new User(); // User 类未导入
        return $user;
    }
}

使用后:

<?php

use App\Models\User;

class TestClass
{
    public function testMethod()
    {
        $user = new User();
        return $user;
    }
}

导入函数

使用前:

<?php

class TestClass
{
    public function testMethod()
    {
        // format_price 函数未导入
        return format_price(100);
    }
}

使用后(将光标放在 format_price 上并按 Ctrl+Alt+I):

<?php

use function App\Helpers\format_price;

class TestClass
{
    public function testMethod()
    {
        return format_price(100);
    }
}

展开命名空间

使用前:

<?php

use App\Models\User;
use App\Models\Post;

class TestClass
{
    public function testMethod()
    {
        $user = new User();
        $post = new Post();
        
        return [$user, $post];
    }
}

使用后(将光标放在 User 上并按 Ctrl+Alt+E):

<?php

use App\Models\User;
use App\Models\Post;

class TestClass
{
    public function testMethod()
    {
        $user = new \App\Models\User();
        $post = new Post();
        
        return [$user, $post];
    }
}

PHPDoc 类型检测

扩展会自动识别 PHPDoc 注释中的类引用,包括:

<?php

use App\Models\User;
use App\Models\Post;
use App\Exceptions\ValidationException;

class UserService
{
    /**
     * @param User $user 用户对象
     * @return User|Post 返回用户或帖子对象
     * @throws ValidationException 当验证失败时抛出
     * @var User $currentUser 当前用户
     */
    public function processUser(User $user)
    {
        return $user;
    }
}

在上面的示例中,User、Post 和 ValidationException 都会被检测为已使用,不会被标记为未使用的 use 语句。

支持的 PHPDoc 标签:

  • @param - 函数参数类型
  • @return - 返回类型
  • @throws - 抛出的异常类型
  • @var - 变量类型
  • @type - 类型注解

⚙️ 扩展设置

在 VSCode 的设置中,搜索 "php use" 可以找到以下配置选项:

设置项 类型 默认值 描述
php-use.detectUnusedImports Boolean true 自动检测未使用的 use 语句
php-use.showUnusedImportsDiagnostics Boolean false 显示未使用的 use 语句诊断警告
php-use.diagnosticSeverity String Information 诊断的严重级别(Warning/Information/Hint)
php-use.showNotifications Boolean true 当删除未使用的 use 语句时显示通知消息
php-use.exclude Array ["/node_modules/", "/vendor/"] 排除搜索的文件和文件夹的 glob 模式
php-use.autoSortAfterImports Boolean false 添加新 use 语句后自动按字母顺序排序命名空间 use 语句
php-use.naturalSort Boolean false 使用自然顺序算法排序 use 语句(例如:Item 2 排在 Item 10 之前)

排序功能说明

扩展支持两种排序方式:

  1. 字母顺序排序(默认):按照字母顺序对命名空间进行排序
  2. 自然顺序排序:使用自然顺序算法,数字部分按数值大小排序

使用方式:

  • 只勾选 php-use.autoSortAfterImports:自动排序(默认使用字母顺序排序)
  • 同时勾选 php-use.autoSortAfterImports 和 php-use.naturalSort:使用自然顺序排序 use 语句

排序示例:

字母顺序排序:

use App\Models\Item10;
use App\Models\Item2;
use App\Models\Item3;

自然顺序排序:

use App\Models\Item2;
use App\Models\Item3;
use App\Models\Item10;

🐛 已知问题

  • 目前没有已知问题。如果您发现了任何问题,请在 GitHub 仓库 中提交。

📄 发布说明

0.0.6

  • 新增函数导入支持,光标放在函数名上即可生成 use function 语句
  • 新增工作区符号索引,后台预建 + 实时增量更新,导入毫秒级响应
  • 同名符号弹出候选列表区分选择,同名冲突自动生成别名
  • 多项性能与稳定性优化,详见 CHANGELOG.md

0.0.5

  • 优化代码结构,提高可维护性
  • 提取正则表达式为类常量,提升性能
  • 拆分大方法为多个小方法,提高代码可读性
  • 使用现代 JavaScript 语法优化代码
  • 优化函数检测性能

0.0.1

  • 初始版本发布
  • 支持单个 use 语句和分组 use 语句
  • 实现删除未使用的 use 语句功能
  • 实现导入未声明的类功能
  • 提供快捷键和右键菜单操作
  • 支持智能诊断和快速修复
  • 添加多种可自定义的设置选项

如果您喜欢这个扩展,请在 VSCode 市场中给它评分!


  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft