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

Source for file CharacterBeforePHPOpeningTagSniff.php

Documentation is available at CharacterBeforePHPOpeningTagSniff.php

  1. <?php
  2. /**
  3.  * Checks that the opening PHP tag is the first content in a file.
  4.  *
  5.  * @author    Andy Grunwald <andygrunwald@gmail.com>
  6.  * @copyright 2010-2014 Andy Grunwald
  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.  
  15. class CharacterBeforePHPOpeningTagSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(T_OPEN_TAG);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in
  36.      *                                                the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $expected = 0;
  43.         if ($stackPtr > 0{
  44.             // Allow a shebang line.
  45.             $tokens $phpcsFile->getTokens();
  46.             if (substr($tokens[0]['content']02=== '#!'{
  47.                 $expected = 1;
  48.             }
  49.         }
  50.  
  51.         if ($stackPtr !== $expected{
  52.             $error 'The opening PHP tag must be the first content in the file';
  53.             $phpcsFile->addError($error$stackPtr'Found');
  54.         }
  55.  
  56.         // Skip the rest of the file so we don't pick up additional
  57.         // open tags, typically embedded in HTML.
  58.         return $phpcsFile->numTokens;
  59.  
  60.     }//end process()
  61.  
  62.  
  63. }//end class

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