作为一名程序猿,我患有 「代码规范强迫症」,其中 「use 导入类优雅排序症」 已经是晚期。
什么是 「use 导入类优雅排序症」?
我们看下面代码
<?php
namespace App\Http\Controllers\NLP;
use App\Http\Controller;
use App\Models\Role;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use App\Models\Action;
use Illuminate\Http\Request;
class ActionController extends Controller
{
// 省略...
}use 导入 参差不齐,看的我针眼都快长出来,完全没有心情做开发呀。不给它排好序,心里难受。
<?php
namespace App\Http\Controllers\NLP;
use App\Models\Role;
use App\Models\Action;
use App\Http\Controller;
use Illuminate\View\View;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class ActionController extends Controller
{
// 省略...
}大家看看这个,多舒服呀。但,这是我一行一行复制粘贴排出来的。好麻烦,好费力。
排:当导入的类太多,或者要排的 php 文件太多,简直给自己增加工作量;
不排:我会发「病」的,看着参差不齐的 use 导入类,没心情开发项目。
就在这种情况背景下,我硬刚了两天 IntelliJ 和 Java,终于开发出来了一个一键式 use 类导入优雅排序。现在分享出来,给同样有这种「病」的童鞋使用。
请尽量使用 2019.1.1 的 PhpStorm 版本。因为是以此版本的 php-openapi 进行开发的。其它旧版本,可能会出现兼容问题,当然可能性较小而已。
插件包地址 --> PhpStorm-Plugins-v1.0
- 打开 PhpStorm 设置
- 选择 Plugins 的 Install Plugins form Disk...
- 选择下载好的 PhpStorm-Plugins.jar
- 最后,重启 PhpStorm
在 PhpStorm 菜单栏右侧多出一个 Yuanshang 的工具集,里面暂有一个工具 Use Format。
Use Format:利用正则规则和冒泡排序,对 use 导入类进行优雅排序。
排序前:
Alt + F 或者鼠标点击 Use Format,进行排序。排序后:
排序工具,会删除从 <?php 到 use 导入类最后一行,中间的注释等非必要内容,并在 use 导入代码集前后加一换行。
排序前
排序后
排序前
排序后
插件 GitHub 开源地址 ---> 传送门
欢迎大家 Star。
对了,使用过程中,如果出现排序乱码,ctrl + z 回一下,然后欢迎提交问题。在评论区发表就可以。















