tuannda/php-coding-standards

dev-main 2022-02-20 00:27 UTC

This package is auto-updated.

Last update: 2024-09-20 05:54:26 UTC


README

需求

PHP_CodeSniffer 3. (3.6 或更高版本)。

全局安装(推荐)

使用以下任意方法全局安装 PHP_CodeSniffer。如果已经安装,请跳过此步骤。

安装完成后,您应该在命令行上能够执行 phpcs -i

您应该会看到类似以下内容:

已安装的编码规范包括 MySource、PEAR、PSR1、PSR2、Squiz 和 Zend。

(推荐)使用 composer...

composer global require tuannda/php-coding-standard

或克隆此仓库...

git clone -b master --depth 1 https://github.com/tuannda/php-coding-standard.git.

或下载。

注意安装路径。

(推荐)在 'php_codesniffer/src/Standards/' 中创建 'php-coding-standard/CodingStandard' 目录的符号链接,例如:

ln -s ~/Documents/Projects/php-coding-standard/CodingStandard ~/.composer/vendor/squizlabs/php_codesniffer/src/Standards/CodingStandard

或复制 CodingStandard 目录到 php_codesniffer/src/Standards/

现在执行 phpcs -i 应该会显示已安装 CodingStandard,例如:

已安装的编码规范包括 CodingStandard、MySource、PEAR、PSR1、PSR2、Squiz 和 Zend。

现在您应该能够在您选择的插件/编辑器/IDE中将 'CodingStandard' 设置为 phpcs 标准。

为单个项目安装 composer

cd /Path/To/MyProject

composer require tuannda/php-coding-standard

在编辑器/插件配置中设置 'phpcs 标准路径' 和 'phpcbf 标准路径' 为

'/Path/To/MyProject/vendor/php-coding-standard/CodingStandard/ruleset.xml'

命令行使用

检查错误和警告(报告)。

单个文件...

phpcs /Path/To/MyFile.php --standard='/Path/To/php-coding-standard/CodingStandard/ruleset.xml'

如果全局安装。

phpcs /Path/To/MyFile.php --standard=CodingStandard

目录(递归)。

phpcs /Path/To/MyProject --standard='/Path/To/php-coding-standard/CodingStandard/ruleset.xml'

如果全局安装。

phpcs /Path/To/MyProject --standard=CodingStandard

修复可修复的错误。

单个文件。

phpcbf /Path/To/MyFile.php --standard='/Path/To/php-coding-standard/CodingStandard/ruleset.xml'

如果全局安装。

phpcbf /Path/To/MyFile.php --standard=CodingStandard

目录(递归)。

phpcbf /Path/To/MyProject --standard='/Path/To/php-coding-standard/CodingStandard/ruleset.xml'

如果全局安装。

phpcbf /Path/To/MyProject --standard=CodingStandard

在 Gitlab-CI 上运行

stylelint:
stage: stylelint
tags:
- gitlab-runner
only:
- merge_requests
script:
- phpcs --config-set default_standard /Path/To/php-coding-standard/CodingStandard/
- echo CI_COMMIT_SHA=${CI_COMMIT_SHA}
- echo CI_MERGE_REQUEST_TARGET_BRANCH_NAME=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
- git fetch origin ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
- FILES=$(git diff --name-only ${CI_COMMIT_SHA} origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} | grep '\.php'$)
- if [ ! -z "$FILES" ]; then echo $FILES | xargs phpcs; fi
when: always

在 Windows 上将 crlf 更改为 lf

git config --global core.autocrlf input
git config --global core.safecrlf input