2017-07-05 26 views
0

必要なPHP CodeSnifferルールを見つけるにはどうすればよいですか?例えばPHP CodeSnifferルールの検索

私は代わりに、それはこのようにする必要があり、この

function asd($var, $var2 = '') { 
    $array = [ $arg, $arg2 ]; 
} 

のように間隔を置いて配置されていないために、関数の引数と配列をしたいと思います:

function asd($var, $var2 = '') { 
    $array = [$arg, $arg2]; 
} 

そして、私はphpcbfのことができるようにしたいのですがこれらを修正しますが、私はどのようにルールを見つけるのか分かりません。

答えて

0

PHPCSルールのドキュメントはありません(私はこの日までに回ります)ので、必要なルールを特定する唯一の方法は、いくつかのサンプルコードでPHPCSを実行することです。この例では

、私はtemp.phpファイルにあなたのサンプルの悪いコードを入れている:

<?php 
function asd($var, $var2 = '') { 
    $array = [ $arg, $arg2 ]; 
} 

は、その後、私は付属の標準を使用して、その上にPHPCSを実行します。 -sコマンドライン引数を使用して、スニフコードも確認できるようにします。この出力が得られます:

$ phpcs temp.php --standard=Generic,Squiz,PEAR,PSR2,Zend -s 

FILE: temp.php 
--------------------------------------------------------------------------------------------------------------------------------------------- 
FOUND 17 ERRORS AND 5 WARNINGS AFFECTING 4 LINES 
--------------------------------------------------------------------------------------------------------------------------------------------- 
1 | ERROR | [ ] The PHP open tag does not have a corresponding PHP close tag (Generic.PHP.ClosingPHPTag.NotFound) 
1 | ERROR | [ ] Missing file doc comment (Squiz.Commenting.FileComment.Missing) 
2 | WARNING | [ ] The method parameter $var is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found) 
2 | WARNING | [ ] The method parameter $var2 is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found) 
2 | WARNING | [ ] Consider putting global function "asd" in a static class (Squiz.Functions.GlobalFunction.Found) 
2 | ERROR | [ ] Missing file doc comment (PEAR.Commenting.FileComment.Missing) 
2 | ERROR | [ ] Missing function doc comment (Squiz.Commenting.FunctionComment.Missing) 
2 | ERROR | [x] Expected 2 blank lines before function; 0 found (Squiz.WhiteSpace.FunctionSpacing.Before) 
2 | ERROR | [ ] Missing function doc comment (PEAR.Commenting.FunctionComment.Missing) 
2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found 
    |   |  (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen) 
2 | WARNING | [ ] Variable "var2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers) 
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found 
    |   |  (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose) 
2 | ERROR | [x] Opening brace should be on a new line (Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine) 
2 | ERROR | [x] Opening brace should be on a new line (Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine) 
2 | ERROR | [x] Opening brace should be on a new line (PEAR.Functions.FunctionDeclaration.BraceOnSameLine) 
3 | ERROR | [x] Tabs must be used to indent lines; spaces are not allowed (Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed) 
3 | ERROR | [x] Short array syntax is not allowed (Generic.Arrays.DisallowShortArraySyntax.Found) 
3 | ERROR | [x] Array with multiple values cannot be declared on a single line (Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed) 
3 | WARNING | [ ] Variable "arg2" contains numbers but this is discouraged (Zend.NamingConventions.ValidVariableName.ContainsNumbers) 
4 | ERROR | [x] Expected //end asd() (Squiz.Commenting.ClosingDeclarationComment.Missing) 
4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found 
    |   |  (Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose) 
4 | ERROR | [x] File must not end with a newline character (Generic.Files.EndFileNoNewline.Found) 
--------------------------------------------------------------------------------------------------------------------------------------------- 
PHPCBF CAN FIX THE 12 MARKED SNIFF VIOLATIONS AUTOMATICALLY 
--------------------------------------------------------------------------------------------------------------------------------------------- 

Time: 84ms; Memory: 8Mb 

次に、保存したいメッセージを選択します。おそらく、これらの二つの修正可能なエラー:

2 | ERROR | [x] Expected 0 spaces between opening bracket and argument "$var"; 1 found 
    |   |  (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen) 
2 | ERROR | [x] Expected 0 spaces between argument "$var2" and closing bracket; 1 found 
    |   |  (Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose) 

ですから、カスタム標準で全体Squiz.Functions.FunctionDeclarationArgumentSpacingスニフを含めてみてください、あなたは結果を好きかどうかを確認することがあります。もしそうでなければ、あなたはその2つのメッセージをあなたの標準に入れることができ、それは嗅覚の残りの部分を無視します。

ショート・アレイの先頭と最後のスペースにエラーが報告されていないことがわかります。これは、これをチェックするPHPCSに含まれているスニフがないことを示しています。そのため、スニッフィングを実行する場合はカスタムスニフを書き込む必要があります。

これは明らかにこの情報を取得するためのきわめて簡単な方法ではなく、ドキュメントははるかに優れていますが、コードをチェックするためにどのスニフを使うことができるかを考えています。それを答えとして投稿する。うまくいけばそれは少し助けてください。

+0

ありがとうございました。私は自分の嗅覚を書こうとしましたが、APIの仕組みに関するドキュメントは見つかりませんでした。私はいくつかの既存の嗅覚を使って、必要なものを実行しました。私は検索と置き換えで残りをやっていると思います。 – user1340531