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

Source for file SAPIUsageSniff.php

Documentation is available at SAPIUsageSniff.php

  1. <?php
  2. /**
  3.  * Ensures the PHP_SAPI constant is used instead of php_sapi_name().
  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.  
  15. class SAPIUsageSniff 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_STRING);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, 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.         $tokens $phpcsFile->getTokens();
  43.  
  44.         $ignore = array(
  45.                    T_DOUBLE_COLON    => true,
  46.                    T_OBJECT_OPERATOR => true,
  47.                    T_FUNCTION        => true,
  48.                    T_CONST           => true,
  49.                   );
  50.  
  51.         $prevToken $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  52.         if (isset($ignore[$tokens[$prevToken]['code']]=== true{
  53.             // Not a call to a PHP function.
  54.             return;
  55.         }
  56.  
  57.         $function strtolower($tokens[$stackPtr]['content']);
  58.         if ($function === 'php_sapi_name'{
  59.             $error 'Use the PHP_SAPI constant instead of calling php_sapi_name()';
  60.             $phpcsFile->addError($error$stackPtr'FunctionFound');
  61.         }
  62.  
  63.     }//end process()
  64.  
  65.  
  66. }//end class

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