1<?php 2/** 3 * PSR2_Sniffs_Files_ClosingTagsSniff. 4 * 5 * PHP version 5 6 * 7 * @category PHP 8 * @package PHP_CodeSniffer 9 * @author Greg Sherwood <gsherwood@squiz.net> 10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 11 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 12 * @link http://pear.php.net/package/PHP_CodeSniffer 13 */ 14 15/** 16 * PSR2_Sniffs_Files_LineEndingsSniff. 17 * 18 * Checks that the file does not end with a closing tag. 19 * 20 * @category PHP 21 * @package PHP_CodeSniffer 22 * @author Greg Sherwood <gsherwood@squiz.net> 23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 24 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 25 * @version Release: @package_version@ 26 * @link http://pear.php.net/package/PHP_CodeSniffer 27 */ 28class PSR2_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff 29{ 30 31 32 /** 33 * Returns an array of tokens this test wants to listen for. 34 * 35 * @return array 36 */ 37 public function register() 38 { 39 return array(T_OPEN_TAG); 40 41 }//end register() 42 43 44 /** 45 * Processes this sniff, when one of its tokens is encountered. 46 * 47 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 48 * @param int $stackPtr The position of the current token in 49 * the stack passed in $tokens. 50 * 51 * @return void 52 */ 53 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 54 { 55 $tokens = $phpcsFile->getTokens(); 56 57 // Make sure this file only contains PHP code. 58 for ($i = 0; $i < $phpcsFile->numTokens; $i++) { 59 if ($tokens[$i]['code'] === T_INLINE_HTML 60 && trim($tokens[$i]['content']) !== '' 61 ) { 62 return $phpcsFile->numTokens; 63 } 64 } 65 66 // Find the last non-empty token. 67 for ($last = ($phpcsFile->numTokens - 1); $last > 0; $last--) { 68 if (trim($tokens[$last]['content']) !== '') { 69 break; 70 } 71 } 72 73 if ($tokens[$last]['code'] === T_CLOSE_TAG) { 74 $error = 'A closing tag is not permitted at the end of a PHP file'; 75 $fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); 76 if ($fix === true) { 77 $phpcsFile->fixer->beginChangeset(); 78 $phpcsFile->fixer->replaceToken($last, $phpcsFile->eolChar); 79 $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($last - 1), null, true); 80 if ($tokens[$prev]['code'] !== T_SEMICOLON 81 && $tokens[$prev]['code'] !== T_CLOSE_CURLY_BRACKET 82 ) { 83 $phpcsFile->fixer->addContent($prev, ';'); 84 } 85 86 $phpcsFile->fixer->endChangeset(); 87 } 88 89 $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); 90 } else { 91 $phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no'); 92 } 93 94 // Ignore the rest of the file. 95 return $phpcsFile->numTokens; 96 97 }//end process() 98 99 100}//end class 101