PHPCSUtils

SpacesFixer

Utility to check and, if necessary, fix the whitespace between two tokens.

Tags
since
1.0.0

Table of Contents

checkAndFix()  : void
Check the whitespace between two tokens, throw an error if it doesn't match the expected whitespace and if relevant, fix it.

Methods

checkAndFix()

Check the whitespace between two tokens, throw an error if it doesn't match the expected whitespace and if relevant, fix it.

public static checkAndFix(File $phpcsFile, int $stackPtr, int $secondPtr, string|int $expectedSpaces, string $errorTemplate[, string $errorCode = 'Found' ][, string $errorType = 'error' ], int $errorSeverity[, string $metricName = '' ]) : void

Note:

  • This method will not auto-fix if there is anything but whitespace between the two tokens. In that case, it will throw a non-fixable error/warning.
  • If 'newline' is expected and no new line is encountered, a new line will be added, but no assumptions will be made about the intended indentation of the code. This should be handled by a (separate) indentation sniff.
  • If 'newline' is expected and multiple new lines are encountered, this will be accepted as valid. No assumptions are made about whether additional blank lines are allowed or not. If exactly one line is desired, combine this fixer with the \PHPCSUtils\Fixers\BlankLineFixer (upcoming).
  • The fixer will not leave behind any trailing spaces on the original line when fixing to 'newline', but it will not correct existing trailing spaces when there already is a new line in place.
  • This method can optionally record a metric for this check which will be displayed when the end-user requests the "info" report.
Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The position of the token which should be used when reporting an issue.

$secondPtr : int

The stack pointer to the second token. This token can be before or after the $stackPtr, but should only be seperated from the $stackPtr by whitespace and/or comments/annotations.

$expectedSpaces : string|int

Number of spaces to enforce. Valid values:

  • (int) Number of spaces. Must be 0 or more.
  • (string) 'newline'.
$errorTemplate : string

Error message template. Note: The placeholder replacement phrase will be in human readable English and include "spaces"/ "new line", so no need to include that in the template. This string should contain two placeholders:

  • %1$s = expected spaces phrase.
  • %2$s = found spaces phrase.
$errorCode : string = 'Found'

A violation code unique to the sniff message. Defaults to "Found". It is strongly recommended to change this if this fixer is used for different errors in the same sniff.

$errorType : string = 'error'

Optional. Whether to report the issue as a "warning" or an "error". Defaults to "error".

$errorSeverity : int

Optional. The severity level for this message. A value of 0 will be converted into the default severity level.

$metricName : string = ''

Optional. The name of the metric to record. This can be a short description phrase. Leave empty to not record metrics.

Tags
since
1.0.0
throws
RuntimeException

If the tokens passed do not exist or are whitespace tokens.

throws
RuntimeException

If $expectedSpaces is not a valid value.

throws
RuntimeException

If the tokens passed are separated by more than just empty (whitespace + comments/annotations) tokens.

Search results