Skip to content

Commit ce7b33b

Browse files
phpcs array keys take classes into account
1 parent 76615b4 commit ce7b33b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

php-cs-fixer/Fixers/SpacesInsideArrayBracketsFixer.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,23 @@ private function fixArrayAccessSpacing( Tokens $tokens, int $index ): void
133133
return;
134134
}
135135

136-
// Only add spaces if the index is a variable (starts with $).
136+
// Only add spaces if the index is a variable (starts with $) or a class constant (e.g., self::FIELD_NAME).
137137

138-
// Skip for string literals, constants, etc.
138+
// Check for variables or class constants.
139139
$isVariable = $tokens[ $nextIndex ]->isGivenKind( T_VARIABLE );
140140

141-
if ( ! $isVariable ) {
141+
// Check if it's a class constant (self::, static::, ClassName::).
142+
$isClassConstant = false;
143+
144+
if ( $tokens[ $nextIndex ]->isGivenKind( [ T_STRING, T_STATIC ] ) ) {
145+
$afterString = $tokens->getNextMeaningfulToken( $nextIndex );
146+
147+
if ( null !== $afterString && $tokens[ $afterString ]->isGivenKind( T_DOUBLE_COLON ) ) {
148+
$isClassConstant = true;
149+
}
150+
}
151+
152+
if ( ! $isVariable && ! $isClassConstant ) {
142153
// Remove any existing spaces for non-variables.
143154
if ( $tokens[ $index + 1 ]->isWhitespace() ) {
144155
$tokens->clearAt( $index + 1 );

0 commit comments

Comments
 (0)