Documentation

MixedArrayKeyTypesSniff extends AbstractArrayDeclarationSniff
in package

FinalYes

Forbid arrays which contain both array items with numeric keys as well as array items with string keys.

Tags
since
1.0.0

Table of Contents

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.
$seenNumericKey  : bool
Whether a numeric key was encountered.
$seenStringKey  : bool
Whether a string key was encountered.

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 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

$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
since
1.0.0

$tokens

The token stack for the current file being examined.

protected array<int, array<string, mixed>> $tokens
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 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 the array declaration.

public processArray(File $phpcsFile) : void
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
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.

Tags
since
1.0.0
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
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.

Tags
since
1.0.0
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<string|int, int|string>
Tags
since
1.0.0
codeCoverageIgnore
Return values
array<string|int, int|string>

        
On this page

Search results