Documentation

PrecisionAlignmentSniff
in package
implements Sniff

FinalYes

Detects when the indentation is not a multiple of a tab-width, i.e. when precision alignment is used.

In rare cases, spaces for precision alignment can be intentional and acceptable, but more often than not, precision alignment is a typo.

Notes:

  • 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.
  • Precision alignment within text strings (multi-line text strings, heredocs, nowdocs) will NOT be flagged by this sniff.
  • The fixer works based on "best guess" and may not always result in the desired indentation.
  • This fixer will use tabs or spaces based on whether tabs were present in the original indent. Use the PHPCS native Generic.WhiteSpace.DisallowTabIndent or the Generic.WhiteSpace.DisallowSpaceIndent sniff to clean up the results if so desired.
Tags
since
1.0.0

Table of Contents

Interfaces

Sniff

Properties

$ignoreAlignmentBefore  : array<string|int, string>
Allow for providing a list of tokens for which (preceding) precision alignment should be ignored.
$ignoreBlankLines  : bool
Whether or not potential trailing whitespace on otherwise blank lines should be examined or ignored.
$indent  : int|null
The indent used for the codebase.
$supportedTokenizers  : array<string|int, string>
A list of tokenizers this sniff supports.
$tabWidth  : int
The --tab-width CLI value that is being used.
$tokensToCheck  : array<int|string, int|string>
Whitespace tokens and tokens which can contain leading whitespace.

Methods

process()  : int
Processes this test, when one of its tokens is encountered.
register()  : array<string|int, int|string>
Returns an array of tokens this test wants to listen for.
getReplacement()  : string
Get the whitespace replacement. Respect tabs vs spaces.

Properties

$ignoreAlignmentBefore

Allow for providing a list of tokens for which (preceding) precision alignment should be ignored.

public array<string|int, string> $ignoreAlignmentBefore = []

By default, precision alignment will always be flagged.

Example usage:

<rule ref="Universal.WhiteSpace.PrecisionAlignment">
   <properties>
       <property name="ignoreAlignmentBefore" type="array">
           <!-- Ignore precision alignment in inline HTML -->
           <element value="T_INLINE_HTML"/>
           <!-- Ignore precision alignment in multiline chained method calls. -->
           <element value="T_OBJECT_OPERATOR"/>
       </property>
   </properties>
</rule>
Tags
since
1.0.0

$ignoreBlankLines

Whether or not potential trailing whitespace on otherwise blank lines should be examined or ignored.

public bool $ignoreBlankLines = true

Defaults to true, i.e. ignore blank lines.

It is recommended to only set this to false if the standard including this sniff does not include the Squiz.WhiteSpace.SuperfluousWhitespace sniff (which is included in most standards).

Tags
since
1.0.0

$indent

The indent used for the codebase.

public int|null $indent = null

This property is used to determine whether something is indentation or precision alignment. If this property is not set, the sniff will look to the --tab-width CLI value. If that also isn't set, the default tab-width of 4 will be used.

Tags
since
1.0.0

$supportedTokenizers

A list of tokenizers this sniff supports.

public array<string|int, string> $supportedTokenizers = ['PHP', 'JS', 'CSS']
Tags
since
1.0.0

$tokensToCheck

Whitespace tokens and tokens which can contain leading whitespace.

private array<int|string, int|string> $tokensToCheck = [\T_WHITESPACE => \T_WHITESPACE, \T_INLINE_HTML => \T_INLINE_HTML, \T_DOC_COMMENT_WHITESPACE => \T_DOC_COMMENT_WHITESPACE, \T_COMMENT => \T_COMMENT, \T_END_HEREDOC => \T_END_HEREDOC, \T_END_NOWDOC => \T_END_NOWDOC]

A few additional tokens will be added to this list in the register() method.

Tags
since
1.0.0

Methods

process()

Processes this test, when one of its tokens is encountered.

public process(File $phpcsFile, int $stackPtr) : int
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the current token in the stack passed in $tokens.

Tags
since
1.0.0
Return values
int

Integer stack pointer to skip the rest of the file.

register()

Returns an array of tokens this test wants to listen for.

public register() : array<string|int, int|string>
Tags
since
1.0.0
Return values
array<string|int, int|string>

getReplacement()

Get the whitespace replacement. Respect tabs vs spaces.

private getReplacement(int $length, string|null $origContent) : string
Parameters
$length : int

The target length of the replacement.

$origContent : string|null

The original token content without tabs replaced (if available).

Return values
string

        
On this page

Search results