How to use PHPCBF to fix one issue at a time?
Asked Answered
C

3

6

I've added a PHPCS configuration to get some standards into a legacy project.

I'm using a relaxed version of PSR-2 as an interim step. Now what I want to do, is slowly remove the exclusions, one by one, committing the changes into Git each step of the way.

How do I go about running PHPCBF for a given configuration?

Within my ruleset, I have:

<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"/>

So, what I am asking is how I run PHPCBF for, for example, 'Generic.PHP.LowerCaseConstant.Found'.

So far I have tried the following:

   ./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

None of which are correct. They just produce PHPCBF's help text.

Colcannon answered 6/7, 2015 at 14:4 Comment(0)
L
3

You can only fix the errors from an entire sniff at once and not a specific error message. So you'd have to run:

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

To fix all errors reported by the Generic.PHP.LowerCaseConstant sniff

Lucubrate answered 6/7, 2015 at 22:56 Comment(5)
How do you alter properties using this method? For example, I want to use PSR2.ControlStructures.ControlStructureSpacing with spacing values of 1 instead of 0. Easy to do inside ruleset.xml for detection with phpcs, but I can't figure it out for phpcbf. Thanks.Real
@Real You tell PHPCBF to use the same ruleset as PHPCS. So assuming your have a ruleset with this PSR2 sniff referenced in it, and your custom spacing values, if you want to fix just PSR2.ControlStructures.ControlStructureSpacing, you would run: phpcs /path/to/code --standard=/path/to/your/ruleset.xml --sniffs=PSR2.ControlStructures.ControlStructureSpacingLucubrate
Thanks @Greg! I got caught up in thinking of the standard var as 'PSR1', 'PSR2', 'Generic', etc. and overlooked that my ruleset qualified as valid input.Real
I've tried to run phpcbf --standard=PSR2 --sniffs=PEAR.Functions.FunctionCallSignature path/to/file.php, but got "ERROR: No sniffs were registered". If I change standard to PEAR, too much changes (sniffs) have applied.Burrows
@Burrows No sniffs were registered because the PSR2 standard does not use that sniff. You will need to use --standard=PEAR. I have just tested PHPCBF again and can confirm it will only fix the things that the PEAR.Functions.FunctionCallSignature finds when you use those arguments. Run the same command with phpcs to see the list of errors that will be fixed.Lucubrate
H
1

For what it's worth I wanted to fix an individual error that phpcs was throwing (I was using the -s flag to show the sniff code)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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)

Don't use the exact code shown above but instead something like this

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

Get the sniff names from

./vendor/bin/phpcs -e

e.g.

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
Halloween answered 18/4, 2022 at 16:48 Comment(0)
K
0

I was able to run phbcbf on a specific error message within a specific sniff file by using the exclude rules like you have listed in the sample code above. You can use the xml file directly in the phbcbf command

phpcbf --standard=path_to_your_ruleset_file.xml

Trying to run phpcbf by selecting specific sniffs in the command line did not allow me to do this though.

This was the only rule that I had in the custom ruleset file

<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>

Do note that I did not explicitly exclude all the possible errors but instead only excluded the errors that were present in the codebase I was running this for. The only error I wanted to fix was the one I did not include in the exclude list.

Karmenkarna answered 11/4, 2016 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.