AbstractArrayDeclarationSniff
in package
implements
Sniff
Abstract sniff to easily examine all parts of an array declaration.
Tags
Table of Contents
Interfaces
- Sniff
Properties
- $arrayCloser : int
- The stack pointer to the array closer.
- $arrayItems : array<int, array<string, int|string>>
- 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<int, array<string, mixed>>
- The token stack for the current file being examined.
Methods
- __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<string|int, int|string>
- Returns an array of tokens this test wants to listen for.
Properties
$arrayCloser
The stack pointer to the array closer.
protected
int
$arrayCloser
Tags
$arrayItems
A multi-dimentional array with information on each array item.
protected
array<int, array<string, int|string>>
$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
$arrayOpener
The stack pointer to the array opener.
protected
int
$arrayOpener
Tags
$itemCount
How many items are in the array.
protected
int
$itemCount
= 0
Tags
$singleLine
Whether or not the array is single line.
protected
bool
$singleLine
Tags
$stackPtr
The stack pointer to the array keyword or the short array open token.
protected
int
$stackPtr
Tags
$tokens
The token stack for the current file being examined.
protected
array<int, array<string, mixed>>
$tokens
Tags
Methods
__construct()
Set up this class.
public
final __construct() : void
Tags
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
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 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
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 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
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
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
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
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 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
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
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
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<string|int, int|string>