Skip to content

Commit 7c87f4f

Browse files
committed
Arrays/ArrayDeclarationSpacing::process_single_line_array(): lower nesting level
As the checks for whitespace around the array opener/closer have now been removed, the code in the `process_single_line_array()` method no longer needs to be nested in condition upon condition. This commit applies the typical "bow out early" pattern to the code in the method. :point_right: This change will be easier to review while ignoring whitespace changes.
1 parent e38b250 commit 7c87f4f

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

WordPress/Sniffs/Arrays/ArrayDeclarationSpacingSniff.php

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,44 +117,46 @@ public function process_token( $stackPtr ) {
117117
*/
118118
protected function process_single_line_array( $stackPtr, $opener, $closer ) {
119119
$array_items = PassedParameters::getParameters( $this->phpcsFile, $stackPtr );
120-
121120
if ( ( false === $this->allow_single_item_single_line_associative_arrays
122-
&& ! empty( $array_items ) )
121+
&& empty( $array_items ) )
123122
|| ( true === $this->allow_single_item_single_line_associative_arrays
124-
&& \count( $array_items ) > 1 )
123+
&& \count( $array_items ) === 1 )
125124
) {
126-
/*
127-
* Make sure the double arrow is for *this* array, not for a nested one.
128-
*/
129-
$array_has_keys = false; // Reset before doing more detailed check.
130-
foreach ( $array_items as $item ) {
131-
if ( Arrays::getDoubleArrowPtr( $this->phpcsFile, $item['start'], $item['end'] ) !== false ) {
132-
$array_has_keys = true;
133-
break;
134-
}
135-
}
136-
137-
if ( true === $array_has_keys ) {
138-
$error = 'When an array uses associative keys, each value should start on %s.';
139-
if ( true === $this->allow_single_item_single_line_associative_arrays ) {
140-
$error = 'When a multi-item array uses associative keys, each value should start on %s.';
141-
}
125+
return;
126+
}
142127

143-
/*
144-
* Just add a new line before the array closer.
145-
* The multi-line array fixer will then fix the individual array items in the next fixer loop.
146-
*/
147-
SpacesFixer::checkAndFix(
148-
$this->phpcsFile,
149-
$closer,
150-
$this->phpcsFile->findPrevious( \T_WHITESPACE, ( $closer - 1 ), null, true ),
151-
'newline',
152-
$error,
153-
'AssociativeArrayFound',
154-
'error'
155-
);
128+
/*
129+
* Make sure the double arrow is for *this* array, not for a nested one.
130+
*/
131+
$array_has_keys = false;
132+
foreach ( $array_items as $item ) {
133+
if ( Arrays::getDoubleArrowPtr( $this->phpcsFile, $item['start'], $item['end'] ) !== false ) {
134+
$array_has_keys = true;
135+
break;
156136
}
157137
}
138+
139+
if ( false === $array_has_keys ) {
140+
return;
141+
}
142+
$error = 'When an array uses associative keys, each value should start on %s.';
143+
if ( true === $this->allow_single_item_single_line_associative_arrays ) {
144+
$error = 'When a multi-item array uses associative keys, each value should start on %s.';
145+
}
146+
147+
/*
148+
* Just add a new line before the array closer.
149+
* The multi-line array fixer will then fix the individual array items in the next fixer loop.
150+
*/
151+
SpacesFixer::checkAndFix(
152+
$this->phpcsFile,
153+
$closer,
154+
$this->phpcsFile->findPrevious( \T_WHITESPACE, ( $closer - 1 ), null, true ),
155+
'newline',
156+
$error,
157+
'AssociativeArrayFound',
158+
'error'
159+
);
158160
}
159161

160162
/**

0 commit comments

Comments
 (0)