如何使用PHPCBF一次解决一个问题


How to use PHPCBF to fix one issue at a time?

我添加了一个PHPCS配置,以便在遗留项目中获得一些标准。

我正在使用PSR-2的宽松版本作为过渡步骤。现在,我想做的是慢慢地一个接一个地删除排除,并在每一步都将更改提交到Git中。

对于给定的配置,我该如何运行PHPCBF?

在我的规则集中,我有:

<arg name="tab-width" value="4"/>
<rule ref="PSR2">
    <exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
    <exclude name="Generic.Files.LineLength.TooLong"/>
    <exclude name="Generic.Formatting.DisallowMultipleStatements.SameLine"/>
    <exclude name="Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma"/>
    <exclude name="Generic.Functions.FunctionCallArgumentSpacing.SpaceBeforeComma"/>
    <!--<exclude name="Generic.PHP.LowerCaseConstant.Found"/>-->
    <!--<exclude name="Generic.PHP.LowerCaseKeyword.Found"/>-->
    <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    <exclude name="Generic.WhiteSpace.ScopeIndent"/>
    <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
    <exclude name="PSR2.Classes.ClassDeclaration.CloseBraceAfterBody"/>
    <exclude name="PSR2.Classes.ClassDeclaration.SpaceBeforeBrace"/>
    <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNotAlone"/>
    <exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>
    <exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace"/>
    <exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration.BreakIndent"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration.BreakNotNewLine"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration.SpaceBeforeColonCASE"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration.SpaceBeforeColonDEFAULT"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.MultipleArguments"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.CloseBracketLine"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.ContentAfterOpenBracket"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.Indent"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.SpaceBeforeCloseBrace"/>
    <exclude name="PSR2.Methods.FunctionCallSignature.SpaceBeforeCloseBracket"/>
    <exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
    <exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
    <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseParenthesis"/>
    <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword"/>
    <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>
    <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterBracket"/>
    <exclude name="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace"/>
    <exclude name="Squiz.ControlStructures.ForLoopDeclaration.NoSpaceAfterFirst"/>
    <exclude name="Squiz.ControlStructures.ForLoopDeclaration.NoSpaceAfterSecond"/>
    <exclude name="Squiz.ControlStructures.ForEachLoopDeclaration.SpaceAfterOpen"/>
    <exclude name="Squiz.ControlStructures.ForEachLoopDeclaration.SpaceBeforeClose"/>
    <exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpaceBeforeEquals"/>
    <exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpaceAfterDefault"/>
    <exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpenHint"/>
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction"/>
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.ContentAfterBrace"/>
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterBracket"/>
    <exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen"/>
    <exclude name="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose"/>
    <exclude name="Squiz.Scope.MethodScope.Missing"/>
    <exclude name="Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore"/>
    <exclude name="Squiz.WhiteSpace.ScopeClosingBrace.Indent"/>
    <exclude name="Squiz.WhiteSpace.SuperfluousWhitespace.EndLine"/>
    <exclude name="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
    <exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpaceBeforeCloseBrace"/>
    <exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment"/>
    <exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
    <exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
    <exclude name="PSR2.Files.EndFileNewline.NoneFound"/>
    <exclude name="PSR2.Files.EndFileNewline.TooMany"/>
</rule>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>

因此,我要问的是如何运行PHPCBF,例如"Generic.PHP.LowerCaseConstant.Found"。

到目前为止,我已经尝试了以下几种:

   ./www/vendor/bin/phpcbf www/application --sniffs=Generic.PHP.LowerCaseConstant.Found
   ./www/vendor/bin/phpcbf www/application --sniffs=Generic.Sniffs.PHP.LowerCaseConstant.Found
   ./www/vendor/bin/phpcbf www/application --sniffs=Generic.Sniffs.PHP.LowerCaseConstantSniff
   ./www/vendor/bin/phpcbf www/application --standard=Generic --sniffs=Generic.Sniffs.PHP.LowerCaseConstantSniff
   ./www/vendor/bin/phpcbf -w www/application --standard=Generic --sniffs=Generic.Sniffs.PHP.LowerCaseConstantSniff
   ./www/vendor/bin/phpcbf -w www/application --standard=generic --sniffs=Generic.Sniffs.PHP.LowerCaseConstantSniff

没有一个是正确的。他们只是生成PHPCBF的帮助文本。

您只能一次修复整个嗅探中的错误,而不能修复特定的错误消息。所以你必须运行:

./www/vendor/bin/phpcbf www/application --standard=Generic --sniffs=Generic.PHP.LowerCaseConstant

修复Generic.PHP.LowerCaseConstant嗅探报告的所有错误

值得一提的是,我想修复phpcs抛出的一个单独错误(我使用-s标志来显示嗅探代码(

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 291 | ERROR | [x] There must not be a space between the question mark and the type in nullable type declarations
     |       |     (PSR12.Functions.NullableTypeDeclaration.WhitespaceFound)

不要使用上面显示的确切代码,而是使用类似于的代码

./vendor/bin/phpcbf src/File.php --sniffs="PSR12.Functions.NullableTypeDeclaration"

从中获取嗅探名称

./vendor/bin/phpcs -e

例如

Generic (15 sniffs)
-------------------
  Generic.ControlStructures.InlineControlStructure
  Generic.Files.ByteOrderMark
  Generic.Files.LineEndings
  Generic.Files.LineLength
  Generic.Formatting.DisallowMultipleStatements
  Generic.Functions.FunctionCallArgumentSpacing
  Generic.NamingConventions.UpperCaseConstantName
  Generic.PHP.DisallowAlternativePHPTags
  Generic.PHP.DisallowShortOpenTag
  Generic.PHP.LowerCaseConstant
  Generic.PHP.LowerCaseKeyword
  Generic.PHP.LowerCaseType
  Generic.WhiteSpace.DisallowTabIndent
  Generic.WhiteSpace.IncrementDecrementSpacing
  Generic.WhiteSpace.ScopeIndent
PEAR (1 sniff)
---------------
  PEAR.Functions.ValidDefaultValue
PSR1 (3 sniffs)
---------------
  PSR1.Classes.ClassDeclaration
  PSR1.Files.SideEffects
  PSR1.Methods.CamelCapsMethodName
PSR2 (9 sniffs)
---------------
  PSR2.Classes.ClassDeclaration
  PSR2.Classes.PropertyDeclaration
  PSR2.ControlStructures.ElseIfDeclaration
  PSR2.ControlStructures.SwitchDeclaration
  PSR2.Files.ClosingTag
  PSR2.Files.EndFileNewline
  PSR2.Methods.FunctionCallSignature
  PSR2.Methods.FunctionClosingBrace
  PSR2.Methods.MethodDeclaration
Squiz (15 sniffs)
-----------------
  Squiz.Classes.ValidClassName
  Squiz.ControlStructures.ControlSignature
  Squiz.ControlStructures.ForEachLoopDeclaration
  Squiz.ControlStructures.ForLoopDeclaration
  Squiz.ControlStructures.LowercaseDeclaration
  Squiz.Functions.FunctionDeclaration
  Squiz.Functions.FunctionDeclarationArgumentSpacing
  Squiz.Functions.LowercaseFunctionKeywords
  Squiz.Functions.MultiLineFunctionDeclaration
  Squiz.Scope.MethodScope
  Squiz.WhiteSpace.CastSpacing
  Squiz.WhiteSpace.ControlStructureSpacing
  Squiz.WhiteSpace.ScopeClosingBrace
  Squiz.WhiteSpace.ScopeKeywordSpacing
  Squiz.WhiteSpace.SuperfluousWhitespace

通过使用上面示例代码中列出的排除规则,我能够在特定嗅探文件中的特定错误消息上运行phbcbf。您可以直接在phbcbf命令中使用xml文件

phpcbf --standard=path_to_your_ruleset_file.xml

试图通过在命令行中选择特定的sniff来运行phpcbf,但我无法做到这一点。

这是我在自定义规则集文件中拥有的唯一规则

<rule ref="Drupal.Commenting.FunctionComment">
  <exclude name="Drupal.Commenting.FunctionComment.Missing"/>
  <exclude name="Drupal.Commenting.FunctionComment.MissingFile"/>
  <exclude name="Drupal.Commenting.FunctionComment.MissingParamType"/>
  <exclude name="Drupal.Commenting.FunctionComment.WrongStyle"/>
  <exclude name="Drupal.Commenting.FunctionComment.ParamCommentNewLine"/>
  <exclude name="Drupal.Commenting.FunctionComment.IncorrectParamVarName"/>
  <exclude name="Drupal.Commenting.FunctionComment.SpacingAfter"/>
  <exclude name="Drupal.Commenting.FunctionComment.SpacingAfterParamType"/>
</rule>

请注意,我并没有明确排除所有可能的错误,而是只排除了我运行该程序的代码库中存在的错误。我唯一想修复的错误是我没有包含在排除列表中的错误。