Skip to content

Commit

Permalink
Add sniff to disallow alternative PHP open tags.
Browse files Browse the repository at this point in the history
Fixes WordPress#580

This should be removed again if the upstream issue squizlabs/PHP_CodeSniffer#1063 would get traction and the associated PR would be merged *and* the minimum PHPCS version for WPCS would become higher than the version in which the PR is merged ;-)
  • Loading branch information
jrfnl committed Jul 11, 2016
1 parent 15460b4 commit 271f546
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- http://make.wordpress.org/core/handbook/coding-standards/php/#no-shorthand-php-tags -->
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<rule ref="WordPress.PHP.DisallowAlternativeOpenTag"/>

<!-- important to prevent issues with content being sent before headers -->
<rule ref="Generic.Files.ByteOrderMark"/>
Expand Down
64 changes: 64 additions & 0 deletions WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Makes sure that no alternative PHP open tags are used.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Juliette Reinders Folmer <wpplugins_nospam@adviesenzo.nl>
*/
class WordPress_Sniffs_PHP_DisallowAlternativeOpenTagSniff 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,
T_OPEN_TAG_WITH_ECHO,
);

} //end register()


/**
* Processes this test, 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();
$openTag = $tokens[ $stackPtr ];

if ( '<%' === $openTag['content'] ) {
$error = 'ASP style opening tag used; expected "<?php" but found "%s"';
$data = array( $openTag['content'] );
$phpcsFile->addError( $error, $stackPtr, 'ASPOpenTag', $data );
}

if ( '<script language="php">' === $openTag['content'] ) {
$error = 'Script style opening tag used; expected "<?php" but found "%s"';
$data = array( $openTag['content'] );
$phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTag', $data );
}

if ( T_OPEN_TAG_WITH_ECHO === $openTag['code'] && '<%=' === $openTag['content'] ) {
$error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$nextVar = $tokens[ $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ) ];
$data = array(
$nextVar['content'],
$openTag['content'],
$nextVar['content'],
);
$phpcsFile->addError( $error, $stackPtr, 'ShortASPOpenTag', $data );
}

}//end process()


}//end class
9 changes: 9 additions & 0 deletions WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<?php echo $var; ?>
Some content here.
<% echo $var; %>
<%= echo $var; %>
<script language="php">
echo $var;
</script>
</div>
45 changes: 45 additions & 0 deletions WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Unit test class for the DisallowAlternativeOpenTag 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 Juliette Reinders Folmer <wpplugins_nospam@adviesenzo.nl>
*/
class WordPress_Tests_PHP_DisallowAlternativeOpenTagUnitTest 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.
*
* @return array<int, int>
*/
public function getErrorList() {
return array(
4 => 1,
5 => 1,
6 => 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

0 comments on commit 271f546

Please sign in to comment.