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

Source for file CallTimePassByReferenceSniff.php

Documentation is available at CallTimePassByReferenceSniff.php

  1. <?php
  2. /**
  3.  * Ensures that variables are not passed by reference when calling a function.
  4.  *
  5.  * @author    Florian Grandel <jerico.dev@gmail.com>
  6.  * @copyright 2009-2014 Florian Grandel
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Functions;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class CallTimePassByReferenceSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return array(
  28.                 T_STRING,
  29.                 T_VARIABLE,
  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
  40.      *                                                in the stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         $findTokens array_merge(
  49.             Tokens::$emptyTokens,
  50.             array(T_BITWISE_AND)
  51.         );
  52.  
  53.         $prev $phpcsFile->findPrevious($findTokens($stackPtr - 1)nulltrue);
  54.  
  55.         // Skip tokens that are the names of functions or classes
  56.         // within their definitions. For example: function myFunction...
  57.         // "myFunction" is T_STRING but we should skip because it is not a
  58.         // function or method *call*.
  59.         $prevCode $tokens[$prev]['code'];
  60.         if ($prevCode === T_FUNCTION || $prevCode === T_CLASS{
  61.             return;
  62.         }
  63.  
  64.         // If the next non-whitespace token after the function or method call
  65.         // is not an opening parenthesis then it cant really be a *call*.
  66.         $functionName $stackPtr;
  67.         $openBracket  $phpcsFile->findNext(
  68.             Tokens::$emptyTokens,
  69.             ($functionName + 1),
  70.             null,
  71.             true
  72.         );
  73.  
  74.         if ($tokens[$openBracket]['code'!== T_OPEN_PARENTHESIS{
  75.             return;
  76.         }
  77.  
  78.         if (isset($tokens[$openBracket]['parenthesis_closer']=== false{
  79.             return;
  80.         }
  81.  
  82.         $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  83.  
  84.         $nextSeparator $openBracket;
  85.         $find          = array(
  86.                           T_VARIABLE,
  87.                           T_OPEN_SHORT_ARRAY,
  88.                          );
  89.  
  90.         while (($nextSeparator $phpcsFile->findNext($find($nextSeparator + 1)$closeBracket)) !== false{
  91.             if (isset($tokens[$nextSeparator]['nested_parenthesis']=== false{
  92.                 continue;
  93.             }
  94.  
  95.             if ($tokens[$nextSeparator]['code'=== T_OPEN_SHORT_ARRAY{
  96.                 $nextSeparator $tokens[$nextSeparator]['bracket_closer'];
  97.                 continue;
  98.             }
  99.  
  100.             // Make sure the variable belongs directly to this function call
  101.             // and is not inside a nested function call or array.
  102.             $brackets    $tokens[$nextSeparator]['nested_parenthesis'];
  103.             $lastBracket array_pop($brackets);
  104.             if ($lastBracket !== $closeBracket{
  105.                 continue;
  106.             }
  107.  
  108.             // Checking this: $value = my_function(...[*]$arg...).
  109.             $tokenBefore $phpcsFile->findPrevious(
  110.                 Tokens::$emptyTokens,
  111.                 ($nextSeparator - 1),
  112.                 null,
  113.                 true
  114.             );
  115.  
  116.             if ($tokens[$tokenBefore]['code'=== T_BITWISE_AND{
  117.                 // Checking this: $value = my_function(...[*]&$arg...).
  118.                 $tokenBefore $phpcsFile->findPrevious(
  119.                     Tokens::$emptyTokens,
  120.                     ($tokenBefore - 1),
  121.                     null,
  122.                     true
  123.                 );
  124.  
  125.                 // We have to exclude all uses of T_BITWISE_AND that are not
  126.                 // references. We use a blacklist approach as we prefer false
  127.                 // positives to not identifying a pass-by-reference call at all.
  128.                 $tokenCode $tokens[$tokenBefore]['code'];
  129.                 if ($tokenCode === T_VARIABLE
  130.                     || $tokenCode === T_CLOSE_PARENTHESIS
  131.                     || $tokenCode === T_CLOSE_SQUARE_BRACKET
  132.                     || $tokenCode === T_LNUMBER
  133.                     || isset(Tokens::$assignmentTokens[$tokenCode]=== true
  134.                 {
  135.                     continue;
  136.                 }
  137.  
  138.                 // T_BITWISE_AND represents a pass-by-reference.
  139.                 $error 'Call-time pass-by-reference calls are prohibited';
  140.                 $phpcsFile->addError($error$tokenBefore'NotAllowed');
  141.             }//end if
  142.         }//end while
  143.  
  144.     }//end process()
  145.  
  146.  
  147. }//end class

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