Source for file DisallowShortOpenTagSniff.php
Documentation is available at DisallowShortOpenTagSniff.php
* Makes sure that shorthand PHP open tags are not used.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class DisallowShortOpenTagSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
$shortOpenTags = (boolean) ini_get('short_open_tag');
if ($shortOpenTags === false ) {
$targets[] = T_INLINE_HTML;
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$token = $tokens[$stackPtr];
if ($token['code'] === T_OPEN_TAG && $token['content'] === '<?') {
$error = 'Short PHP opening tag used; expected "<?php" but found "%s"';
$data = array ($token['content']);
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'Found', $data);
$correctOpening = '<?php';
if (isset ($tokens[($stackPtr + 1 )]) === true && $tokens[($stackPtr + 1 )]['code'] !== T_WHITESPACE ) {
// Avoid creation of invalid open tags like <?phpecho if the original was <?echo .
$phpcsFile->fixer ->replaceToken ($stackPtr, $correctOpening);
$phpcsFile->recordMetric ($stackPtr, 'PHP short open tag used', 'yes');
$phpcsFile->recordMetric ($stackPtr, 'PHP short open tag used', 'no');
if ($token['code'] === T_OPEN_TAG_WITH_ECHO ) {
$nextVar = $tokens[$phpcsFile->findNext (Tokens ::$emptyTokens, ($stackPtr + 1 ), null , true )];
$error = 'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$fix = $phpcsFile->addFixableError ($error, $stackPtr, 'EchoFound', $data);
if ($tokens[($stackPtr + 1 )]['code'] !== T_WHITESPACE ) {
$phpcsFile->fixer ->replaceToken ($stackPtr, '<?php echo ');
$phpcsFile->fixer ->replaceToken ($stackPtr, '<?php echo');
if ($token['code'] === T_INLINE_HTML ) {
$content = $token['content'];
$openerFound = strpos($content, '<?');
if ($openerFound === false ) {
// Inspect current token and subsequent inline HTML token to find a close tag.
for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++ ) {
if ($tokens[$i]['code'] !== T_INLINE_HTML ) {
$closerFound = strrpos($tokens[$i]['content'], '?>');
if ($closerFound !== false ) {
} else if ($closerFound > $openerFound) {
if ($closerFound !== false ) {
$error = 'Possible use of short open tags detected; found: %s';
$snippet = $this->getSnippet ($content, '<?');
$data = array ('<?'. $snippet);
$phpcsFile->addWarning ($error, $stackPtr, 'PossibleFound', $data);
// Skip forward to the token containing the closer.
if (($i - 1 ) > $stackPtr) {
* Get a snippet from a HTML token.
* @param string $content The content of the HTML token.
* @param string $start Partial string to use as a starting point for the snippet.
* @param int $length The target length of the snippet to get. Defaults to 40.
protected function getSnippet ($content, $start= '', $length=40 )
$startPos = strpos($content, $start);
if ($startPos !== false ) {
$snippet = substr($content, $startPos, $length);
if ((strlen($content) - $startPos) > $length) {
Documentation generated on Mon, 11 Mar 2019 15:27:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|