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

Source for file InnerFunctionsSniff.php

Documentation is available at InnerFunctionsSniff.php

  1. <?php
  2. /**
  3.  * Ensures that functions within functions are never 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\Squiz\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class InnerFunctionsSniff 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_FUNCTION);
  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 the
  36.      *                                                stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         $function $phpcsFile->getCondition($stackPtrT_FUNCTION);
  45.         if ($function === false{
  46.             // Not a nested function.
  47.             return;
  48.         }
  49.  
  50.         $class $phpcsFile->getCondition($stackPtrT_ANON_CLASS);
  51.         if ($class !== false && $class $function{
  52.             // Ignore methods in anon classes.
  53.             return;
  54.         }
  55.  
  56.         $prev $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  57.         if ($tokens[$prev]['code'=== T_EQUAL{
  58.             // Ignore closures.
  59.             return;
  60.         }
  61.  
  62.         $error 'The use of inner functions is forbidden';
  63.         $phpcsFile->addError($error$stackPtr'NotAllowed');
  64.  
  65.     }//end process()
  66.  
  67.  
  68. }//end class

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