PHP 开发工具扩展包 使用笔记

前置知识

PHP代码风格指南

PSR 是 PHP Standard Recommendations (PHP 推荐标准)的简写,由 PHP FIG 组织制定的 PHP 规范,是 PHP 开发的实践标准。中文文档 例如composer 遵循psr4标准

很多php框架和组件都遵循psr规范,因此要编写遵循行业规范的代码

PHP-CS-Fixer

推荐在项目中安装配置,因为不同的项目php版本和规范不同,依赖的php-cs-fixer的版本也不相同

安装

composer require --dev friendsofphp/php-cs-fixer

项目根目录创建 .php-cs-fixer.php 配置文件(旧版本为.php_cs) 并且将它纳入git版本管理,保证该项目的所有开发人员都使用相同的规范

将缓存文件.php-cs-fixer.cache 添加到.gitignore文件中 (旧版本为.php_cs.cache)

规则和配置文件

可用内置规则地址 Laravel框架规则集地址

配置文件生成工具

点击问号,查看配置工具使用说明

通过规则风格筛选 比如php版本,psr12等

选中需要的规则

点击导出按钮生成规则配置文件

如何使用

手动使用方式 文档

#指定规则运行
$ php php-cs-fixer.phar fix /path/to/project --rules=@PSR12

#框架项目中指定配置文件手动修复,以laravel为例,在项目根目录下执行
$ vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php
#执行结果如下
Loaded config default from ".php-cs-fixer.php".
   1) app/Http/Controllers/Admin/CategoryController.php
   2) app/Http/Controllers/Admin/HotKeywordsController.php
  ... 此处省略中间部分输出信息
 205) routes/api.php
 206) tests/Feature/CommonApiTest.php
Fixed all files in 2.135 seconds, 20.000 MB memory used

php-cs-fixer vscode插件 github

扩展>>安装 ,个人配置项

{
   "php-cs-fixer.executablePath": "${extensionPath}/php-cs-fixer.phar",
   "php-cs-fixer.onsave": true,
   "[php]": {
        "editor.defaultFormatter": "junstyle.php-cs-fixer"
   },
}

PHPStan

付费替代品vscode 插件 PHP Intelephense 支持代码自动提示,跳转追踪和语法检测 99RMB永久授权

laravel版本 Larastan github 也是基于phpstan开发的

Laravel-ide-helper

安装

composer require --dev barryvdh/laravel-ide-helper

如果需要为模型生成字段,需要安装如下扩展包,参考文章是laravel5.5版本 跟laravel-ide-helper版本有关

laravel-ide-helper”: “^2.10″版本测试会自动安装doctrine/dbal 扩展包

composer require doctrine/dbal

生成配置文件 config目录下会生成ide-helper.php

php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config

为Facades生成文档提示,会在项目根目录下生成_ide_helper.php,可以根据团队需要决定是否将该文件加入.gitignore

php artisan ide-helper:generate

为模型生成注释,要在创建完成模型文件和数据迁移文件运行之后,若数据库字段有注释,则模型字段也会有注释

#不加命令参数给所有模型生成注释
php artisan ide-helper:models
#包含指定模型
php artisan ide-helper:models "App\Models\ExcelDemo"
#忽略指定模型
php artisan ide-helper:models --ignore="App\Models\BaseModel"
#会提示是否覆盖原模型,不覆盖会在根目录下生成_ide_helper_models.php文件,推荐no选项
 Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead (yes/no) [no]:
 > 

使用 Travis-CI 做自动化测试

使用 StyleCI 自动修复代码格式

编辑器相关插件如下

vscode 常用插件及配置

左下角齿轮图标 >> Extension 打开扩展商品搜索即可安装

主题图标

vscode-icons 图标

Atom One Dark Theme 主题

语言包

Chinese (Simplified) Language Pack for Visual Studio Code

修改侧边栏样式

CSS文件目录:/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.css

查找 .part>.content 如图修改字体

配置文件

MySQL Syntax 语法高亮显示

NGINX Configuration Language Support 语法高亮和自动补全

Apache Conf 语法高亮

Linux Desktop File support 桌面配置语法高亮

EditorConfig for VS Code 编码风格统一配置 editorconfig文档

DotENV .env格式配置文件 高亮显示 node php的部分框架都可以用

远程插件

Remote Development 远程开发插件

Remote – Containers 链接远程docker容器

Remote – SSH ssh远程终端工具

Remote – SSH: Editing Configuration Filesms 便捷ssh配置文件

其它插件

TODO Highlight TODO 标注功能的,如你写到某一部分的代码时,其中部分的功能需要稍后再来实现,这是就可以在对应的代码处添加一个 TODO 类型的注释

search-online 在线翻译和搜索插件

GitLens — Git supercharged git可视化工具,显示代码作者和提交历史

Project Manager ctrl + shift + p 如图使用

PHP插件

PHP Intelephense 代码提示,语法检测,psr12格式化,点击自动跳转追赠方法,显示内置API的官方文档,点击直接跳转官方文档提升查手册效率

可以免费使用,有付费功能,购买后输入key

设置PHP版本

出现未定义 undefined 错误 需要到如下选项配置支持。laravel框架需要 安装laravel ide help扩展包 swoole 也需要安装swoole ide help

php-cs-fixer 代码格式化插件 ,也可以项目中使用通过composer安装

    "php-cs-fixer.executablePath": "php-cs-fixer",
    "php-cs-fixer.onsave": false,
    "php-cs-fixer.rules": "@PSR1,@PSR1,@Symfony",
    "php-cs-fixer.config": ".php_cs;.php_cs.dist",
    "php-cs-fixer.allowRisky": false,
    "php-cs-fixer.pathMode": "override",
    "php-cs-fixer.exclude": [],
    "php-cs-fixer.autoFixByBracket": true,
    "php-cs-fixer.autoFixBySemicolon": false,
    "php-cs-fixer.formatHtml": false,
    "php-cs-fixer.documentFormattingProvider": true,

多个格式化插件冲突时 通过如下配置,设置默认格式化插件

"[php]": {
        "editor.defaultFormatter": "junstyle.php-cs-fixer"
 },

PHP DocBlocker PHP注释插件

配置作者和邮箱,@author触发

"php-docblocker.author": {
    "name": "yangliuan",
    "email": "yangliuancn@foxmail.com"
 },

laravel框架插件

laravel goto view 点击跳转 视图

laravel-goto-controller 点击跳转到控制器

Laravel Snippets laravel语句提示

Laravel Blade Snippets Blade模板语法提示

laravel8.x版本如下方式写路由时可以不使用此插件

Javascript

Vetur

ESLint

    "eslint.validate": [
        "javascript",
        "javascriptreact",
        "vue"
    ],
    "eslint.options": {
        "plugins": [
            "html"
        ]
    },

搜索时包含指定目录

点击替换下的…按钮可以输入要包含和排出的文件

shell

shellcheck

详细配置见扩展详情页或github

开发环境和开发工具整理篇

Database Tools

mysql workbench | navicat 15 激活

PHP

开发环境 oneinstack

Javascript

开发环境 nvm

接口文档工具

showDoc php开发的几口文档管理工具,简介,支持shell脚本通过注释自动生成接口文档

YAPI YAPI是由去哪儿网移动架构组开发的可视化接口管理工具

参考链接 https://juejin.cn/post/6844903874046722055

Swagger

Eolinker

ApiDoc

IDE

vscode 常用插件

Git常用命令

Git中文文档

设置与配置

git config --list 列出所有git配置项

git config --global user.name "yangliuan" 配置用户名

git config --global user.email "yangliuancn@foxmail.com" 配置用户邮箱

git config --global core.editor vim 配置默认编辑器

git config --global core.filemode false 所有版本库忽略文件权限跟踪

git config core.filemode false 当前版本库忽略文件权限跟踪

建立版本库

git init 新建项目

git clone 从线上拉取一个项目

git clone -b 分支名称 仓库地址 [文件夹名称]     克隆指定分支  文件夹名称选填不需要重命名的时候不填

git add . 监控工作区的状态树,把工作区的所有变化提交到暂存区包括文件内容修改(modified)以及新文件(new),但不包括被删除的文件

git add -u  仅监控已经被add的文件(即tracked file)他会将被修改的文件提交到暂存区

git add -A 是上面两个功能的合并(git add --all的缩写)

提交代码

git commit -m "注释"

git push 推送

git push origin  [branch] 推送到远程指定仓库

git push -u origin master  推送到远程指定仓库并设置为默认推送分支

git pull 拉取

git pull origin [branch] 拉去远程代码

git pull = git fetch + git merge

git pull --rebase = git fetch + git rebase

git rebase用于把一个分支的修改合并到当前分支。

仓库管理

git remote rename oldname newname 重命名仓库

git remote rm name  删除仓库

git remote set-url origin git@xxxxx.git

更换仓库出现如下错误,原因是两个分支是两个不同的版本,具有不同的提交历史

Git :fatal: refusing to merge unrelated histories
//使用如下命令其强制合并
git pull origin master --allow-unrelated-histories
如果还有问题,出现合并冲突之类的
删除本地仓库中的.git文件 重新clone新仓库到新目录,将代码文件和.git文件移动到旧的本地仓库中

分支管理

git push origin --delete branchname 删除远程分支

git checkout branchname 切换分支

git branch -D branchname 删除本地分支

git checkout -b branchname 在当前分支基础上新建分支

git push --set-upstream origin branchname 将新建的本地分支推送到远程并关联

拉取所有远程分支

git clone只能clone远程库的master分支,无法clone所有分支,解决办法如下:

找一个干净目录,假设是git_work
cd git_work
git clone http://myrepo.xxx.com/project/.git ,这样在git_work目录下得到一个project子目录
cd project
git branch -a,列出所有分支名称如下:
remotes/origin/dev
remotes/origin/release
git checkout -b dev origin/dev,作用是checkout远程的dev分支,在本地起名为dev分支,并切换到本地的dev分支
git checkout -b release origin/release,作用参见上一步解释
git checkout dev,切换回dev分支,并开始开发。
git tag 列出所有tag

git tag -a 20190815 -m "注释" f8a5b56af1e962744d3dbecb19d971496715b260
指定commit 打标签 

git show xxx   查看指定名称tag

git push origin --tags  推送本地所有tag

git push origin [tagName]  推送单个tag

git clone --branch [tags标签] [git地址] 拉去指定tag的项目

git clone -b 标签名称 --depth=1 git地址 拉去指定tag的项目 --depth 表示克隆深度, 1 表示只克隆最新的版本. 因为如果项目迭代的版本很多, 克隆会很慢

强制回退到制定版本

git log 查看日志,找到你要回退的版本查看commit 编号

git reset --hard commit编号 回退到指定版本

git push -f -u origin master 强制提交到master分支,注意保存版本代码,回退之后无法恢复,master分支可以替换成你要回退的远程分支

注意多个本地仓库时,都需要回退版本。比如你开发环境回退到xxx然后推送到远程仓库,此时,要在生成和测试环境上同意做回退操作保持和远程仓库一致,才能继续更新。

删除文件或目录

#--cached不会把本地的dirname删除
git rm -r --cached dirname

git commit -m 'delete dir'

git push -u origin master

将被.gitignore文件所忽略的文件从版本库中删除

git rm -r --cached .
git add .
git commit
git push  -u origin master

空目录内创建.gitkeep文件保证目录被纳入版本管理

使用git在本地新建一个分支后,需要做远程分支关联。如果没有关联,git会在下面的操作中提示你显示的添加关联。

关联目的是在执行git pull, git push操作时就不需要指定对应的远程分支,你只要没有显示指定,git pull的时候,就会提示你。

git branch --set-upstream-to=origin/remote_branch  your_branch

其中,origin/remote_branch是你本地分支对应的远程分支;your_branch是你当前的本地分支

git config advice.objectNameWarning false 待完善

其它命令

git symbolic-ref --short -q HEAD  打印当前分支名称,shell脚本用的上

git submodul

git submodule 的使用

FAQ

1错误信息

warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

解决方案

git config --global pull.ff only

参考
https://stackoverflow.com/questions/62653114/how-to-deal-with-this-git-warning-pulling-without-specifying-how-to-reconcile

指令参考

上述文件转自EASY微博

https://learnku.com/articles/61909