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.         return array(T_STRING);
  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.         // Skip tokens that are the names of functions or classes
  46.         // within their definitions. For example:
  47.         // function myFunction...
  48.         // "myFunction" is T_STRING but we should skip because it is not a
  49.         // function or method *call*.
  50.         $functionName    $stackPtr;
  51.         $ignoreTokens    = Tokens::$emptyTokens;
  52.         $ignoreTokens[]  = T_BITWISE_AND;
  53.         $functionKeyword $phpcsFile->findPrevious($ignoreTokens($stackPtr - 1)nulltrue);
  54.         if ($tokens[$functionKeyword]['code'=== T_FUNCTION || $tokens[$functionKeyword]['code'=== T_CLASS{
  55.             return;
  56.         }
  57.  
  58.         // If the next non-whitespace token after the function or method call
  59.         // is not an opening parenthesis then it cant really be a *call*.
  60.         $openBracket $phpcsFile->findNext(Tokens::$emptyTokens($functionName + 1)nulltrue);
  61.         if ($tokens[$openBracket]['code'!== T_OPEN_PARENTHESIS{
  62.             return;
  63.         }
  64.  
  65.         if (isset($tokens[$openBracket]['parenthesis_closer']=== false{
  66.             return;
  67.         }
  68.  
  69.         $closeBracket  $tokens[$openBracket]['parenthesis_closer'];
  70.         $nextSeparator $openBracket;
  71.  
  72.         $find = array(
  73.                  T_COMMA,
  74.                  T_VARIABLE,
  75.                  T_CLOSURE,
  76.                  T_OPEN_SHORT_ARRAY,
  77.                 );
  78.  
  79.         while (($nextSeparator $phpcsFile->findNext($find($nextSeparator + 1)$closeBracket)) !== false{
  80.             if ($tokens[$nextSeparator]['code'=== T_CLOSURE{
  81.                 // Skip closures.
  82.                 $nextSeparator $tokens[$nextSeparator]['scope_closer'];
  83.                 continue;
  84.             else if ($tokens[$nextSeparator]['code'=== T_OPEN_SHORT_ARRAY{
  85.                 // Skips arrays using short notation.
  86.                 $nextSeparator $tokens[$nextSeparator]['bracket_closer'];
  87.                 continue;
  88.             }
  89.  
  90.             // Make sure the comma or variable belongs directly to this function call,
  91.             // and is not inside a nested function call or array.
  92.             $brackets    $tokens[$nextSeparator]['nested_parenthesis'];
  93.             $lastBracket array_pop($brackets);
  94.             if ($lastBracket !== $closeBracket{
  95.                 continue;
  96.             }
  97.  
  98.             if ($tokens[$nextSeparator]['code'=== T_COMMA{
  99.                 if ($tokens[($nextSeparator - 1)]['code'=== T_WHITESPACE{
  100.                     $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($nextSeparator - 2)nulltrue);
  101.                     if (isset(Tokens::$heredocTokens[$tokens[$prev]['code']]=== false{
  102.                         $error 'Space found before comma in function call';
  103.                         $fix   $phpcsFile->addFixableError($error$nextSeparator'SpaceBeforeComma');
  104.                         if ($fix === true{
  105.                             $phpcsFile->fixer->replaceToken(($nextSeparator - 1)'');
  106.                         }
  107.                     }
  108.                 }
  109.  
  110.                 if ($tokens[($nextSeparator + 1)]['code'!== T_WHITESPACE{
  111.                     $error 'No space found after comma in function call';
  112.                     $fix   $phpcsFile->addFixableError($error$nextSeparator'NoSpaceAfterComma');
  113.                     if ($fix === true{
  114.                         $phpcsFile->fixer->addContent($nextSeparator' ');
  115.                     }
  116.                 else {
  117.                     // If there is a newline in the space, then they must be formatting
  118.                     // each argument on a newline, which is valid, so ignore it.
  119.                     $next $phpcsFile->findNext(Tokens::$emptyTokens($nextSeparator + 1)nulltrue);
  120.                     if ($tokens[$next]['line'=== $tokens[$nextSeparator]['line']{
  121.                         $space strlen($tokens[($nextSeparator + 1)]['content']);
  122.                         if ($space > 1{
  123.                             $error 'Expected 1 space after comma in function call; %s found';
  124.                             $data  = array($space);
  125.                             $fix   $phpcsFile->addFixableError($error$nextSeparator'TooMuchSpaceAfterComma'$data);
  126.                             if ($fix === true{
  127.                                 $phpcsFile->fixer->replaceToken(($nextSeparator + 1)' ');
  128.                             }
  129.                         }
  130.                     }
  131.                 }//end if
  132.             else {
  133.                 // Token is a variable.
  134.                 $nextToken $phpcsFile->findNext(Tokens::$emptyTokens($nextSeparator + 1)$closeBrackettrue);
  135.                 if ($nextToken !== false{
  136.                     if ($tokens[$nextToken]['code'=== T_EQUAL{
  137.                         if (($tokens[($nextToken - 1)]['code']!== T_WHITESPACE{
  138.                             $error 'Expected 1 space before = sign of default value';
  139.                             $fix   $phpcsFile->addFixableError($error$nextToken'NoSpaceBeforeEquals');
  140.                             if ($fix === true{
  141.                                 $phpcsFile->fixer->addContentBefore($nextToken' ');
  142.                             }
  143.                         }
  144.  
  145.                         if ($tokens[($nextToken + 1)]['code'!== T_WHITESPACE{
  146.                             $error 'Expected 1 space after = sign of default value';
  147.                             $fix   $phpcsFile->addFixableError($error$nextToken'NoSpaceAfterEquals');
  148.                             if ($fix === true{
  149.                                 $phpcsFile->fixer->addContent($nextToken' ');
  150.                             }
  151.                         }
  152.                     }
  153.                 }
  154.             }//end if
  155.         }//end while
  156.  
  157.     }//end process()
  158.  
  159.  
  160. }//end class

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