一个配对的编程合作伙伴,用于编写更好的PHP。 pahout意味着php mahout?
PHP在悠久历史中添加了各种功能。但是,由于其历史悠久,许多旧语法散布在互联网上。如果PHPER从他们那里学到,PHPER将不知道最新的PHP版本中存在的出色语法和功能。这是一件非常可悲的事情。
因此,我考虑过像一个配对编程合作伙伴这样的衬里,他告诉您一个很好的方法。它将帮助您与您一起编写更好的PHP。
但是,请不要基于Pahout的提示来修复所有现有代码。 Pahout是配对的编程合作伙伴。配对编程时,您不会检查所有现有代码,对吗?我的建议是仅检查新创建或修改的文件。例如,最好设置预先承诺的挂钩,如下所示:
#! /bin/sh
git diff --name-only | grep .php | xargs vendor/bin/pahout
if [ " $? " -ne 0] ; then
exit 1
fi Pahout需要以下环境:
您可以使用作曲家安装。
$ composer require --dev wata727/pahout
$ vendor/bin/pahout -V
通过使用Docker Image,您可以轻松尝试Pahout而不会影响当地环境。
$ docker run --rm -t -v $(pwd):/workdir wata727/pahout
您正在项目中使用PHP 7.1.8。您如何看待以下代码?
<?php
// Do something...
$ response = get_awesome_response ();
$ error = isset ( $ response [ ' error ' ]) ? $ response [ ' error ' ] : null ;
// Do something...也许这是一个熟悉的代码。但是,如果您知道无效的合并操作员,您将写下类似的东西:
<?php
// Do something...
$ response = get_awesome_response ();
$ error = $ response [ ' error ' ] ?? null ; # Great!
// Do something...Pahout将支持这种重写。尝试在上面的示例上运行。
$ pahout --php-version 7.1.8 test.php
test.php:8
NullCoalescingOperator: Use the null coalecing operator instead of the ternary operator. [https://github.com/wata727/pahout/blob/master/docs/NullCoalescingOperator.md]
1 files checked, 1 hints detected.
Pahout告诉您可以在哪里使用Null合并操作员!有关Pahout提供的提示列表,请参阅此文档。
如果要分析多个文件,则可以指定多个文件。
$ pahout --php-version 7.1.8 test.php test2.php ...
如果指定目录名称,则将涵盖该目录下的所有.php文件。
$ pahout --php-version 7.1.8 src
您可以从命令行更改配置。
$ pahout --help
Description:
A pair programming partner for writing better PHP
Usage:
check [options] [--] [<files>...]
Arguments:
files List of file names or directory names to be analyzed
Options:
--php-version[=PHP-VERSION] Target PHP version [default: runtime version]
--ignore-tools[=IGNORE-TOOLS] Ignore tool types [default: Nothing to ignore] (multiple values allowed)
--ignore-paths[=IGNORE-PATHS] Ignore files and directories [default: Nothing to ignore] (multiple values allowed)
--extensions[=EXTENSIONS] File extensions to be analyzed [default: php] (multiple values allowed)
--vendor[=VENDOR] Check vendor directory [default: false]
-f, --format[=FORMAT] Output format [default: "pretty", possibles: "pretty", "json"]
-c, --config[=CONFIG] Config file path [default: ".pahout.yaml"]
--only-tools[=ONLY-TOOLS] Check only the given tool types (multiple values allowed)
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
您还可以通过准备称为.pahout.yaml的配置文件来更改配置。
php_version : 7.0.0
ignore_tools :
- ShortArraySyntax
ignore_paths :
- tests
- bin
extensions :
- php
- module
- inc
vendor : true指定项目的PHP版本。默认值是运行时版本。 Pahout使用此版本选择工具。
在Pahout中,生成的提示称为“工具”。您可以指定要忽略的工具名称。请查看文档以获取工具名称列表。
与ignore_tools相反,指定要检查的工具。
您可以指定要忽略的文件或目录。如果指定了目录名称,则该目录下的所有文件都将忽略。
要分析的文件扩展名。默认值仅是php 。
您可以设置是否忽略供应商目录。
注意:默认情况下忽略了供应商目录。通常,您无需检查供应商目录。
指定输出格式。目前只有pretty json得到支持。
指定配置文件名。当您想在配置文件中使用.pahout.yaml以外的文件名时,这很有用。
使用注释,您可以忽略特定行的提示。指定要使用@rebel忽略的工具名称。
<?php
/** @rebel NullCoalescingOperator */
$ error = isset ( $ response [ ' error ' ]) ? $ response [ ' error ' ] : null ;以下评论将以相同的方式工作。
<?php
/**
* @rebel NullCoalescingOperator
*/
$ error = isset ( $ response [ ' error ' ]) ? $ response [ ' error ' ] : null ;
$ error = isset ( $ response [ ' error ' ]) ? $ response [ ' error ' ] : null ; # @rebel NullCoalescingOperator 瓦特纳贝库玛