Skip to content

Commit 772e7d8

Browse files
jrfnldingo-d
authored andcommitted
✨ New Universal.WhiteSpace.PrecisionAlignment sniff
New sniff to enforce indentation to always be a multiple of a tabstop, i.e. disallow precision alignment. For space-based standards, the `Generic.WhiteSpace.DisallowTabIndent` sniff (unintentionally/incorrectly) already removes precision alignment. For tab-based standards, the `Generic.WhiteSpace.DisallowSpaceIndent` sniff, however, does not remove precision alignment. With that in mind, this sniff is mostly intended for standards which demand tabs for indentation. In rare cases, spaces for precision alignment can be intentional and acceptable, but more often than not, precision alignment is a typo. Notes: * This sniff does not concern itself with tabs versus spaces. Use the PHPCS native `Generic.WhiteSpace.DisallowTabIndent` or the `Generic.WhiteSpace.DisallowSpaceIndent` sniffs for that. * When using this sniff with tab-based standards, please ensure that the `tab-width` is set and either don't set the `$indent` property or set it to the tab-width (or a multiple thereof). * Precision alignment *within* text strings (multi-line text strings, heredocs, nowdocs) will NOT be flagged by this sniff. * Precision alignment in (trailing) whitespace on otherwise blank lines will not be flagged by default. Most standards will automatically remove trailing whitespace anyway using the `Squiz.WhiteSpace.SuperfluousWhitespace` sniff. If the Squiz sniff is not used, set the `ignoreBlankLines` property to `false` to enable reporting for this. * The sniff also supports an option to ignore precision alignment in specific situations, like for multi-line chained method calls. **Note**: the fixer works based on "best guess" and may not always result in the desired indentation. Combine this sniff with the `Generic.WhiteSpace.ScopeIndent` sniff for more precise indentation fixes. If so desired, the fixer can be turned off by including the rule in a standard like so: `<rule phpcs-only="true" ref="Universal.WhiteSpace.PrecisionAlignment"/>` As it is, the sniff supports both PHP, as well as JS and CSS, though the support for JS and CSS should be considered incidental and will be removed once PHPCS 4.0 will be released. Includes fixer. Includes unit tests. Includes documentation.
1 parent c1f8436 commit 772e7d8

File tree

50 files changed

+3268
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3268
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<documentation title="Precision Alignment">
3+
<standard>
4+
<![CDATA[
5+
Detects when the indentation is not a multiple of a tab-width, i.e. when precision alignment is used.
6+
]]>
7+
</standard>
8+
<code_comparison>
9+
<code title="Valid: indentation equals (a multiple of) the tab width.">
10+
<![CDATA[
11+
// Code samples presume tab width = 4.
12+
<em>[space][space][space][space]</em>$foo = 'bar';
13+
14+
<em>[tab]</em>$foo = 'bar';
15+
]]>
16+
</code>
17+
<code title="Invalid: precision alignment used, indentation does not equal (a multiple of) the tab width.">
18+
<![CDATA[
19+
// Code samples presume tab width = 4.
20+
<em>[space][space]</em>$foo = 'bar';
21+
22+
<em>[tab][space]</em>$foo = 'bar';
23+
]]>
24+
</code>
25+
</code_comparison>
26+
</documentation>

0 commit comments

Comments
 (0)