当我以PSR2 ruleset.xml为例时,我看到许多评论块说<!-- checked in Files/SideEffectsSniff -->
以下是一小段摘录: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml
<!-- PHP code MUST use only UTF-8 without BOM. -->
<rule ref="Generic.Files.ByteOrderMark"/>
<!-- 2.3. Side Effects -->
<!-- A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both. -->
<!-- checked in Files/SideEffectsSniff -->
文件的其余部分没有类似于Files.SideEffectsSniff
的规则。
我的问题是,是否检查了副作用?如果是,那么什么规则对此负责?
感谢您的澄清。
答案 0 :(得分:1)
Short answer: Yes, side effects are checked by PSR1 and PSR2 and the sniff responsible is this one: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php and the code to import it into your own standard is PSR1.Files.SideEffects
.
First a note that the ruleset snippet you've posted is actually from PSR1, but PSR2 imports the entire PSR1 standard at the top of its ruleset.xml file, so the sniff is executed when running both standards.
When I wrote those ruleset.xml files, I used <rule>
tags to show when I was importing sniffs from other standards, and used comments to show when the sniff was included inside the standard itself.
In PHPCS, you can either import sniffs using a code, or include classes inside a Sniffs
folder in the same directory as the ruleset.xml file. PSR1 and PSR2 both have their own sniff classes, and these are imported automatically, which is why I don't need to use a <rule>
tag in the ruleset.