@@ -294,103 +294,6 @@ public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arra
294294 }//end processSingleLineArray()
295295
296296
297- /**
298- * Get the stackPtr where the array declaration starts.
299- *
300- * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
301- * @param int $stackPtr The position of the current token
302- * in the stack passed in $tokens.
303- *
304- * @todo This is 'brute force' at the moment and ought to be refactored.
305- *
306- * @return int|false
307- */
308- public function arrayDeclaredAt ($ phpcsFile , $ stackPtr )
309- {
310- $ tokens = $ phpcsFile ->getTokens ();
311-
312- // Possible indent starting token.
313- $ indentStart = $ stackPtr ;
314-
315- // Tokens that could come before an array declaration variable.
316- $ preTokens = array (
317- T_VAR => true ,
318- T_PUBLIC => true ,
319- T_PRIVATE => true ,
320- T_PROTECTED => true ,
321- T_ARRAY_CAST => true ,
322- T_UNSET_CAST => true ,
323- T_OBJECT_CAST => true ,
324- T_STATIC => true ,
325- );
326-
327- $ before1 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
328-
329- // Is it a variable.
330- // - '$arr = [];'.
331- if ($ tokens [$ before1 ]['code ' ] === T_VARIABLE ) {
332- // Store this in case this is the start.
333- $ indentStart = $ before1 ;
334-
335- // Does it have visibility scope or a type hint?
336- // - 'private $arr = [];'
337- // - '(array) $arr = [];'.
338- // - 'private static $arr = [];'.
339- $ before2 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ before1 - 1 ), null , true );
340- if ($ before2 !== false && isset ($ preTokens [$ tokens [$ before2 ]['code ' ]]) === true ) {
341- // It is preceded with scope or type.
342- $ indentStart = $ before2 ;
343-
344- // Could still need to go back one level if it's static.
345- // - 'private static $arr = [];'.
346- $ before3 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ before2 - 1 ), null , true );
347- if ($ before3 !== false && isset ($ preTokens [$ tokens [$ before3 ]['code ' ]]) === true ) {
348- // It is preceded with scope or type.
349- $ indentStart = $ before3 ;
350- }
351- }
352- }//end if
353-
354- // - $obj->arr[] = [];
355- if ($ tokens [$ before1 ]['code ' ] === T_CLOSE_SQUARE_BRACKET ) {
356- $ before1 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ tokens [$ before1 ]['bracket_opener ' ] - 1 ), null , true );
357- }
358-
359- // Is it a string?, if so expect object or constant.
360- // - '$obj->arr = [];'.
361- // - 'MY_CONST = [];'
362- // - 'const MY_CONST = [];.
363- if ($ tokens [$ before1 ]['code ' ] === T_STRING ) {
364- $ before2 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ before1 - 1 ), null , true );
365- // Is it a constant?
366- if ($ tokens [$ before2 ]['code ' ] === T_CONST ) {
367- $ indentStart = $ before2 ;
368- return $ indentStart ;
369- }
370-
371- // Is it an object?
372- if ($ tokens [$ before2 ]['code ' ] === T_OBJECT_OPERATOR ) {
373- $ indentStart = $ before2 ;
374-
375- $ before3 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ before2 - 1 ), null , true );
376- if ($ tokens [$ before3 ]['code ' ] === T_VARIABLE ) {
377- $ indentStart = $ before3 ;
378-
379- // Does it have visibility scope or a is it cast?
380- $ before4 = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ before3 - 1 ), null , true );
381- if ($ before4 !== false && isset ($ preTokens [$ tokens [$ before4 ]['code ' ]]) === true ) {
382- // It is preceded with scope or type.
383- $ indentStart = $ before4 ;
384- }
385- }
386- }
387- }//end if
388-
389- return $ indentStart ;
390-
391- }//end arrayDeclaredAt()
392-
393-
394297 /**
395298 * Processes a multi-line array definition.
396299 *
@@ -408,25 +311,51 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
408311 $ keywordStart = $ tokens [$ stackPtr ]['column ' ];
409312
410313 $ prevNonWhitespaceToken = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
411- if ($ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_EQUAL
412- || $ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_OPEN_PARENTHESIS
413- || $ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_RETURN
414- ) {
314+
315+ // Find where this array should be indented from.
316+ switch ($ tokens [$ prevNonWhitespaceToken ]['code ' ]) {
317+ case T_EQUAL :
318+ case T_OPEN_PARENTHESIS :
415319 // It's "=", "(" or "return".
416- $ indentStart = $ this ->arrayDeclaredAt ($ phpcsFile , $ prevNonWhitespaceToken );
417- } else if ($ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_DOUBLE_ARROW ) {
320+ $ starts = array (
321+ T_VARIABLE ,
322+ T_VAR ,
323+ T_PUBLIC ,
324+ T_PRIVATE ,
325+ T_PROTECTED ,
326+ T_ARRAY_CAST ,
327+ T_UNSET_CAST ,
328+ T_OBJECT_CAST ,
329+ T_STATIC ,
330+ T_CONST ,
331+ );
332+
333+ $ firstOnLine = $ phpcsFile ->findFirstOnLine ($ starts , $ prevNonWhitespaceToken );
334+ $ indentStart = $ firstOnLine ;
335+ break ;
336+ case T_DOUBLE_ARROW :
418337 // It's an array in an array "=> []".
419338 $ indentStart = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ prevNonWhitespaceToken - 1 ), null , true );
420- } else if ($ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_OPEN_SHORT_ARRAY
421- || $ tokens [$ prevNonWhitespaceToken ]['code ' ] === T_COMMA
422- ) {
339+ break ;
340+ case T_RETURN :
341+ $ indentStart = $ prevNonWhitespaceToken ;
342+ break ;
343+ case T_COMMENT :
344+ case T_OPEN_SHORT_ARRAY :
423345 // It's an array in an array "[[]]" or the end of an array line "[],".
424346 $ indentStart = $ stackPtr ;
425- } else {
426- // Nothing expected preceded this so return here and
347+ break ;
348+ case T_COMMA :
349+ // The end of an array line "[],".
350+ // Argument in a function "$item->save($data, [...], ...)".
351+ $ firstOnLine = $ phpcsFile ->findFirstOnLine (array (T_VARIABLE , T_CLOSE_SHORT_ARRAY ), $ prevNonWhitespaceToken );
352+ $ indentStart = $ firstOnLine ;
353+ break ;
354+ default :
355+ // Nothing expected preceded this so leave ptr where it is and
427356 // it should get picked in a future pass.
428- return ;
429- }
357+ $ indentStart = $ stackPtr ;
358+ }//end switch
430359
431360 // Check the closing bracket is on a new line.
432361 $ lastContent = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ arrayEnd - 1 ), $ arrayStart , true );
0 commit comments