Documentation

DuplicateArrayKeySniff extends AbstractArrayDeclarationSniff
in package

FinalYes

Detect duplicate array keys in array declarations.

This sniff will detect duplicate keys with high precision, though any array key set via a variable/constant/function call is excluded from the examination.

The sniff will handle the change in how numeric array keys are set since PHP 8.0 and will flag keys which would be duplicates cross-version.

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.
$currentMaxIntKeyGt8  : int
Keep track of the maximum seen integer key to know what the next value will be for array items without a key on PHP >= 8.0.
$currentMaxIntKeyLt8  : int
Keep track of the maximum seen integer key to know what the next value will be for array items without a key on PHP < 8.0.
$keysSeenGt8  : array<int, array<string, int>>
Keep track of which array keys have been seen already on PHP >= 8.0.
$keysSeenLt8  : array<int, array<string, int>>
Keep track of which array keys have been seen already on PHP < 8.0.
$phpVersion  : int
PHP version as configured or -1 if unknown.

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()  : void
Process the tokens in an array key.
processNoKey()  : 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

$currentMaxIntKeyGt8

Keep track of the maximum seen integer key to know what the next value will be for array items without a key on PHP >= 8.0.

private int $currentMaxIntKeyGt8
Tags
since
1.0.0

$currentMaxIntKeyLt8

Keep track of the maximum seen integer key to know what the next value will be for array items without a key on PHP < 8.0.

private int $currentMaxIntKeyLt8
Tags
since
1.0.0

$keysSeenGt8

Keep track of which array keys have been seen already on PHP >= 8.0.

private array<int, array<string, int>> $keysSeenGt8 = []
Tags
since
1.0.0

$keysSeenLt8

Keep track of which array keys have been seen already on PHP < 8.0.

private array<int, array<string, int>> $keysSeenLt8 = []
Tags
since
1.0.0

$phpVersion

PHP version as configured or -1 if unknown.

private int $phpVersion
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 every part of the array declaration.

public processArray(File $phpcsFile) : void

This contains 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) : 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

processNoKey()

Process an array item without an array key.

public processNoKey(File $phpcsFile, int $startPtr, int $itemNr) : 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

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