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

Source for file MultiLineAssignmentSniff.php

Documentation is available at MultiLineAssignmentSniff.php

  1. <?php
  2. /**
  3.  * If an assignment goes over two lines, ensure the equal sign is indented.
  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\PEAR\Sniffs\Formatting;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class MultiLineAssignmentSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * The number of spaces code should be indented.
  20.      *
  21.      * @var integer 
  22.      */
  23.     public $indent = 4;
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(T_EQUAL);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  42.      * @param int                         $stackPtr  The position of the current token
  43.      *                                                in the stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         // Equal sign can't be the last thing on the line.
  52.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  53.         if ($next === false{
  54.             // Bad assignment.
  55.             return;
  56.         }
  57.  
  58.         if ($tokens[$next]['line'!== $tokens[$stackPtr]['line']{
  59.             $error 'Multi-line assignments must have the equal sign on the second line';
  60.             $phpcsFile->addError($error$stackPtr'EqualSignLine');
  61.             return;
  62.         }
  63.  
  64.         // Make sure it is the first thing on the line, otherwise we ignore it.
  65.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)falsetrue);
  66.         if ($prev === false{
  67.             // Bad assignment.
  68.             return;
  69.         }
  70.  
  71.         if ($tokens[$prev]['line'=== $tokens[$stackPtr]['line']{
  72.             return;
  73.         }
  74.  
  75.         // Find the required indent based on the ident of the previous line.
  76.         $assignmentIndent = 0;
  77.         $prevLine         $tokens[$prev]['line'];
  78.         for ($i ($prev - 1)$i >= 0; $i--{
  79.             if ($tokens[$i]['line'!== $prevLine{
  80.                 $i++;
  81.                 break;
  82.             }
  83.         }
  84.  
  85.         if ($tokens[$i]['code'=== T_WHITESPACE{
  86.             $assignmentIndent strlen($tokens[$i]['content']);
  87.         }
  88.  
  89.         // Find the actual indent.
  90.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1));
  91.  
  92.         $expectedIndent ($assignmentIndent $this->indent);
  93.         $foundIndent    strlen($tokens[$prev]['content']);
  94.         if ($foundIndent !== $expectedIndent{
  95.             $error 'Multi-line assignment not indented correctly; expected %s spaces but found %s';
  96.             $data  = array(
  97.                       $expectedIndent,
  98.                       $foundIndent,
  99.                      );
  100.             $phpcsFile->addError($error$stackPtr'Indent'$data);
  101.         }
  102.  
  103.     }//end process()
  104.  
  105.  
  106. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:38 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.