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

Source for file MultiLineFunctionDeclarationSniff.php

Documentation is available at MultiLineFunctionDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Ensure single and multi-line function declarations are defined 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\Squiz\Sniffs\Functions;
  11.  
  12. use PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionDeclarationSniff as PEARFunctionDeclarationSniff;
  13. use PHP_CodeSniffer\Util\Tokens;
  14.  
  15. class MultiLineFunctionDeclarationSniff extends PEARFunctionDeclarationSniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  28.  
  29.     /**
  30.      * Determine if this is a multi-line function declaration.
  31.      *
  32.      * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
  33.      * @param int                         $stackPtr    The position of the current token
  34.      *                                                  in the stack passed in $tokens.
  35.      * @param int                         $openBracket The position of the opening bracket
  36.      *                                                  in the stack passed in $tokens.
  37.      * @param array                       $tokens      The stack of tokens that make up
  38.      *                                                  the file.
  39.      *
  40.      * @return void 
  41.      */
  42.     public function isMultiLineDeclaration($phpcsFile$stackPtr$openBracket$tokens)
  43.     {
  44.         $bracketsToCheck = array($stackPtr => $openBracket);
  45.  
  46.         // Closures may use the USE keyword and so be multi-line in this way.
  47.         if ($tokens[$stackPtr]['code'=== T_CLOSURE{
  48.             $use $phpcsFile->findNext(T_USE($tokens[$openBracket]['parenthesis_closer'+ 1)$tokens[$stackPtr]['scope_opener']);
  49.             if ($use !== false{
  50.                 $open $phpcsFile->findNext(T_OPEN_PARENTHESIS($use + 1));
  51.                 if ($open !== false{
  52.                     $bracketsToCheck[$use$open;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         foreach ($bracketsToCheck as $stackPtr => $openBracket{
  58.             // If the first argument is on a new line, this is a multi-line
  59.             // function declaration, even if there is only one argument.
  60.             $next $phpcsFile->findNext(Tokens::$emptyTokens($openBracket + 1)nulltrue);
  61.             if ($tokens[$next]['line'!== $tokens[$stackPtr]['line']{
  62.                 return true;
  63.             }
  64.  
  65.             $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  66.  
  67.             $end $phpcsFile->findEndOfStatement($openBracket + 1);
  68.             while ($tokens[$end]['code'=== T_COMMA{
  69.                 // If the next bit of code is not on the same line, this is a
  70.                 // multi-line function declaration.
  71.                 $next $phpcsFile->findNext(Tokens::$emptyTokens($end + 1)$closeBrackettrue);
  72.                 if ($next === false{
  73.                     continue(2);
  74.                 }
  75.  
  76.                 if ($tokens[$next]['line'!== $tokens[$end]['line']{
  77.                     return true;
  78.                 }
  79.  
  80.                 $end $phpcsFile->findEndOfStatement($next);
  81.             }
  82.  
  83.             // We've reached the last argument, so see if the next content
  84.             // (should be the close bracket) is also on the same line.
  85.             $next $phpcsFile->findNext(Tokens::$emptyTokens($end + 1)$closeBrackettrue);
  86.             if ($next !== false && $tokens[$next]['line'!== $tokens[$end]['line']{
  87.                 return true;
  88.             }
  89.         }//end foreach
  90.  
  91.         return false;
  92.  
  93.     }//end isMultiLineDeclaration()
  94.  
  95.  
  96.     /**
  97.      * Processes multi-line declarations.
  98.      *
  99.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  100.      * @param int                         $stackPtr  The position of the current token
  101.      *                                                in the stack passed in $tokens.
  102.      * @param array                       $tokens    The stack of tokens that make up
  103.      *                                                the file.
  104.      *
  105.      * @return void 
  106.      */
  107.     public function processMultiLineDeclaration($phpcsFile$stackPtr$tokens)
  108.     {
  109.         // We do everything the parent sniff does, and a bit more.
  110.         parent::processMultiLineDeclaration($phpcsFile$stackPtr$tokens);
  111.  
  112.         $openBracket $tokens[$stackPtr]['parenthesis_opener'];
  113.         $this->processBracket($phpcsFile$openBracket$tokens'function');
  114.  
  115.         if ($tokens[$stackPtr]['code'!== T_CLOSURE{
  116.             return;
  117.         }
  118.  
  119.         $use $phpcsFile->findNext(T_USE($tokens[$stackPtr]['parenthesis_closer'+ 1)$tokens[$stackPtr]['scope_opener']);
  120.         if ($use === false{
  121.             return;
  122.         }
  123.  
  124.         $openBracket $phpcsFile->findNext(T_OPEN_PARENTHESIS($use + 1)null);
  125.         $this->processBracket($phpcsFile$openBracket$tokens'use');
  126.  
  127.         // Also check spacing.
  128.         if ($tokens[($use - 1)]['code'=== T_WHITESPACE{
  129.             $gap strlen($tokens[($use - 1)]['content']);
  130.         else {
  131.             $gap = 0;
  132.         }
  133.  
  134.     }//end processMultiLineDeclaration()
  135.  
  136.  
  137.     /**
  138.      * Processes the contents of a single set of brackets.
  139.      *
  140.      * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
  141.      * @param int                         $openBracket The position of the open bracket
  142.      *                                                  in the stack passed in $tokens.
  143.      * @param array                       $tokens      The stack of tokens that make up
  144.      *                                                  the file.
  145.      * @param string                      $type        The type of the token the brackets
  146.      *                                                  belong to (function or use).
  147.      *
  148.      * @return void 
  149.      */
  150.     public function processBracket($phpcsFile$openBracket$tokens$type='function')
  151.     {
  152.         $errorPrefix '';
  153.         if ($type === 'use'{
  154.             $errorPrefix 'Use';
  155.         }
  156.  
  157.         $closeBracket $tokens[$openBracket]['parenthesis_closer'];
  158.  
  159.         // The open bracket should be the last thing on the line.
  160.         if ($tokens[$openBracket]['line'!== $tokens[$closeBracket]['line']{
  161.             $next $phpcsFile->findNext(Tokens::$emptyTokens($openBracket + 1)nulltrue);
  162.             if ($tokens[$next]['line'!== ($tokens[$openBracket]['line'+ 1)) {
  163.                 $error 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
  164.                 $fix   $phpcsFile->addFixableError($error$next$errorPrefix.'FirstParamSpacing');
  165.                 if ($fix === true{
  166.                     $phpcsFile->fixer->addNewline($openBracket);
  167.                 }
  168.             }
  169.         }
  170.  
  171.         // Each line between the brackets should contain a single parameter.
  172.         $lastComma = null;
  173.         for ($i ($openBracket + 1)$i $closeBracket$i++{
  174.             // Skip brackets, like arrays, as they can contain commas.
  175.             if (isset($tokens[$i]['bracket_opener']=== true{
  176.                 $i $tokens[$i]['bracket_closer'];
  177.                 continue;
  178.             }
  179.  
  180.             if (isset($tokens[$i]['parenthesis_opener']=== true{
  181.                 $i $tokens[$i]['parenthesis_closer'];
  182.                 continue;
  183.             }
  184.  
  185.             if ($tokens[$i]['code'!== T_COMMA{
  186.                 continue;
  187.             }
  188.  
  189.             $next $phpcsFile->findNext(T_WHITESPACE($i + 1)nulltrue);
  190.             if ($tokens[$next]['line'=== $tokens[$i]['line']{
  191.                 $error 'Multi-line '.$type.' declarations must define one parameter per line';
  192.                 $fix   $phpcsFile->addFixableError($error$next$errorPrefix.'OneParamPerLine');
  193.                 if ($fix === true{
  194.                     $phpcsFile->fixer->addNewline($i);
  195.                 }
  196.             }
  197.         }//end for
  198.  
  199.     }//end processBracket()
  200.  
  201.  
  202. }//end class

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