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

Source for file LowercasePHPFunctionsSniff.php

Documentation is available at LowercasePHPFunctionsSniff.php

  1. <?php
  2. /**
  3.  * Ensures all calls to inbuilt PHP functions are lowercase.
  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\Squiz\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class LowercasePHPFunctionsSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * String -> int hash map of all php built in function names
  20.      *
  21.      * @var array 
  22.      */
  23.     private $builtInFunctions;
  24.  
  25.  
  26.     /**
  27.      * Construct the LowercasePHPFunctionSniff
  28.      */
  29.     public function __construct()
  30.     {
  31.  
  32.         $allFunctions           get_defined_functions();
  33.         $this->builtInFunctions array_flip($allFunctions['internal']);
  34.  
  35.     }//end __construct()
  36.  
  37.  
  38.     /**
  39.      * Returns an array of tokens this test wants to listen for.
  40.      *
  41.      * @return array 
  42.      */
  43.     public function register()
  44.     {
  45.         return array(T_STRING);
  46.  
  47.     }//end register()
  48.  
  49.  
  50.     /**
  51.      * Processes this test, when one of its tokens is encountered.
  52.      *
  53.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  54.      * @param int                         $stackPtr  The position of the current token in
  55.      *                                                the stack passed in $tokens.
  56.      *
  57.      * @return void 
  58.      */
  59.     public function process(File $phpcsFile$stackPtr)
  60.     {
  61.         $tokens $phpcsFile->getTokens();
  62.  
  63.         // Make sure this is a function call.
  64.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  65.         if ($next === false{
  66.             // Not a function call.
  67.             return;
  68.         }
  69.  
  70.         if ($tokens[$next]['code'!== T_OPEN_PARENTHESIS{
  71.             // Not a function call.
  72.             return;
  73.         }
  74.  
  75.         $prev $phpcsFile->findPrevious(array(T_WHITESPACET_BITWISE_AND)($stackPtr - 1)nulltrue);
  76.         if ($tokens[$prev]['code'=== T_FUNCTION{
  77.             // Function declaration, not a function call.
  78.             return;
  79.         }
  80.  
  81.         if ($tokens[$prev]['code'=== T_NS_SEPARATOR{
  82.             // Namespaced class/function, not an inbuilt function.
  83.             return;
  84.         }
  85.  
  86.         if ($tokens[$prev]['code'=== T_NEW{
  87.             // Object creation, not an inbuilt function.
  88.             return;
  89.         }
  90.  
  91.         if ($tokens[$prev]['code'=== T_OBJECT_OPERATOR{
  92.             // Not an inbuilt function.
  93.             return;
  94.         }
  95.  
  96.         if ($tokens[$prev]['code'=== T_DOUBLE_COLON{
  97.             // Not an inbuilt function.
  98.             return;
  99.         }
  100.  
  101.         // Make sure it is an inbuilt PHP function.
  102.         // PHP_CodeSniffer can possibly include user defined functions
  103.         // through the use of vendor/autoload.php.
  104.         $content $tokens[$stackPtr]['content'];
  105.         if (isset($this->builtInFunctions[strtolower($content)]=== false{
  106.             return;
  107.         }
  108.  
  109.         if ($content !== strtolower($content)) {
  110.             $error 'Calls to inbuilt PHP functions must be lowercase; expected "%s" but found "%s"';
  111.             $data  = array(
  112.                       strtolower($content),
  113.                       $content,
  114.                      );
  115.  
  116.             $fix $phpcsFile->addFixableError($error$stackPtr'CallUppercase'$data);
  117.             if ($fix === true{
  118.                 $phpcsFile->fixer->replaceToken($stackPtrstrtolower($content));
  119.             }
  120.         }
  121.  
  122.     }//end process()
  123.  
  124.  
  125. }//end class

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