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

Source for file FunctionCallArgumentSpacingSniff.php

Documentation is available at FunctionCallArgumentSpacingSniff.php

  1. <?php
  2. /**
  3.  * Checks that calls to methods and functions are spaced correctly.
  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\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 FunctionCallArgumentSpacingSniff 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.         $tokens = Tokens::$functionNameTokens;
  28.  
  29.         $tokens[= T_VARIABLE;
  30.         $tokens[T_CLOSE_CURLY_BRACKET;
  31.         $tokens[T_CLOSE_PARENTHESIS;
  32.  
  33.         return $tokens;
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  42.      * @param int                         $stackPtr  The position of the current token in the
  43.      *                                                stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         // Skip tokens that are the names of functions or classes
  52.         // within their definitions. For example:
  53.         // function myFunction...
  54.         // "myFunction" is T_STRING but we should skip because it is not a
  55.         // function or method *call*.
  56.         $functionName    $stackPtr;
  57.         $ignoreTokens    = Tokens::$emptyTokens;
  58.         $ignoreTokens[]  = T_BITWISE_AND;
  59.         $functionKeyword $phpcsFile->findPrevious($ignoreTokens($stackPtr - 1)nulltrue);
  60.         if ($tokens[$functionKeyword]['code'=== T_FUNCTION || $tokens[$functionKeyword]['code'=== T_CLASS{
  61.             return;
  62.         }
  63.  
  64.         if ($tokens[$stackPtr]['code'=== T_CLOSE_CURLY_BRACKET
  65.             && isset($tokens[$stackPtr]['scope_condition']=== true
  66.         {
  67.             // Not a function call.
  68.             return;
  69.         }
  70.  
  71.         // If the next non-whitespace token after the function or method call
  72.         // is not an opening parenthesis then it cant really be a *call*.
  73.         $openBracket $phpcsFile->findNext(Tokens::$emptyTokens($functionName + 1)nulltrue);
  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.         $nextSeparator $openBracket;
  84.  
  85.         $find = array(
  86.                  T_COMMA,
  87.                  T_VARIABLE,
  88.                  T_CLOSURE,
  89.                  T_OPEN_SHORT_ARRAY,
  90.                 );
  91.  
  92.         while (($nextSeparator $phpcsFile->findNext($find($nextSeparator + 1)$closeBracket)) !== false{
  93.             if ($tokens[$nextSeparator]['code'=== T_CLOSURE{
  94.                 // Skip closures.
  95.                 $nextSeparator $tokens[$nextSeparator]['scope_closer'];
  96.                 continue;
  97.             else if ($tokens[$nextSeparator]['code'=== T_OPEN_SHORT_ARRAY{
  98.                 // Skips arrays using short notation.
  99.                 $nextSeparator $tokens[$nextSeparator]['bracket_closer'];
  100.                 continue;
  101.             }
  102.  
  103.             // Make sure the comma or variable belongs directly to this function call,
  104.             // and is not inside a nested function call or array.
  105.             $brackets    $tokens[$nextSeparator]['nested_parenthesis'];
  106.             $lastBracket array_pop($brackets);
  107.             if ($lastBracket !== $closeBracket{
  108.                 continue;
  109.             }
  110.  
  111.             if ($tokens[$nextSeparator]['code'=== T_COMMA{
  112.                 if ($tokens[($nextSeparator - 1)]['code'=== T_WHITESPACE{
  113.                     $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($nextSeparator - 2)nulltrue);
  114.                     if (isset(Tokens::$heredocTokens[$tokens[$prev]['code']]=== false{
  115.                         $error 'Space found before comma in function call';
  116.                         $fix   $phpcsFile->addFixableError($error$nextSeparator'SpaceBeforeComma');
  117.                         if ($fix === true{
  118.                             $phpcsFile->fixer->replaceToken(($nextSeparator - 1)'');
  119.                         }
  120.                     }
  121.                 }
  122.  
  123.                 if ($tokens[($nextSeparator + 1)]['code'!== T_WHITESPACE{
  124.                     $error 'No space found after comma in function call';
  125.                     $fix   $phpcsFile->addFixableError($error$nextSeparator'NoSpaceAfterComma');
  126.                     if ($fix === true{
  127.                         $phpcsFile->fixer->addContent($nextSeparator' ');
  128.                     }
  129.                 else {
  130.                     // If there is a newline in the space, then they must be formatting
  131.                     // each argument on a newline, which is valid, so ignore it.
  132.                     $next $phpcsFile->findNext(Tokens::$emptyTokens($nextSeparator + 1)nulltrue);
  133.                     if ($tokens[$next]['line'=== $tokens[$nextSeparator]['line']{
  134.                         $space strlen($tokens[($nextSeparator + 1)]['content']);
  135.                         if ($space > 1{
  136.                             $error 'Expected 1 space after comma in function call; %s found';
  137.                             $data  = array($space);
  138.                             $fix   $phpcsFile->addFixableError($error$nextSeparator'TooMuchSpaceAfterComma'$data);
  139.                             if ($fix === true{
  140.                                 $phpcsFile->fixer->replaceToken(($nextSeparator + 1)' ');
  141.                             }
  142.                         }
  143.                     }
  144.                 }//end if
  145.             else {
  146.                 // Token is a variable.
  147.                 $nextToken $phpcsFile->findNext(Tokens::$emptyTokens($nextSeparator + 1)$closeBrackettrue);
  148.                 if ($nextToken !== false{
  149.                     if ($tokens[$nextToken]['code'=== T_EQUAL{
  150.                         if (($tokens[($nextToken - 1)]['code']!== T_WHITESPACE{
  151.                             $error 'Expected 1 space before = sign of default value';
  152.                             $fix   $phpcsFile->addFixableError($error$nextToken'NoSpaceBeforeEquals');
  153.                             if ($fix === true{
  154.                                 $phpcsFile->fixer->addContentBefore($nextToken' ');
  155.                             }
  156.                         }
  157.  
  158.                         if ($tokens[($nextToken + 1)]['code'!== T_WHITESPACE{
  159.                             $error 'Expected 1 space after = sign of default value';
  160.                             $fix   $phpcsFile->addFixableError($error$nextToken'NoSpaceAfterEquals');
  161.                             if ($fix === true{
  162.                                 $phpcsFile->fixer->addContent($nextToken' ');
  163.                             }
  164.                         }
  165.                     }
  166.                 }
  167.             }//end if
  168.         }//end while
  169.  
  170.     }//end process()
  171.  
  172.  
  173. }//end class

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