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

Source for file ReturnFunctionValueSniff.php

Documentation is available at ReturnFunctionValueSniff.php

  1. <?php
  2. /**
  3.  * Warns when function values are returned directly.
  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\MySource\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ReturnFunctionValueSniff 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_RETURN);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, 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.         $functionName $phpcsFile->findNext(T_STRING($stackPtr + 1)nullfalsenulltrue);
  45.  
  46.         while ($functionName !== false{
  47.             // Check if this is really a function.
  48.             $bracket $phpcsFile->findNext(T_WHITESPACE($functionName + 1)nulltrue);
  49.             if ($tokens[$bracket]['code'!== T_OPEN_PARENTHESIS{
  50.                 // Not a function call.
  51.                 $functionName $phpcsFile->findNext(T_STRING($functionName + 1)nullfalsenulltrue);
  52.                 continue;
  53.             }
  54.  
  55.             $error 'The result of a function call should be assigned to a variable before being returned';
  56.             $phpcsFile->addWarning($error$stackPtr'NotAssigned');
  57.             break;
  58.         }
  59.  
  60.     }//end process()
  61.  
  62.  
  63. }//end class

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