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

Source for file ClosingTagSniff.php

Documentation is available at ClosingTagSniff.php

  1. <?php
  2. /**
  3.  * Checks that the file does not end with a closing tag.
  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\PSR2\Sniffs\Files;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ClosingTagSniff 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(T_OPEN_TAG);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this sniff, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token in
  37.      *                                                the stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         // Make sure this file only contains PHP code.
  46.         for ($i = 0; $i $phpcsFile->numTokens; $i++{
  47.             if ($tokens[$i]['code'=== T_INLINE_HTML
  48.                 && trim($tokens[$i]['content']!== ''
  49.             {
  50.                 return $phpcsFile->numTokens;
  51.             }
  52.         }
  53.  
  54.         // Find the last non-empty token.
  55.         for ($last ($phpcsFile->numTokens - 1)$last > 0; $last--{
  56.             if (trim($tokens[$last]['content']!== ''{
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         if ($tokens[$last]['code'=== T_CLOSE_TAG{
  62.             $error 'A closing tag is not permitted at the end of a PHP file';
  63.             $fix   $phpcsFile->addFixableError($error$last'NotAllowed');
  64.             if ($fix === true{
  65.                 $phpcsFile->fixer->beginChangeset();
  66.                 $phpcsFile->fixer->replaceToken($last$phpcsFile->eolChar);
  67.                 $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($last - 1)nulltrue);
  68.                 if ($tokens[$prev]['code'!== T_SEMICOLON
  69.                     && $tokens[$prev]['code'!== T_CLOSE_CURLY_BRACKET
  70.                 {
  71.                     $phpcsFile->fixer->addContent($prev';');
  72.                 }
  73.  
  74.                 $phpcsFile->fixer->endChangeset();
  75.             }
  76.  
  77.             $phpcsFile->recordMetric($stackPtr'PHP closing tag at end of PHP-only file''yes');
  78.         else {
  79.             $phpcsFile->recordMetric($stackPtr'PHP closing tag at end of PHP-only file''no');
  80.         }
  81.  
  82.         // Ignore the rest of the file.
  83.         return $phpcsFile->numTokens;
  84.  
  85.     }//end process()
  86.  
  87.  
  88. }//end class

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