PHPCSUtils

AbstractArrayDeclarationSniff implements Sniff

Abstract sniff to easily examine all parts of an array declaration.

Tags
since
1.0.0

Interfaces, Classes and Traits

Sniff

Table of Contents

$arrayCloser  : int
The stack pointer to the array closer.
$arrayItems  : array
A multi-dimentional array with information on each array item.
$arrayOpener  : int
The stack pointer to the array opener.
$itemCount  : int
How many items are in the array.
$singleLine  : bool
Whether or not the array is single line.
$stackPtr  : int
The stack pointer to the array keyword or the short array open token.
$tokens  : array
The token stack for the current file being examined.
__construct()  : void
Set up this class.
getActualArrayKey()  : string|int|void
Determine what the actual array key would be.
process()  : void
Processes this test when one of its tokens is encountered.
processArray()  : void
Process every part of the array declaration.
processArrow()  : true|void
Process the double arrow.
processComma()  : true|void
Process the comma after an array item.
processKey()  : true|void
Process the tokens in an array key.
processNoKey()  : true|void
Process an array item without an array key.
processOpenClose()  : true|void
Process the array opener and closer.
processValue()  : true|void
Process the tokens in an array value.
register()  : array
Returns an array of tokens this test wants to listen for.

Properties

$arrayItems

A multi-dimentional array with information on each array item.

protected array $arrayItems

The array index is 1-based and contains the following information on each array item:

1 => array(
  'start' => int,    // The stack pointer to the first token in the array item.
  'end'   => int,    // The stack pointer to the last token in the array item.
  'raw'   => string, // A string with the contents of all tokens between `start` and `end`.
  'clean' => string, // Same as `raw`, but all comment tokens have been stripped out.
)
Tags
since
1.0.0

Methods

getActualArrayKey()

Determine what the actual array key would be.

public getActualArrayKey(File $phpcsFile, int $startPtr, int $endPtr) : string|int|void

Helper function for processsing array keys in the processKey() function. Using this method is up to the sniff implementation in the child class.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$startPtr : int

The stack pointer to the first token in the "key" part of an array item.

$endPtr : int

The stack pointer to the last token in the "key" part of an array item.

Tags
since
1.0.0
Return values
string|int|void

The string or integer array key or void if the array key could not reliably be determined.

process()

Processes this test when one of its tokens is encountered.

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

This method fills the properties with relevant information for examining the array and then passes off to the \PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff::processArray() method.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$stackPtr : int

The position in the PHP_CodeSniffer file's token stack where the token was found.

Tags
since
1.0.0

processArray()

Process every part of the array declaration.

public processArray(File $phpcsFile) : void

Controller which calls the individual process...() methods for each part of the array.

The method starts by calling the \PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff::processOpenClose() method and subsequently calls the following methods for each array item:

Unkeyed arrays Keyed arrays
processNoKey() processKey()
- processArrow()
processValue() processValue()
processComma() processComma()

This is the default logic for the sniff, but can be overloaded in a concrete child class if needed.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

Tags
since
1.0.0

processArrow()

Process the double arrow.

public processArrow(File $phpcsFile, int $arrowPtr, int $itemNr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$arrowPtr : int

The stack pointer to the double arrow for the array item.

$itemNr : int

Which item in the array is being handled. 1-based, i.e. the first item is item 1, the second 2 etc.

Tags
since
1.0.0
codeCoverageIgnore
Return values
true|void

Returning TRUE will short-circuit the array item loop and stop processing. In effect, this means that the sniff will not examine the array value or comma for this array item and will not process any array items after this one.

processComma()

Process the comma after an array item.

public processComma(File $phpcsFile, int $commaPtr, int $itemNr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$commaPtr : int

The stack pointer to the comma.

$itemNr : int

Which item in the array is being handled. 1-based, i.e. the first item is item 1, the second 2 etc.

Tags
since
1.0.0
codeCoverageIgnore
Return values
true|void

Returning TRUE will short-circuit the array item loop and stop processing. In effect, this means that the sniff will not process any array items after this one.

processKey()

Process the tokens in an array key.

public processKey(File $phpcsFile, int $startPtr, int $endPtr, int $itemNr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Note: The $startPtr and $endPtr do not discount whitespace or comments, but are all inclusive to allow for examining all tokens in an array key.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$startPtr : int

The stack pointer to the first token in the "key" part of an array item.

$endPtr : int

The stack pointer to the last token in the "key" part of an array item.

$itemNr : int

Which item in the array is being handled. 1-based, i.e. the first item is item 1, the second 2 etc.

Tags
since
1.0.0
codeCoverageIgnore
see
AbstractArrayDeclarationSniff::getActualArrayKey()

Optional helper function.

Return values
true|void

Returning TRUE will short-circuit the array item loop and stop processing. In effect, this means that the sniff will not examine the double arrow, the array value or comma for this array item and will not process any array items after this one.

processNoKey()

Process an array item without an array key.

public processNoKey(File $phpcsFile, int $startPtr, int $itemNr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Note: This method is not intended for processing the array value. Use the \PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff::processValue() method to implement processing of the array value.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$startPtr : int

The stack pointer to the first token in the array item, which in this case will be the first token of the array value part of the array item.

$itemNr : int

Which item in the array is being handled. 1-based, i.e. the first item is item 1, the second 2 etc.

Tags
since
1.0.0
codeCoverageIgnore
see
AbstractArrayDeclarationSniff::processValue()

Method to process the array value.

Return values
true|void

Returning TRUE will short-circuit the array item loop and stop processing. In effect, this means that the sniff will not examine the array value or comma for this array item and will not process any array items after this one.

processOpenClose()

Process the array opener and closer.

public processOpenClose(File $phpcsFile, int $openPtr, int $closePtr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$openPtr : int

The position of the array opener token in the token stack.

$closePtr : int

The position of the array closer token in the token stack.

Tags
since
1.0.0
codeCoverageIgnore
Return values
true|void

Returning TRUE will short-circuit the sniff and stop processing. In effect, this means that the sniff will not examine the individual array items if TRUE is returned.

processValue()

Process the tokens in an array value.

public processValue(File $phpcsFile, int $startPtr, int $endPtr, int $itemNr) : true|void

Optional method to be implemented in concrete child classes. By default, this method does nothing.

Note: The $startPtr and $endPtr do not discount whitespace or comments, but are all inclusive to allow for examining all tokens in an array value.

Parameters
$phpcsFile : File

The PHP_CodeSniffer file where the token was found.

$startPtr : int

The stack pointer to the first token in the "value" part of an array item.

$endPtr : int

The stack pointer to the last token in the "value" part of an array item.

$itemNr : int

Which item in the array is being handled. 1-based, i.e. the first item is item 1, the second 2 etc.

Tags
since
1.0.0
codeCoverageIgnore
Return values
true|void

Returning TRUE will short-circuit the array item loop and stop processing. In effect, this means that the sniff will not examine the comma for this array item and will not process any array items after this one.

register()

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

public register() : array
Tags
since
1.0.0
codeCoverageIgnore
Return values
array

Search results