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(
  28.                 T_FUNCTION,
  29.                 T_CLOSURE,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this test, when one of its tokens is encountered.
  37.      *
  38.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  39.      * @param int                         $stackPtr  The position of the current token in the
  40.      *                                                stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         $argStart $tokens[$stackPtr]['parenthesis_opener'];
  49.         $argEnd   $tokens[$stackPtr]['parenthesis_closer'];
  50.  
  51.         // Flag for when we have found a default in our arg list.
  52.         // If there is a value without a default after this, it is an error.
  53.         $defaultFound = false;
  54.  
  55.         $params $phpcsFile->getMethodParameters($stackPtr);
  56.         foreach ($params as $param{
  57.             if ($param['variable_length'=== true{
  58.                 continue;
  59.             }
  60.  
  61.             if (array_key_exists('default'$param=== true{
  62.                 $defaultFound = true;
  63.                 // Check if the arg is type hinted and using NULL for the default.
  64.                 // This does not make the argument optional - it just allows NULL
  65.                 // to be passed in.
  66.                 if ($param['type_hint'!== '' && strtolower($param['default']=== 'null'{
  67.                     $defaultFound = false;
  68.                 }
  69.  
  70.                 continue;
  71.             }
  72.  
  73.             if ($defaultFound === true{
  74.                 $error 'Arguments with default values must be at the end of the argument list';
  75.                 $phpcsFile->addError($error$param['token']'NotAtEnd');
  76.                 return;
  77.             }
  78.         }//end foreach
  79.  
  80.     }//end process()
  81.  
  82.  
  83. }//end class

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