happytech / phpcs
Requires
- php: >=5.1.2
- squizlabs/php_codesniffer: ^2.6.1
This package is not auto-updated.
Last update: 2019-02-20 20:03:29 UTC
README
PHP_CodeSniffer是一组两个PHP脚本;主要的 phpcs 脚本对PHP、JavaScript和CSS文件进行标记,以检测违反定义的编码标准,第二个 phpcbf 脚本用于自动纠正编码标准违规。PHP_CodeSniffer是一个确保您的代码保持干净和一致的基本开发工具。
安装
-
在命令行中,进入 phpcs
Standards目录
在 Linux Mint 下,路径是
/usr/share/php/PHP/CodeSniffer/Standards/
-
检出此仓库
git clone git@github.com:Microclimat/phpcs.git Happy -
检查已安装的“Happy”编码标准
phpcs -i -
将Happy设置为默认标准
phpcs --config-set default_standard Happy -
完成!
选项 - 不必要求但推荐
默认情况下使用颜色输出
phpcs --config-set colors 1
默认隐藏警告
phpcs --config-set show_warnings 0
用法
启动分析
phpcs /path/to/code
自动修复错误
phpcbf /path/to/code
忽略
<?php
some_code();
// @codingStandardsIgnoreStart
this_will_be_ignored();
// @codingStandardsIgnoreEnd
some_other_code();
所有文档在这里 这里
仅限娱乐
用于显示所有包含错误的提交
phpcs --report=gitblame /path/to/code
在 IntelliJ IDEA 中注册 PHP Code Sniffer
打开设置对话框,然后在 PHP 节点下点击 Code Sniffer。在 PHP Code Sniffer 路径文本框中,指定 Code Sniffer 可执行文件 phpcs.bat 的位置。手动输入路径或点击浏览按钮浏览Button.png并选择对话框中打开的路径。为了确保 IntelliJ IDEA 和 Code Sniffer 之间的交互,即工具可以从 IntelliJ IDEA 中启动,IntelliJ IDEA 将从其中接收问题报告,请点击验证按钮。此验证等同于运行 phpcs --version 命令。如果验证成功通过,IntelliJ IDEA 将显示检测到的 Code Sniffer 版本信息。
将 PHP Code Sniffer 配置为 IntelliJ IDEA 检查
打开设置对话框,然后点击检查。在打开的检查页面上,在 PHP 节点下选择 PHP Code Sniffer 验证复选框。在页面右侧的窗格中,使用选项区域中的控件配置 PHP Code Sniffer 工具:从严重性下拉列表中选择 Code Sniffer 检查的严重程度。所选值确定 IntelliJ IDEA 将如何处理检测到的差异,并在检查结果中显示。在编码标准下拉列表中,指定要检查您的代码的编码风格。列表包含主 PHP_CodeSniffer 目录结构内安装的所有编码标准。使用预定义的编码标准之一,或选择自定义以指定自己的标准。可选地,选中忽略警告复选框以仅报告错误并抑制警告报告。此选项等同于 -n 命令行参数。
在 Sublime Text 上使用 PHP CS
请访问 http://benmatselby.github.io/sublime-phpcs/ 在 Win7 上:首选项 > 包设置 > Php Code Sniffer > 设置 - 用户
{
// Example for:
// - Windows 7
// - With phpcs and php-cs-fixer support
// We want debugging on
"show_debug": true,
// Only execute the plugin for php files
"extensions_to_execute": ["php"],
// Do not execute for twig files
"extensions_to_blacklist": ["twig.php"],
// Execute the sniffer on file save
"phpcs_execute_on_save": true,
// Show the error list after save.
"phpcs_show_errors_on_save": true,
// Show the errors in the gutter
"phpcs_show_gutter_marks": true,
// Show outline for errors
"phpcs_outline_for_errors": true,
// Show the errors in the status bar
"phpcs_show_errors_in_status": true,
// Show the errors in the quick panel so you can then goto line
"phpcs_show_quick_panel": true,
// Path to php on windows installation
// This is needed as we cannot run phars on windows, so we run it through php
"phpcs_php_prefix_path": "C:\\Program Files (x86)\\PHP\\php.exe",
// We want the fixer to be run through the php application
"phpcs_commands_to_php_prefix": ["Fixer"],
// PHP_CodeSniffer settings
// Yes, run the phpcs command
"phpcs_sniffer_run": true,
// And execute it on save
"phpcs_command_on_save": true,
// This is the path to the bat file when we installed PHP_CodeSniffer
"phpcs_executable_path": "C:\\Program Files (x86)\\PHP\\PEAR\\phpcs.bat",
// I want to run the PSR2 standard, and ignore warnings
"phpcs_additional_args": {
"--standard": "PSR2",
"-n": ""
},
// PHP-CS-Fixer settings
// Don't want to auto fix issue with php-cs-fixer
"php_cs_fixer_on_save": false,
// Show the quick panel
"php_cs_fixer_show_quick_panel": true,
// The fixer phar file is stored here:
"php_cs_fixer_executable_path": "C:\\Program Files (x86)\\PHP\\PEAR\\php-cs-fixer.phar",
// Additional arguments, run all levels of fixing
"php_cs_fixer_additional_args": {
},
// PHP Linter settings
// Yes, lets lint the files
"phpcs_linter_run": true,
// And execute that on each file when saved (php only as per extensions_to_execute)
"phpcs_linter_command_on_save": true,
// Path to php
"phpcs_php_path": "C:\\Program Files (x86)\\PHP\\php.exe",
// This is the regex format of the errors
"phpcs_linter_regex": "(?P<message>.*) on line (?P<line>\\d+)",
// PHP Mess Detector settings
// Not turning on the mess detector here
"phpmd_run": false,
"phpmd_command_on_save": false,
"phpmd_executable_path": "",
"phpmd_additional_args": {}
}
并使用您自己的值更改 PATH
使用 phpcs 与 GrumPHP(提交前的检查)
首先,您需要 grumphp:https://github.com/phpro/grumphp
按照说明进行操作,并将类型为 phpcs 的任务添加到您的 grumphp.yml 中(如下例所示进行编辑)。
您应该在文件中写下以下内容
parameters:
git_dir: .
bin_dir: vendor/bin
tasks:
phpcs:
standard: "vendor/happytech/phpcs/Happy"
show_warnings: true
tab_width: ~
whitelist_patterns: []
encoding: ~
ignore_patterns: []
sniffs: []
triggered_by: [php]
进入您项目的根目录,打开终端并输入
composer require --dev happytech/phpcs
这将添加 Happy convention 和 phpcs 到您的 vendors 并更新您的 composer.json
接下来,尝试提交!