CommaSpacingSniff
in package
implements
Sniff
Check spacing around commas.
- Demands there is no whitespace between the preceeding code and the comma.
- Demands exactly one space or a new line after a comma.
- Demands that when there is a trailing comment, the comma follows the code, not the comment.
The following exclusions are in place:
- A comma preceded or followed by a parenthesis, curly or square bracket. These will not be flagged to prevent conflicts with sniffs handling spacing around braces.
- A comma preceded or followed by another comma, like for skipping items in a list assignment. These will not be flagged.
- A comma preceded by a non-indented heredoc/nowdoc closer.
In that case, unless the
php_versionconfig directive is set to a version higher than PHP 7.3.0, a new line before will be enforced to prevent parse errors on PHP < 7.3.
The sniff has a separate error code for when a comma is found with more than one space after it, followed by a trailing comment. That way trailing comment alignment can be allowed by excluding that error code.
The sniff uses modular error code suffixes for select situations, like *InFunctionDeclaration,
*InFunctionCall, to allow for preventing duplicate messages if another sniff is already
handling the comma spacing.
Tags
Table of Contents
Interfaces
Constants
- METRIC_NAME_AFTER = 'Spaces found after comma'
- Name of the "Spacing after" metric.
- METRIC_NAME_BEFORE = 'Spaces found before comma'
- Name of the "Spacing before" metric.
Properties
- $phpVersion : int
- PHP version as configured or 0 if unknown.
Methods
- process() : void
- 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.
- processSpacingAfter() : void
- Check the spacing after the comma.
- processSpacingBefore() : void
- Check the spacing before the comma.
- codeSuffixToMetric() : string
- Transform a suffix for an error code into a suffix for a metric.
- escapePlaceholders() : string
- Escape arbitrary token content for *printf() placeholders.
- getSuffix() : string
- Retrieve a text string for use as a suffix to an error code.
Constants
METRIC_NAME_AFTER
Name of the "Spacing after" metric.
public
string
METRIC_NAME_AFTER
= 'Spaces found after comma'
Tags
METRIC_NAME_BEFORE
Name of the "Spacing before" metric.
public
string
METRIC_NAME_BEFORE
= 'Spaces found before comma'
Tags
Properties
$phpVersion
PHP version as configured or 0 if unknown.
private
int
$phpVersion
Tags
Methods
process()
Processes this test, when one of its tokens is encountered.
public
process(File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.
Tags
register()
Returns an array of tokens this test wants to listen for.
public
register() : array<string|int, int|string>
Tags
Return values
array<string|int, int|string>processSpacingAfter()
Check the spacing after the comma.
protected
processSpacingAfter(File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.
Tags
processSpacingBefore()
Check the spacing before the comma.
protected
processSpacingBefore(File $phpcsFile, int $stackPtr) : void
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.
Tags
codeSuffixToMetric()
Transform a suffix for an error code into a suffix for a metric.
private
codeSuffixToMetric(string $suffix) : string
Parameters
- $suffix : string
-
Error code suffix.
Tags
Return values
stringescapePlaceholders()
Escape arbitrary token content for *printf() placeholders.
private
escapePlaceholders(string $text) : string
Parameters
- $text : string
-
Arbitrary text string.
Tags
Return values
stringgetSuffix()
Retrieve a text string for use as a suffix to an error code.
private
getSuffix(File $phpcsFile, int $stackPtr) : string
This allows for modular error codes, which in turn allow for selectively excluding error codes.
Parameters
- $phpcsFile : File
-
The file being scanned.
- $stackPtr : int
-
The position of the current token in the stack passed in $tokens.