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

Source for file DisallowMultipleStatementsSniff.php

Documentation is available at DisallowMultipleStatementsSniff.php

  1. <?php
  2. /**
  3.  * Ensures each statement is on a line by itself.
  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\Generic\Sniffs\Formatting;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DisallowMultipleStatementsSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(T_SEMICOLON);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in
  36.      *                                                the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         $prev $phpcsFile->findPrevious(array(T_SEMICOLONT_OPEN_TAGT_OPEN_TAG_WITH_ECHO)($stackPtr - 1));
  45.         if ($prev === false
  46.             || $tokens[$prev]['code'=== T_OPEN_TAG
  47.             || $tokens[$prev]['code'=== T_OPEN_TAG_WITH_ECHO
  48.         {
  49.             $phpcsFile->recordMetric($stackPtr'Multiple statements on same line''no');
  50.             return;
  51.         }
  52.  
  53.         // Ignore multiple statements in a FOR condition.
  54.         if (isset($tokens[$stackPtr]['nested_parenthesis']=== true{
  55.             foreach ($tokens[$stackPtr]['nested_parenthesis'as $bracket{
  56.                 if (isset($tokens[$bracket]['parenthesis_owner']=== false{
  57.                     // Probably a closure sitting inside a function call.
  58.                     continue;
  59.                 }
  60.  
  61.                 $owner $tokens[$bracket]['parenthesis_owner'];
  62.                 if ($tokens[$owner]['code'=== T_FOR{
  63.                     return;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         if ($tokens[$prev]['line'=== $tokens[$stackPtr]['line']{
  69.             $phpcsFile->recordMetric($stackPtr'Multiple statements on same line''yes');
  70.  
  71.             $error 'Each PHP statement must be on a line by itself';
  72.             $fix   $phpcsFile->addFixableError($error$stackPtr'SameLine');
  73.             if ($fix === true{
  74.                 $phpcsFile->fixer->beginChangeset();
  75.                 $phpcsFile->fixer->addNewline($prev);
  76.                 if ($tokens[($prev + 1)]['code'=== T_WHITESPACE{
  77.                     $phpcsFile->fixer->replaceToken(($prev + 1)'');
  78.                 }
  79.  
  80.                 $phpcsFile->fixer->endChangeset();
  81.             }
  82.         else {
  83.             $phpcsFile->recordMetric($stackPtr'Multiple statements on same line''no');
  84.         }
  85.  
  86.     }//end process()
  87.  
  88.  
  89. }//end class

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