Php_codesniffer: Which rules to use for whitespace only fixes?

Created on 3 Aug 2017  路  3Comments  路  Source: squizlabs/PHP_CodeSniffer

I would like to fix these whitespace issues:

  • replace tabs with spaces
  • indentation level correction
  • remove trailing whitespace chars
  • set newline type to unix
  • replace more than two empty lines to just one
  • place braces on single line on the line after the corresponding function keyword
  • set file encoding to UTF-8

Which rules do I have to use? I am having a hard time discovering the appropriate rules in the src folder.

If I may add a feature request: a searchable online catalog and rules.xml generator (with comparison feature) would be a great addition to phpcs!

Thanks for your attention!

Awaiting Feedback Question

All 3 comments

I am having a hard time discovering the appropriate rules in the src folder.

You may find the -e option useful. If you run it against each standard, it will provide you with a list of all included sniffs and should give a good starting point on your path to discover the right sniffs for you.
Try phpcs -e --standard=PSR2 as an example.

As for the specific rules you are asking for:

  • replace tabs with spaces

Generic.WhiteSpace.DisallowTabIndent

  • indentation level correction

Try: Generic.WhiteSpace.ScopeIndent and have a look at the customizable properties for it: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericwhitespacescopeindent

  • remove trailing whitespace chars

Squiz.WhiteSpace.SuperfluousWhitespace Also see the customizable properties for it: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#squizwhitespacesuperfluouswhitespace

  • set newline type to unix

Generic.Files.LineEndings and have a look at the customizable properties for it: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericfileslineendings

  • replace more than two empty lines to just one

Squiz.WhiteSpace.SuperfluousWhitespace

  • place braces on single line on the line after the corresponding function keyword

Generic.Functions.OpeningFunctionBraceKernighanRitchie, and again have a look at the customizable properties: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericfunctionsopeningfunctionbracekernighanritchie

  • set file encoding to UTF-8

I'm not sure if there is a sniff for that. Though there is one to remove BOMs from files: Generic.Files.ByteOrderMark

Hope this helps.

Thank you, thank you, thank you very much and greetings to my fav city Amsterdam!
Have a nice weekend!

If this answered your question sufficiently, can you close the issue please ?

Was this page helpful?
0 / 5 - 0 ratings