PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file DisallowShortOpenTagSniff.php

Documentation is available at DisallowShortOpenTagSniff.php

  1. <?php
  2. /**
  3.  * Makes sure that shorthand PHP open tags are not used.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class DisallowShortOpenTagSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         $targets = array(
  28.                     T_OPEN_TAG,
  29.                     T_OPEN_TAG_WITH_ECHO,
  30.                    );
  31.  
  32.         $shortOpenTags = (boolean) ini_get('short_open_tag');
  33.         if ($shortOpenTags === false{
  34.             $targets[= T_INLINE_HTML;
  35.         }
  36.  
  37.         return $targets;
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46.      * @param int                         $stackPtr  The position of the current token
  47.      *                                                in the stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens $phpcsFile->getTokens();
  54.         $token  $tokens[$stackPtr];
  55.  
  56.         if ($token['code'=== T_OPEN_TAG && $token['content'=== '<?'{
  57.             $error 'Short PHP opening tag used; expected "<?php" but found "%s"';
  58.             $data  = array($token['content']);
  59.             $fix   $phpcsFile->addFixableError($error$stackPtr'Found'$data);
  60.             if ($fix === true{
  61.                 $correctOpening '<?php';
  62.                 if (isset($tokens[($stackPtr + 1)]=== true && $tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  63.                     // Avoid creation of invalid open tags like <?phpecho if the original was <?echo .
  64.                     $correctOpening .= ' ';
  65.                 }
  66.  
  67.                 $phpcsFile->fixer->replaceToken($stackPtr$correctOpening);
  68.             }
  69.  
  70.             $phpcsFile->recordMetric($stackPtr'PHP short open tag used''yes');
  71.         else {
  72.             $phpcsFile->recordMetric($stackPtr'PHP short open tag used''no');
  73.         }
  74.  
  75.         if ($token['code'=== T_OPEN_TAG_WITH_ECHO{
  76.             $nextVar $tokens[$phpcsFile->findNext(Tokens::$emptyTokens($stackPtr + 1)nulltrue)];
  77.             $error   'Short PHP opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
  78.             $data    = array(
  79.                         $nextVar['content'],
  80.                         $token['content'],
  81.                         $nextVar['content'],
  82.                        );
  83.             $fix     $phpcsFile->addFixableError($error$stackPtr'EchoFound'$data);
  84.             if ($fix === true{
  85.                 if ($tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  86.                     $phpcsFile->fixer->replaceToken($stackPtr'<?php echo ');
  87.                 else {
  88.                     $phpcsFile->fixer->replaceToken($stackPtr'<?php echo');
  89.                 }
  90.             }
  91.         }
  92.  
  93.         if ($token['code'=== T_INLINE_HTML{
  94.             $content     $token['content'];
  95.             $openerFound strpos($content'<?');
  96.  
  97.             if ($openerFound === false{
  98.                 return;
  99.             }
  100.  
  101.             $closerFound = false;
  102.  
  103.             // Inspect current token and subsequent inline HTML token to find a close tag.
  104.             for ($i $stackPtr$i $phpcsFile->numTokens; $i++{
  105.                 if ($tokens[$i]['code'!== T_INLINE_HTML{
  106.                     break;
  107.                 }
  108.  
  109.                 $closerFound strrpos($tokens[$i]['content']'?>');
  110.                 if ($closerFound !== false{
  111.                     if ($i !== $stackPtr{
  112.                         break;
  113.                     else if ($closerFound $openerFound{
  114.                         break;
  115.                     else {
  116.                         $closerFound = false;
  117.                     }
  118.                 }
  119.             }
  120.  
  121.             if ($closerFound !== false{
  122.                 $error   'Possible use of short open tags detected; found: %s';
  123.                 $snippet $this->getSnippet($content'<?');
  124.                 $data    = array('<?'.$snippet);
  125.  
  126.                 $phpcsFile->addWarning($error$stackPtr'PossibleFound'$data);
  127.  
  128.                 // Skip forward to the token containing the closer.
  129.                 if (($i - 1$stackPtr{
  130.                     return $i;
  131.                 }
  132.             }
  133.         }//end if
  134.  
  135.     }//end process()
  136.  
  137.  
  138.     /**
  139.      * Get a snippet from a HTML token.
  140.      *
  141.      * @param string $content The content of the HTML token.
  142.      * @param string $start   Partial string to use as a starting point for the snippet.
  143.      * @param int    $length  The target length of the snippet to get. Defaults to 40.
  144.      *
  145.      * @return string 
  146.      */
  147.     protected function getSnippet($content$start=''$length=40)
  148.     {
  149.         $startPos = 0;
  150.  
  151.         if ($start !== ''{
  152.             $startPos strpos($content$start);
  153.             if ($startPos !== false{
  154.                 $startPos += strlen($start);
  155.             }
  156.         }
  157.  
  158.         $snippet substr($content$startPos$length);
  159.         if ((strlen($content$startPos$length{
  160.             $snippet .= '...';
  161.         }
  162.  
  163.         return $snippet;
  164.  
  165.     }//end getSnippet()
  166.  
  167.  
  168. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.