Documentation

OperatorSpacingSniff extends OperatorSpacingSniff

FinalYes

Verify operator spacing, uses the Squiz sniff, but additionally also sniffs for the `!` (boolean not) and the boolean and logical and/or operators.

"Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators."

Tags
link
https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#space-usage
link
https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
since
0.1.0
since
0.3.0

This sniff now has the ability to fix the issues it flags.

since
0.12.0

This sniff used to be a copy of a very old and outdated version of the upstream sniff. Now, the sniff defers completely to the upstream sniff, adding just the T_BOOLEAN_NOT and the logical operators (&& and the like) - via the registration method and changing the value of the customizable $ignoreNewlines property.

since
0.13.0

Class name changed: this class is now namespaced.

Last verified with base class June 2023 at commit 085b1e091b0f2e451333c0bc26dd50bba39402c4.

Table of Contents

Properties

$ignoreNewlines  : bool
Allow newlines instead of spaces.
$ignoreSpacingBeforeAssignments  : bool
Don't check spacing for assignment operators.
$supportedTokenizers  : array<string|int, mixed>
A list of tokenizers this sniff supports.

Methods

process()  : void|int
Processes this sniff, when one of its tokens is encountered.
register()  : array<string|int, mixed>
Returns an array of tokens this test wants to listen for.
isOperator()  : bool
Checks if an operator is actually a different type of token in the current context.

Properties

$ignoreNewlines

Allow newlines instead of spaces.

public bool $ignoreNewlines = true

N.B.: The upstream sniff defaults to false.

$ignoreSpacingBeforeAssignments

Don't check spacing for assignment operators.

public bool $ignoreSpacingBeforeAssignments = true

This allows multiple assignment statements to be aligned.

$supportedTokenizers

A list of tokenizers this sniff supports.

public array<string|int, mixed> $supportedTokenizers = ['PHP', 'JS']

Methods

process()

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

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

The current file being checked.

$stackPtr : int

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

Return values
void|int

Optionally returns a stack pointer. The sniff will not be called again on the current file until the returned stack pointer is reached. Return $phpcsFile->numTokens to skip the rest of the file.

register()

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

public register() : array<string|int, mixed>
Return values
array<string|int, mixed>

isOperator()

Checks if an operator is actually a different type of token in the current context.

protected isOperator(File $phpcsFile, int $stackPtr) : bool
Parameters
$phpcsFile : File

The current file being checked.

$stackPtr : int

The position of the operator in the stack.

Return values
bool

        
On this page

Search results