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に含まれているスニフがないことを示しています。そのため、スニッフィングを実行する場合はカスタムスニフを書き込む必要があります。
これは明らかにこの情報を取得するためのきわめて簡単な方法ではなく、ドキュメントははるかに優れていますが、コードをチェックするためにどのスニフを使うことができるかを考えています。それを答えとして投稿する。うまくいけばそれは少し助けてください。
ありがとうございました。私は自分の嗅覚を書こうとしましたが、APIの仕組みに関するドキュメントは見つかりませんでした。私はいくつかの既存の嗅覚を使って、必要なものを実行しました。私は検索と置き換えで残りをやっていると思います。 – user1340531