一個配對的編程合作夥伴,用於編寫更好的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 瓦特納貝庫瑪