forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug squizlabs#453 : PSR2 standard does not allow closing tag fo…
…r mixed PHP/HTML files
- Loading branch information
Showing
8 changed files
with
198 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
CodeSniffer/Standards/PSR2/Sniffs/Files/ClosingTagSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* PSR2_Sniffs_Files_ClosingTagsSniff. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* PSR2_Sniffs_Files_LineEndingsSniff. | ||
* | ||
* Checks that the file does not end with a closing tag. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class PSR2_Sniffs_Files_ClosingTagSniff implements PHP_CodeSniffer_Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return array(T_OPEN_TAG); | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this sniff, when one of its tokens is encountered. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in | ||
* the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
// Make sure this file only contains PHP code. | ||
for ($i = 0; $i < $phpcsFile->numTokens; $i++) { | ||
if ($tokens[$i]['code'] === T_INLINE_HTML | ||
&& trim($tokens[$i]['content']) !== '' | ||
) { | ||
return $phpcsFile->numTokens; | ||
} | ||
} | ||
|
||
// Find the last non-empty token. | ||
for ($last = ($phpcsFile->numTokens - 1); $last > 0; $last--) { | ||
if (trim($tokens[$last]['content']) !== '') { | ||
break; | ||
} | ||
} | ||
|
||
if ($tokens[$last]['code'] === T_CLOSE_TAG) { | ||
$error = 'A closing tag is not permitted at the end of a PHP file'; | ||
$fix = $phpcsFile->addFixableError($error, $last, 'NotAllowed'); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->replaceToken($last, ''); | ||
} | ||
|
||
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'yes'); | ||
} else { | ||
$phpcsFile->recordMetric($stackPtr, 'PHP closing tag at end of PHP-only file', 'no'); | ||
} | ||
|
||
// Ignore the rest of the file. | ||
return $phpcsFile->numTokens; | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
12 changes: 12 additions & 0 deletions
12
CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.1.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
echo 'hi'; | ||
|
||
?> | ||
|
||
<?php | ||
|
||
echo 'bye'; | ||
|
||
?> | ||
|
3 changes: 3 additions & 0 deletions
3
CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.2.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="clear"></div> | ||
<?php include('inc.php'); ?> | ||
</div> |
7 changes: 7 additions & 0 deletions
7
CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.3.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
$arr = [1, 2, 3]; | ||
?> | ||
|
||
A: <?= $arr[0] ?> | ||
B: <?= $arr[1] ?> | ||
C: <?= $arr[2] ?> |
71 changes: 71 additions & 0 deletions
71
CodeSniffer/Standards/PSR2/Tests/Files/ClosingTagUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Unit test class for the ClosingTag sniff. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
/** | ||
* Unit test class for the ClosingTag sniff. | ||
* | ||
* A sniff unit test checks a .inc file for expected violations of a single | ||
* coding standard. Expected errors and warnings are stored in this class. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Greg Sherwood <[email protected]> | ||
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class PSR2_Tests_Files_ClosingTagUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList($testFile='') | ||
{ | ||
if ($testFile !== 'ClosingTagUnitTest.1.inc') { | ||
return array(); | ||
} | ||
|
||
return array( | ||
11 => 1, | ||
); | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return array(); | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters