Skip to content

Commit

Permalink
Make alternative openings tags fixable.
Browse files Browse the repository at this point in the history
Only implemented for when we are sure we've found alternative opening tags.
The 'maybe' causes which need manual inspection should not be auto-fixable.
  • Loading branch information
jrfnl committed Jul 20, 2016
1 parent 961d13c commit 2cdba84
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
72 changes: 69 additions & 3 deletions WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,33 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
if ( '<%' === $openTag['content'] ) {
$error = 'ASP style opening tag used; expected "<?php" but found "%s"';
$data = array( $openTag['content'] );
$phpcsFile->addError( $error, $stackPtr, 'ASPOpenTagFound', $data );

$closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' );

if ( false === $closer ) {
$phpcsFile->addError( $error, $stackPtr, 'ASPOpenTagFound', $data );
} else {
$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPOpenTagFound', $data );
if ( true === $fix ) {
$this->add_changeset( $phpcsFile, $stackPtr, $closer );
}
}
}

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 );

$closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '</script>' );

if ( false === $closer ) {
$phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTagFound', $data );
} else {
$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ScriptOpenTagFound', $data );
if ( true === $fix ) {
$this->add_changeset( $phpcsFile, $stackPtr, $closer );
}
}
}
}

Expand All @@ -77,7 +97,17 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$openTag['content'],
$nextVar['content'],
);
$phpcsFile->addError( $error, $stackPtr, 'ASPShortOpenTagFound', $data );

$closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' );

if ( false === $closer ) {
$phpcsFile->addError( $error, $stackPtr, 'ASPShortOpenTagFound', $data );
} else {
$fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPShortOpenTagFound', $data );
if ( true === $fix ) {
$this->add_changeset( $phpcsFile, $stackPtr, $closer );
}
}
}

if ( ( true === $asp_enabled && false === $short_enabled ) && ( T_INLINE_HTML === $openTag['code'] && 0 === strpos( $openTag['content'], '<%=' ) ) ) {
Expand Down Expand Up @@ -120,4 +150,40 @@ private function get_snippet( $content, $length = 40 ) {
return $snippet;
} // end get_snippet()

/**
* Try and find a matching PHP closing tag.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $tokens The token stack.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $content The expected content of the closing tag to match the opener.
* @return int|false Pointer to the position in the stack for the closing tag or false if not found.
*/
private function find_closing_tag( PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content ) {
$closer = $phpcsFile->findNext( T_CLOSE_TAG, ( $stackPtr + 1 ) );

if ( false !== $closer ) {
if ( $content === $tokens[ $closer ]['content'] ) {
return $closer;
}
}

return false;
} // end find_closing_tag()

/**
* Add a changeset to replace the alternative PHP tags.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $open_tag_pointer Stack pointer to the PHP open tag.
* @param int $close_tag_pointer Stack pointer to the PHP close tag.
*/
private function add_changeset( $phpcsFile, $open_tag_pointer, $close_tag_pointer ) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken( $open_tag_pointer, '<?php' );
$phpcsFile->fixer->replaceToken( $close_tag_pointer, '?>' );
$phpcsFile->fixer->endChangeset();
} // end add_changeset()

} // end class
9 changes: 9 additions & 0 deletions WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<?php echo $var; ?>
Some content here.
<?php echo $var; ?>
<?php echo $var . ' and some more text to make sure the snippet works'; ?>
<?php
echo $var;
?>
</div>

0 comments on commit 2cdba84

Please sign in to comment.