PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file ArrayDeclarationSniff.php

Documentation is available at ArrayDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Ensures that arrays conform to the array coding standard.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ArrayDeclarationSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(
  28.                 T_ARRAY,
  29.                 T_OPEN_SHORT_ARRAY,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this sniff, when one of its tokens is encountered.
  37.      *
  38.      * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  39.      * @param int                  $stackPtr  The position of the current token in
  40.      *                                         the stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         if ($tokens[$stackPtr]['code'=== T_ARRAY{
  49.             $phpcsFile->recordMetric($stackPtr'Short array syntax used''no');
  50.  
  51.             // Array keyword should be lower case.
  52.             if ($tokens[$stackPtr]['content'!== strtolower($tokens[$stackPtr]['content'])) {
  53.                 if ($tokens[$stackPtr]['content'=== strtoupper($tokens[$stackPtr]['content'])) {
  54.                     $phpcsFile->recordMetric($stackPtr'Array keyword case''upper');
  55.                 else {
  56.                     $phpcsFile->recordMetric($stackPtr'Array keyword case''mixed');
  57.                 }
  58.  
  59.                 $error 'Array keyword should be lower case; expected "array" but found "%s"';
  60.                 $data  = array($tokens[$stackPtr]['content']);
  61.                 $fix   $phpcsFile->addFixableError($error$stackPtr'NotLowerCase'$data);
  62.                 if ($fix === true{
  63.                     $phpcsFile->fixer->replaceToken($stackPtr'array');
  64.                 }
  65.             else {
  66.                 $phpcsFile->recordMetric($stackPtr'Array keyword case''lower');
  67.             }
  68.  
  69.             $arrayStart $tokens[$stackPtr]['parenthesis_opener'];
  70.             if (isset($tokens[$arrayStart]['parenthesis_closer']=== false{
  71.                 return;
  72.             }
  73.  
  74.             $arrayEnd $tokens[$arrayStart]['parenthesis_closer'];
  75.  
  76.             if ($arrayStart !== ($stackPtr + 1)) {
  77.                 $error 'There must be no space between the "array" keyword and the opening parenthesis';
  78.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SpaceAfterKeyword');
  79.  
  80.                 if ($fix === true{
  81.                     $phpcsFile->fixer->beginChangeset();
  82.                     for ($i ($stackPtr + 1)$i $arrayStart$i++{
  83.                         $phpcsFile->fixer->replaceToken($i'');
  84.                     }
  85.  
  86.                     $phpcsFile->fixer->endChangeset();
  87.                 }
  88.             }
  89.         else {
  90.             $phpcsFile->recordMetric($stackPtr'Short array syntax used''yes');
  91.             $arrayStart $stackPtr;
  92.             $arrayEnd   $tokens[$stackPtr]['bracket_closer'];
  93.         }//end if
  94.  
  95.         // Check for empty arrays.
  96.         $content $phpcsFile->findNext(T_WHITESPACE($arrayStart + 1)($arrayEnd + 1)true);
  97.         if ($content === $arrayEnd{
  98.             // Empty array, but if the brackets aren't together, there's a problem.
  99.             if (($arrayEnd $arrayStart!== 1{
  100.                 $error 'Empty array declaration must have no space between the parentheses';
  101.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SpaceInEmptyArray');
  102.  
  103.                 if ($fix === true{
  104.                     $phpcsFile->fixer->beginChangeset();
  105.                     for ($i ($arrayStart + 1)$i $arrayEnd$i++{
  106.                         $phpcsFile->fixer->replaceToken($i'');
  107.                     }
  108.  
  109.                     $phpcsFile->fixer->endChangeset();
  110.                 }
  111.             }
  112.  
  113.             // We can return here because there is nothing else to check. All code
  114.             // below can assume that the array is not empty.
  115.             return;
  116.         }
  117.  
  118.         if ($tokens[$arrayStart]['line'=== $tokens[$arrayEnd]['line']{
  119.             $this->processSingleLineArray($phpcsFile$stackPtr$arrayStart$arrayEnd);
  120.         else {
  121.             $this->processMultiLineArray($phpcsFile$stackPtr$arrayStart$arrayEnd);
  122.         }
  123.  
  124.     }//end process()
  125.  
  126.  
  127.     /**
  128.      * Processes a single-line array definition.
  129.      *
  130.      * @param PHP_CodeSniffer_File $phpcsFile  The current file being checked.
  131.      * @param int                  $stackPtr   The position of the current token
  132.      *                                          in the stack passed in $tokens.
  133.      * @param int                  $arrayStart The token that starts the array definition.
  134.      * @param int                  $arrayEnd   The token that ends the array definition.
  135.      *
  136.      * @return void 
  137.      */
  138.     public function processSingleLineArray($phpcsFile$stackPtr$arrayStart$arrayEnd)
  139.     {
  140.         $tokens $phpcsFile->getTokens();
  141.  
  142.         // Check if there are multiple values. If so, then it has to be multiple lines
  143.         // unless it is contained inside a function call or condition.
  144.         $valueCount = 0;
  145.         $commas     = array();
  146.         for ($i ($arrayStart + 1)$i $arrayEnd$i++{
  147.             // Skip bracketed statements, like function calls.
  148.             if ($tokens[$i]['code'=== T_OPEN_PARENTHESIS{
  149.                 $i $tokens[$i]['parenthesis_closer'];
  150.                 continue;
  151.             }
  152.  
  153.             if ($tokens[$i]['code'=== T_COMMA{
  154.                 // Before counting this comma, make sure we are not
  155.                 // at the end of the array.
  156.                 $next $phpcsFile->findNext(T_WHITESPACE($i + 1)$arrayEndtrue);
  157.                 if ($next !== false{
  158.                     $valueCount++;
  159.                     $commas[$i;
  160.                 else {
  161.                     // There is a comma at the end of a single line array.
  162.                     $error 'Comma not allowed after last value in single-line array declaration';
  163.                     $fix   $phpcsFile->addFixableError($error$i'CommaAfterLast');
  164.                     if ($fix === true{
  165.                         $phpcsFile->fixer->replaceToken($i'');
  166.                     }
  167.                 }
  168.             }
  169.         }//end for
  170.  
  171.         // Now check each of the double arrows (if any).
  172.         $nextArrow $arrayStart;
  173.         while (($nextArrow $phpcsFile->findNext(T_DOUBLE_ARROW($nextArrow + 1)$arrayEnd)) !== false{
  174.             if ($tokens[($nextArrow - 1)]['code'!== T_WHITESPACE{
  175.                 $content $tokens[($nextArrow - 1)]['content'];
  176.                 $error   'Expected 1 space between "%s" and double arrow; 0 found';
  177.                 $data    = array($content);
  178.                 $fix     $phpcsFile->addFixableError($error$nextArrow'NoSpaceBeforeDoubleArrow'$data);
  179.                 if ($fix === true{
  180.                     $phpcsFile->fixer->addContentBefore($nextArrow' ');
  181.                 }
  182.             else {
  183.                 $spaceLength $tokens[($nextArrow - 1)]['length'];
  184.                 if ($spaceLength !== 1{
  185.                     $content $tokens[($nextArrow - 2)]['content'];
  186.                     $error   'Expected 1 space between "%s" and double arrow; %s found';
  187.                     $data    = array(
  188.                                 $content,
  189.                                 $spaceLength,
  190.                                );
  191.  
  192.                     $fix $phpcsFile->addFixableError($error$nextArrow'SpaceBeforeDoubleArrow'$data);
  193.                     if ($fix === true{
  194.                         $phpcsFile->fixer->replaceToken(($nextArrow - 1)' ');
  195.                     }
  196.                 }
  197.             }//end if
  198.  
  199.             if ($tokens[($nextArrow + 1)]['code'!== T_WHITESPACE{
  200.                 $content $tokens[($nextArrow + 1)]['content'];
  201.                 $error   'Expected 1 space between double arrow and "%s"; 0 found';
  202.                 $data    = array($content);
  203.                 $fix     $phpcsFile->addFixableError($error$nextArrow'NoSpaceAfterDoubleArrow'$data);
  204.                 if ($fix === true{
  205.                     $phpcsFile->fixer->addContent($nextArrow' ');
  206.                 }
  207.             else {
  208.                 $spaceLength $tokens[($nextArrow + 1)]['length'];
  209.                 if ($spaceLength !== 1{
  210.                     $content $tokens[($nextArrow + 2)]['content'];
  211.                     $error   'Expected 1 space between double arrow and "%s"; %s found';
  212.                     $data    = array(
  213.                                 $content,
  214.                                 $spaceLength,
  215.                                );
  216.  
  217.                     $fix $phpcsFile->addFixableError($error$nextArrow'SpaceAfterDoubleArrow'$data);
  218.                     if ($fix === true{
  219.                         $phpcsFile->fixer->replaceToken(($nextArrow + 1)' ');
  220.                     }
  221.                 }
  222.             }//end if
  223.         }//end while
  224.  
  225.         if ($valueCount > 0{
  226.             $conditionCheck $phpcsFile->findPrevious(array(T_OPEN_PARENTHESIST_SEMICOLON)($stackPtr - 1)nullfalse);
  227.  
  228.             if ($conditionCheck === false
  229.                 || $tokens[$conditionCheck]['line'!== $tokens[$stackPtr]['line']
  230.             {
  231.                 $error 'Array with multiple values cannot be declared on a single line';
  232.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SingleLineNotAllowed');
  233.                 if ($fix === true{
  234.                     $phpcsFile->fixer->beginChangeset();
  235.                     $phpcsFile->fixer->addNewline($arrayStart);
  236.                     $phpcsFile->fixer->addNewlineBefore($arrayEnd);
  237.                     $phpcsFile->fixer->endChangeset();
  238.                 }
  239.  
  240.                 return;
  241.             }
  242.  
  243.             // We have a multiple value array that is inside a condition or
  244.             // function. Check its spacing is correct.
  245.             foreach ($commas as $comma{
  246.                 if ($tokens[($comma + 1)]['code'!== T_WHITESPACE{
  247.                     $content $tokens[($comma + 1)]['content'];
  248.                     $error   'Expected 1 space between comma and "%s"; 0 found';
  249.                     $data    = array($content);
  250.                     $fix     $phpcsFile->addFixableError($error$comma'NoSpaceAfterComma'$data);
  251.                     if ($fix === true{
  252.                         $phpcsFile->fixer->addContent($comma' ');
  253.                     }
  254.                 else {
  255.                     $spaceLength $tokens[($comma + 1)]['length'];
  256.                     if ($spaceLength !== 1{
  257.                         $content $tokens[($comma + 2)]['content'];
  258.                         $error   'Expected 1 space between comma and "%s"; %s found';
  259.                         $data    = array(
  260.                                     $content,
  261.                                     $spaceLength,
  262.                                    );
  263.  
  264.                         $fix $phpcsFile->addFixableError($error$comma'SpaceAfterComma'$data);
  265.                         if ($fix === true{
  266.                             $phpcsFile->fixer->replaceToken(($comma + 1)' ');
  267.                         }
  268.                     }
  269.                 }//end if
  270.  
  271.                 if ($tokens[($comma - 1)]['code'=== T_WHITESPACE{
  272.                     $content     $tokens[($comma - 2)]['content'];
  273.                     $spaceLength $tokens[($comma - 1)]['length'];
  274.                     $error       'Expected 0 spaces between "%s" and comma; %s found';
  275.                     $data        = array(
  276.                                     $content,
  277.                                     $spaceLength,
  278.                                    );
  279.  
  280.                     $fix $phpcsFile->addFixableError($error$comma'SpaceBeforeComma'$data);
  281.                     if ($fix === true{
  282.                         $phpcsFile->fixer->replaceToken(($comma - 1)'');
  283.                     }
  284.                 }
  285.             }//end foreach
  286.         }//end if
  287.  
  288.     }//end processSingleLineArray()
  289.  
  290.  
  291.     /**
  292.      * Processes a multi-line array definition.
  293.      *
  294.      * @param PHP_CodeSniffer_File $phpcsFile  The current file being checked.
  295.      * @param int                  $stackPtr   The position of the current token
  296.      *                                          in the stack passed in $tokens.
  297.      * @param int                  $arrayStart The token that starts the array definition.
  298.      * @param int                  $arrayEnd   The token that ends the array definition.
  299.      *
  300.      * @return void 
  301.      */
  302.     public function processMultiLineArray($phpcsFile$stackPtr$arrayStart$arrayEnd)
  303.     {
  304.         $tokens       $phpcsFile->getTokens();
  305.         $keywordStart $tokens[$stackPtr]['column'];
  306.  
  307.         // Check the closing bracket is on a new line.
  308.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($arrayEnd - 1)$arrayStarttrue);
  309.         if ($tokens[$lastContent]['line'=== $tokens[$arrayEnd]['line']{
  310.             $error 'Closing parenthesis of array declaration must be on a new line';
  311.             $fix   $phpcsFile->addFixableError($error$arrayEnd'CloseBraceNewLine');
  312.             if ($fix === true{
  313.                 $phpcsFile->fixer->addNewlineBefore($arrayEnd);
  314.             }
  315.         else if ($tokens[$arrayEnd]['column'!== $keywordStart{
  316.             // Check the closing bracket is lined up under the "a" in array.
  317.             $expected ($keywordStart - 1);
  318.             $found    ($tokens[$arrayEnd]['column'- 1);
  319.             $error    'Closing parenthesis not aligned correctly; expected %s space(s) but found %s';
  320.             $data     = array(
  321.                          $expected,
  322.                          $found,
  323.                         );
  324.  
  325.             $fix $phpcsFile->addFixableError($error$arrayEnd'CloseBraceNotAligned'$data);
  326.             if ($fix === true{
  327.                 if ($found === 0{
  328.                     $phpcsFile->fixer->addContent(($arrayEnd - 1)str_repeat(' '$expected));
  329.                 else {
  330.                     $phpcsFile->fixer->replaceToken(($arrayEnd - 1)str_repeat(' '$expected));
  331.                 }
  332.             }
  333.         }//end if
  334.  
  335.         $nextToken  $stackPtr;
  336.         $keyUsed    = false;
  337.         $singleUsed = false;
  338.         $indices    = array();
  339.         $maxLength  = 0;
  340.  
  341.         if ($tokens[$stackPtr]['code'=== T_ARRAY{
  342.             $lastToken $tokens[$stackPtr]['parenthesis_opener'];
  343.         else {
  344.             $lastToken $stackPtr;
  345.         }
  346.  
  347.         // Find all the double arrows that reside in this scope.
  348.         for ($nextToken ($stackPtr + 1)$nextToken $arrayEnd$nextToken++{
  349.             // Skip bracketed statements, like function calls.
  350.             if ($tokens[$nextToken]['code'=== T_OPEN_PARENTHESIS
  351.                 && (isset($tokens[$nextToken]['parenthesis_owner']=== false
  352.                 || $tokens[$nextToken]['parenthesis_owner'!== $stackPtr)
  353.             {
  354.                 $nextToken $tokens[$nextToken]['parenthesis_closer'];
  355.                 continue;
  356.             }
  357.  
  358.             if ($tokens[$nextToken]['code'=== T_ARRAY{
  359.                 // Let subsequent calls of this test handle nested arrays.
  360.                 if ($tokens[$lastToken]['code'!== T_DOUBLE_ARROW{
  361.                     $indices[= array('value' => $nextToken);
  362.                     $lastToken $nextToken;
  363.                 }
  364.  
  365.                 $nextToken $tokens[$tokens[$nextToken]['parenthesis_opener']]['parenthesis_closer'];
  366.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  367.                 if ($tokens[$nextToken]['code'!== T_COMMA{
  368.                     $nextToken--;
  369.                 else {
  370.                     $lastToken $nextToken;
  371.                 }
  372.  
  373.                 continue;
  374.             }
  375.  
  376.             if ($tokens[$nextToken]['code'=== T_OPEN_SHORT_ARRAY{
  377.                 // Let subsequent calls of this test handle nested arrays.
  378.                 if ($tokens[$lastToken]['code'!== T_DOUBLE_ARROW{
  379.                     $indices[= array('value' => $nextToken);
  380.                     $lastToken $nextToken;
  381.                 }
  382.  
  383.                 $nextToken $tokens[$nextToken]['bracket_closer'];
  384.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  385.                 if ($tokens[$nextToken]['code'!== T_COMMA{
  386.                     $nextToken--;
  387.                 else {
  388.                     $lastToken $nextToken;
  389.                 }
  390.  
  391.                 continue;
  392.             }
  393.  
  394.             if ($tokens[$nextToken]['code'=== T_CLOSURE{
  395.                 if ($tokens[$lastToken]['code'!== T_DOUBLE_ARROW{
  396.                     $indices[= array('value' => $nextToken);
  397.                     $lastToken $nextToken;
  398.                 }
  399.  
  400.                 $nextToken $tokens[$nextToken]['scope_closer'];
  401.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  402.                 if ($tokens[$nextToken]['code'!== T_COMMA{
  403.                     $nextToken--;
  404.                 else {
  405.                     $lastToken $nextToken;
  406.                 }
  407.  
  408.                 continue;
  409.             }
  410.  
  411.             if ($tokens[$nextToken]['code'!== T_DOUBLE_ARROW
  412.                 && $tokens[$nextToken]['code'!== T_COMMA
  413.             {
  414.                 continue;
  415.             }
  416.  
  417.             $currentEntry = array();
  418.  
  419.             if ($tokens[$nextToken]['code'=== T_COMMA{
  420.                 $stackPtrCount = 0;
  421.                 if (isset($tokens[$stackPtr]['nested_parenthesis']=== true{
  422.                     $stackPtrCount count($tokens[$stackPtr]['nested_parenthesis']);
  423.                 }
  424.  
  425.                 $commaCount = 0;
  426.                 if (isset($tokens[$nextToken]['nested_parenthesis']=== true{
  427.                     $commaCount count($tokens[$nextToken]['nested_parenthesis']);
  428.                     if ($tokens[$stackPtr]['code'=== T_ARRAY{
  429.                         // Remove parenthesis that are used to define the array.
  430.                         $commaCount--;
  431.                     }
  432.                 }
  433.  
  434.                 if ($commaCount $stackPtrCount{
  435.                     // This comma is inside more parenthesis than the ARRAY keyword,
  436.                     // then there it is actually a comma used to separate arguments
  437.                     // in a function call.
  438.                     continue;
  439.                 }
  440.  
  441.                 if ($keyUsed === true && $tokens[$lastToken]['code'=== T_COMMA{
  442.                     $error 'No key specified for array entry; first entry specifies key';
  443.                     $phpcsFile->addError($error$nextToken'NoKeySpecified');
  444.                     return;
  445.                 }
  446.  
  447.                 if ($keyUsed === false{
  448.                     if ($tokens[($nextToken - 1)]['code'=== T_WHITESPACE{
  449.                         $content $tokens[($nextToken - 2)]['content'];
  450.                         if ($tokens[($nextToken - 1)]['content'=== $phpcsFile->eolChar{
  451.                             $spaceLength 'newline';
  452.                         else {
  453.                             $spaceLength $tokens[($nextToken - 1)]['length'];
  454.                         }
  455.  
  456.                         $error 'Expected 0 spaces between "%s" and comma; %s found';
  457.                         $data  = array(
  458.                                   $content,
  459.                                   $spaceLength,
  460.                                  );
  461.  
  462.                         $fix $phpcsFile->addFixableError($error$nextToken'SpaceBeforeComma'$data);
  463.                         if ($fix === true{
  464.                             $phpcsFile->fixer->replaceToken(($nextToken - 1)'');
  465.                         }
  466.                     }
  467.  
  468.                     $valueContent $phpcsFile->findNext(
  469.                         Tokens::$emptyTokens,
  470.                         ($lastToken + 1),
  471.                         $nextToken,
  472.                         true
  473.                     );
  474.  
  475.                     $indices[]  = array('value' => $valueContent);
  476.                     $singleUsed = true;
  477.                 }//end if
  478.  
  479.                 $lastToken $nextToken;
  480.                 continue;
  481.             }//end if
  482.  
  483.             if ($tokens[$nextToken]['code'=== T_DOUBLE_ARROW{
  484.                 if ($singleUsed === true{
  485.                     $error 'Key specified for array entry; first entry has no key';
  486.                     $phpcsFile->addError($error$nextToken'KeySpecified');
  487.                     return;
  488.                 }
  489.  
  490.                 $currentEntry['arrow'$nextToken;
  491.                 $keyUsed = true;
  492.  
  493.                 // Find the start of index that uses this double arrow.
  494.                 $indexEnd   $phpcsFile->findPrevious(T_WHITESPACE($nextToken - 1)$arrayStarttrue);
  495.                 $indexStart $phpcsFile->findStartOfStatement($indexEnd);
  496.  
  497.                 if ($indexStart === $indexEnd{
  498.                     $currentEntry['index']         $indexEnd;
  499.                     $currentEntry['index_content'$tokens[$indexEnd]['content'];
  500.                 else {
  501.                     $currentEntry['index']         $indexStart;
  502.                     $currentEntry['index_content'$phpcsFile->getTokensAsString($indexStart($indexEnd $indexStart + 1));
  503.                 }
  504.  
  505.                 $indexLength strlen($currentEntry['index_content']);
  506.                 if ($maxLength $indexLength{
  507.                     $maxLength $indexLength;
  508.                 }
  509.  
  510.                 // Find the value of this index.
  511.                 $nextContent $phpcsFile->findNext(
  512.                     Tokens::$emptyTokens,
  513.                     ($nextToken + 1),
  514.                     $arrayEnd,
  515.                     true
  516.                 );
  517.  
  518.                 $currentEntry['value'$nextContent;
  519.                 $indices[$currentEntry;
  520.                 $lastToken $nextToken;
  521.             }//end if
  522.         }//end for
  523.  
  524.         // Check for mutli-line arrays that should be single-line.
  525.         $singleValue = false;
  526.  
  527.         if (empty($indices=== true{
  528.             $singleValue = true;
  529.         else if (count($indices=== 1 && $tokens[$lastToken]['code'=== T_COMMA{
  530.             // There may be another array value without a comma.
  531.             $exclude     = Tokens::$emptyTokens;
  532.             $exclude[]   = T_COMMA;
  533.             $nextContent $phpcsFile->findNext($exclude($indices[0]['value'+ 1)$arrayEndtrue);
  534.             if ($nextContent === false{
  535.                 $singleValue = true;
  536.             }
  537.         }
  538.  
  539.         if ($singleValue === true{
  540.             // Array cannot be empty, so this is a multi-line array with
  541.             // a single value. It should be defined on single line.
  542.             $error 'Multi-line array contains a single value; use single-line array instead';
  543.             $fix   $phpcsFile->addFixableError($error$stackPtr'MultiLineNotAllowed');
  544.  
  545.             if ($fix === true{
  546.                 $phpcsFile->fixer->beginChangeset();
  547.                 for ($i ($arrayStart + 1)$i $arrayEnd$i++{
  548.                     if ($tokens[$i]['code'!== T_WHITESPACE{
  549.                         break;
  550.                     }
  551.  
  552.                     $phpcsFile->fixer->replaceToken($i'');
  553.                 }
  554.  
  555.                 for ($i ($arrayEnd - 1)$i $arrayStart$i--{
  556.                     if ($tokens[$i]['code'!== T_WHITESPACE{
  557.                         break;
  558.                     }
  559.  
  560.                     $phpcsFile->fixer->replaceToken($i'');
  561.                 }
  562.  
  563.                 $phpcsFile->fixer->endChangeset();
  564.             }
  565.  
  566.             return;
  567.         }//end if
  568.  
  569.         /*
  570.             This section checks for arrays that don't specify keys.
  571.  
  572.             Arrays such as:
  573.                array(
  574.                 'aaa',
  575.                 'bbb',
  576.                 'd',
  577.                );
  578.         */
  579.  
  580.         if ($keyUsed === false && empty($indices=== false{
  581.             $count     count($indices);
  582.             $lastIndex $indices[($count - 1)]['value'];
  583.  
  584.             $trailingContent $phpcsFile->findPrevious(
  585.                 Tokens::$emptyTokens,
  586.                 ($arrayEnd - 1),
  587.                 $lastIndex,
  588.                 true
  589.             );
  590.  
  591.             if ($tokens[$trailingContent]['code'!== T_COMMA{
  592.                 $phpcsFile->recordMetric($stackPtr'Array end comma''no');
  593.                 $error 'Comma required after last value in array declaration';
  594.                 $fix   $phpcsFile->addFixableError($error$trailingContent'NoCommaAfterLast');
  595.                 if ($fix === true{
  596.                     $phpcsFile->fixer->addContent($trailingContent',');
  597.                 }
  598.             else {
  599.                 $phpcsFile->recordMetric($stackPtr'Array end comma''yes');
  600.             }
  601.  
  602.             $lastValueLine = false;
  603.             foreach ($indices as $value{
  604.                 if (empty($value['value']=== true{
  605.                     // Array was malformed and we couldn't figure out
  606.                     // the array value correctly, so we have to ignore it.
  607.                     // Other parts of this sniff will correct the error.
  608.                     continue;
  609.                 }
  610.  
  611.                 if ($lastValueLine !== false && $tokens[$value['value']]['line'=== $lastValueLine{
  612.                     $error 'Each value in a multi-line array must be on a new line';
  613.                     $fix   $phpcsFile->addFixableError($error$value['value']'ValueNoNewline');
  614.                     if ($fix === true{
  615.                         if ($tokens[($value['value'- 1)]['code'=== T_WHITESPACE{
  616.                             $phpcsFile->fixer->replaceToken(($value['value'- 1)'');
  617.                         }
  618.  
  619.                         $phpcsFile->fixer->addNewlineBefore($value['value']);
  620.                     }
  621.                 else if ($tokens[($value['value'- 1)]['code'=== T_WHITESPACE{
  622.                     $expected $keywordStart;
  623.  
  624.                     $first $phpcsFile->findFirstOnLine(T_WHITESPACE$value['value']true);
  625.                     $found ($tokens[$first]['column'- 1);
  626.                     if ($found !== $expected{
  627.                         $error 'Array value not aligned correctly; expected %s spaces but found %s';
  628.                         $data  = array(
  629.                                   $expected,
  630.                                   $found,
  631.                                  );
  632.  
  633.                         $fix $phpcsFile->addFixableError($error$value['value']'ValueNotAligned'$data);
  634.                         if ($fix === true{
  635.                             if ($found === 0{
  636.                                 $phpcsFile->fixer->addContent(($value['value'- 1)str_repeat(' '$expected));
  637.                             else {
  638.                                 $phpcsFile->fixer->replaceToken(($value['value'- 1)str_repeat(' '$expected));
  639.                             }
  640.                         }
  641.                     }
  642.                 }//end if
  643.  
  644.                 $lastValueLine $tokens[$value['value']]['line'];
  645.             }//end foreach
  646.         }//end if
  647.  
  648.         /*
  649.             Below the actual indentation of the array is checked.
  650.             Errors will be thrown when a key is not aligned, when
  651.             a double arrow is not aligned, and when a value is not
  652.             aligned correctly.
  653.             If an error is found in one of the above areas, then errors
  654.             are not reported for the rest of the line to avoid reporting
  655.             spaces and columns incorrectly. Often fixing the first
  656.             problem will fix the other 2 anyway.
  657.  
  658.             For example:
  659.  
  660.             $a = array(
  661.                   'index'  => '2',
  662.                  );
  663.  
  664.             or
  665.  
  666.             $a = [
  667.                   'index'  => '2',
  668.                  ];
  669.  
  670.             In this array, the double arrow is indented too far, but this
  671.             will also cause an error in the value's alignment. If the arrow were
  672.             to be moved back one space however, then both errors would be fixed.
  673.         */
  674.  
  675.         $numValues count($indices);
  676.  
  677.         $indicesStart  ($keywordStart + 1);
  678.         $arrowStart    ($indicesStart $maxLength + 1);
  679.         $valueStart    ($arrowStart + 3);
  680.         $indexLine     $tokens[$stackPtr]['line'];
  681.         $lastIndexLine = null;
  682.         foreach ($indices as $index{
  683.             if (isset($index['index']=== false{
  684.                 // Array value only.
  685.                 if ($tokens[$index['value']]['line'=== $tokens[$stackPtr]['line'&& $numValues > 1{
  686.                     $error 'The first value in a multi-value array must be on a new line';
  687.                     $fix   $phpcsFile->addFixableError($error$stackPtr'FirstValueNoNewline');
  688.                     if ($fix === true{
  689.                         $phpcsFile->fixer->addNewlineBefore($index['value']);
  690.                     }
  691.                 }
  692.  
  693.                 continue;
  694.             }
  695.  
  696.             $lastIndexLine $indexLine;
  697.             $indexLine     $tokens[$index['index']]['line'];
  698.  
  699.             if ($indexLine === $tokens[$stackPtr]['line']{
  700.                 $error 'The first index in a multi-value array must be on a new line';
  701.                 $fix   $phpcsFile->addFixableError($error$index['index']'FirstIndexNoNewline');
  702.                 if ($fix === true{
  703.                     $phpcsFile->fixer->addNewlineBefore($index['index']);
  704.                 }
  705.  
  706.                 continue;
  707.             }
  708.  
  709.             if ($indexLine === $lastIndexLine{
  710.                 $error 'Each index in a multi-line array must be on a new line';
  711.                 $fix   $phpcsFile->addFixableError($error$index['index']'IndexNoNewline');
  712.                 if ($fix === true{
  713.                     if ($tokens[($index['index'- 1)]['code'=== T_WHITESPACE{
  714.                         $phpcsFile->fixer->replaceToken(($index['index'- 1)'');
  715.                     }
  716.  
  717.                     $phpcsFile->fixer->addNewlineBefore($index['index']);
  718.                 }
  719.  
  720.                 continue;
  721.             }
  722.  
  723.             if ($tokens[$index['index']]['column'!== $indicesStart{
  724.                 $expected ($indicesStart - 1);
  725.                 $found    ($tokens[$index['index']]['column'- 1);
  726.                 $error    'Array key not aligned correctly; expected %s spaces but found %s';
  727.                 $data     = array(
  728.                              $expected,
  729.                              $found,
  730.                             );
  731.  
  732.                 $fix $phpcsFile->addFixableError($error$index['index']'KeyNotAligned'$data);
  733.                 if ($fix === true{
  734.                     if ($found === 0{
  735.                         $phpcsFile->fixer->addContent(($index['index'- 1)str_repeat(' '$expected));
  736.                     else {
  737.                         $phpcsFile->fixer->replaceToken(($index['index'- 1)str_repeat(' '$expected));
  738.                     }
  739.                 }
  740.  
  741.                 continue;
  742.             }
  743.  
  744.             if ($tokens[$index['arrow']]['column'!== $arrowStart{
  745.                 $expected ($arrowStart (strlen($index['index_content']$tokens[$index['index']]['column']));
  746.                 $found    ($tokens[$index['arrow']]['column'(strlen($index['index_content']$tokens[$index['index']]['column']));
  747.                 $error    'Array double arrow not aligned correctly; expected %s space(s) but found %s';
  748.                 $data     = array(
  749.                              $expected,
  750.                              $found,
  751.                             );
  752.  
  753.                 $fix $phpcsFile->addFixableError($error$index['arrow']'DoubleArrowNotAligned'$data);
  754.                 if ($fix === true{
  755.                     if ($found === 0{
  756.                         $phpcsFile->fixer->addContent(($index['arrow'- 1)str_repeat(' '$expected));
  757.                     else {
  758.                         $phpcsFile->fixer->replaceToken(($index['arrow'- 1)str_repeat(' '$expected));
  759.                     }
  760.                 }
  761.  
  762.                 continue;
  763.             }
  764.  
  765.             if ($tokens[$index['value']]['column'!== $valueStart{
  766.                 $expected ($valueStart ($tokens[$index['arrow']]['length'$tokens[$index['arrow']]['column']));
  767.                 $found    ($tokens[$index['value']]['column'($tokens[$index['arrow']]['length'$tokens[$index['arrow']]['column']));
  768.                 if ($found < 0{
  769.                     $found 'newline';
  770.                 }
  771.  
  772.                 $error 'Array value not aligned correctly; expected %s space(s) but found %s';
  773.                 $data  = array(
  774.                           $expected,
  775.                           $found,
  776.                          );
  777.  
  778.                 $fix $phpcsFile->addFixableError($error$index['arrow']'ValueNotAligned'$data);
  779.                 if ($fix === true{
  780.                     if ($found === 'newline'{
  781.                         $prev $phpcsFile->findPrevious(T_WHITESPACE($index['value'- 1)nulltrue);
  782.                         $phpcsFile->fixer->beginChangeset();
  783.                         for ($i ($prev + 1)$i $index['value']$i++{
  784.                             $phpcsFile->fixer->replaceToken($i'');
  785.                         }
  786.  
  787.                         $phpcsFile->fixer->replaceToken(($index['value'- 1)str_repeat(' '$expected));
  788.                         $phpcsFile->fixer->endChangeset();
  789.                     else if ($found === 0{
  790.                         $phpcsFile->fixer->addContent(($index['value'- 1)str_repeat(' '$expected));
  791.                     else {
  792.                         $phpcsFile->fixer->replaceToken(($index['value'- 1)str_repeat(' '$expected));
  793.                     }
  794.                 }
  795.             }//end if
  796.  
  797.             // Check each line ends in a comma.
  798.             $valueLine $tokens[$index['value']]['line'];
  799.             $nextComma = false;
  800.             for ($i $index['value']$i $arrayEnd$i++{
  801.                 // Skip bracketed statements, like function calls.
  802.                 if ($tokens[$i]['code'=== T_OPEN_PARENTHESIS{
  803.                     $i         $tokens[$i]['parenthesis_closer'];
  804.                     $valueLine $tokens[$i]['line'];
  805.                     continue;
  806.                 }
  807.  
  808.                 if ($tokens[$i]['code'=== T_ARRAY{
  809.                     $i         $tokens[$tokens[$i]['parenthesis_opener']]['parenthesis_closer'];
  810.                     $valueLine $tokens[$i]['line'];
  811.                     continue;
  812.                 }
  813.  
  814.                 // Skip to the end of multi-line strings.
  815.                 if (isset(Tokens::$stringTokens[$tokens[$i]['code']]=== true{
  816.                     $i $phpcsFile->findNext($tokens[$i]['code']($i + 1)nulltrue);
  817.                     $i--;
  818.                     $valueLine $tokens[$i]['line'];
  819.                     continue;
  820.                 }
  821.  
  822.                 if ($tokens[$i]['code'=== T_OPEN_SHORT_ARRAY{
  823.                     $i         $tokens[$i]['bracket_closer'];
  824.                     $valueLine $tokens[$i]['line'];
  825.                     continue;
  826.                 }
  827.  
  828.                 if ($tokens[$i]['code'=== T_CLOSURE{
  829.                     $i         $tokens[$i]['scope_closer'];
  830.                     $valueLine $tokens[$i]['line'];
  831.                     continue;
  832.                 }
  833.  
  834.                 if ($tokens[$i]['code'=== T_COMMA{
  835.                     $nextComma $i;
  836.                     break;
  837.                 }
  838.             }//end for
  839.  
  840.             if ($nextComma === false || ($tokens[$nextComma]['line'!== $valueLine)) {
  841.                 $error 'Each line in an array declaration must end in a comma';
  842.                 $fix   $phpcsFile->addFixableError($error$index['value']'NoComma');
  843.  
  844.                 if ($fix === true{
  845.                     // Find the end of the line and put a comma there.
  846.                     for ($i ($index['value'+ 1)$i $arrayEnd$i++{
  847.                         if ($tokens[$i]['line'$valueLine{
  848.                             break;
  849.                         }
  850.                     }
  851.  
  852.                     $phpcsFile->fixer->addContentBefore(($i - 1)',');
  853.                 }
  854.             }
  855.  
  856.             // Check that there is no space before the comma.
  857.             if ($nextComma !== false && $tokens[($nextComma - 1)]['code'=== T_WHITESPACE{
  858.                 $content     $tokens[($nextComma - 2)]['content'];
  859.                 $spaceLength $tokens[($nextComma - 1)]['length'];
  860.                 $error       'Expected 0 spaces between "%s" and comma; %s found';
  861.                 $data        = array(
  862.                                 $content,
  863.                                 $spaceLength,
  864.                                );
  865.  
  866.                 $fix $phpcsFile->addFixableError($error$nextComma'SpaceBeforeComma'$data);
  867.                 if ($fix === true{
  868.                     $phpcsFile->fixer->replaceToken(($nextComma - 1)'');
  869.                 }
  870.             }
  871.         }//end foreach
  872.  
  873.     }//end processMultiLineArray()
  874.  
  875.  
  876. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.