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 32699fe
Show file tree
Hide file tree
Showing 4 changed files with 143 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
79 changes: 79 additions & 0 deletions WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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() {
$tokens = array(
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
);

$option = (boolean) ini_get( 'asp_tags' );
if ( false === $option ) {
$tokens[] = T_INLINE_HTML;
}

return $tokens;

} //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 ];
$option = (boolean) ini_get( 'asp_tags' );

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

if ( ( false === $option && T_INLINE_HTML === $openTag['code'] ) && ( 0 === strpos( $openTag['content'], '<% ' ) || 0 === strpos( $openTag['content'], '<%= ' ) ) ) {
$error = 'Possible use of ASP style opening tags. Needs manual inspection. Found: %s';
$snippet = substr( $openTag['content'], 0, 20 );
if ( strlen( $openTag['content'] ) > 20 ) {
$snippet .= '...';
}
$data = array( $snippet );
$phpcsFile->addWarning( $error, $stackPtr, 'PotentialASPOpenTagFound', $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>
54 changes: 54 additions & 0 deletions WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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() {
$errors = array(
6 => 1,
);
$option = (boolean) ini_get( 'asp_tags' );
if ( true === $option ) {
$errors[4] = 1;
$errors[5] = 1;
}
return $errors;
}

/**
* 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() {
$warnings = array();
$option = (boolean) ini_get( 'asp_tags' );
if ( false === $option ) {
$warnings = array(
4 => 1,
5 => 1,
);
}
return $warnings;

}
} //end class

0 comments on commit 32699fe

Please sign in to comment.