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

Source for file ValidDefaultValueSniff.php

Documentation is available at ValidDefaultValueSniff.php

  1. <?php
  2. /**
  3.  * Ensures function params with default values are at the end of the declaration.
  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\PEAR\Sniffs\Functions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ValidDefaultValueSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return int[] 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(T_FUNCTION);
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  36.      * @param int                  $stackPtr  The position of the current token in the
  37.      *                                         stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         $argStart $tokens[$stackPtr]['parenthesis_opener'];
  46.         $argEnd   $tokens[$stackPtr]['parenthesis_closer'];
  47.  
  48.         // Flag for when we have found a default in our arg list.
  49.         // If there is a value without a default after this, it is an error.
  50.         $defaultFound = false;
  51.  
  52.         $nextArg $argStart;
  53.         while (($nextArg $phpcsFile->findNext(T_VARIABLE($nextArg + 1)$argEnd)) !== false{
  54.             if ($tokens[($nextArg - 1)]['code'=== T_ELLIPSIS{
  55.                 continue;
  56.             }
  57.  
  58.             $argHasDefault = self::argHasDefault($phpcsFile$nextArg);
  59.             if ($argHasDefault === false && $defaultFound === true{
  60.                 $error 'Arguments with default values must be at the end of the argument list';
  61.                 $phpcsFile->addError($error$nextArg'NotAtEnd');
  62.                 return;
  63.             }
  64.  
  65.             if ($argHasDefault === true{
  66.                 $defaultFound = true;
  67.             }
  68.         }
  69.  
  70.     }//end process()
  71.  
  72.  
  73.     /**
  74.      * Returns true if the passed argument has a default value.
  75.      *
  76.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  77.      * @param int                  $argPtr    The position of the argument
  78.      *                                         in the stack.
  79.      *
  80.      * @return bool 
  81.      */
  82.     private static function argHasDefault($phpcsFile$argPtr)
  83.     {
  84.         $tokens    $phpcsFile->getTokens();
  85.         $nextToken $phpcsFile->findNext(Tokens::$emptyTokens($argPtr + 1)nulltrue);
  86.         if ($tokens[$nextToken]['code'!== T_EQUAL{
  87.             return false;
  88.         }
  89.  
  90.         return true;
  91.  
  92.     }//end argHasDefault()
  93.  
  94.  
  95. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.