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

Source for file UnusedFunctionParameterSniff.php

Documentation is available at UnusedFunctionParameterSniff.php

  1. <?php
  2. /**
  3.  * Checks the for unused function parameters.
  4.  *
  5.  * This sniff checks that all function parameters are used in the function body.
  6.  * One exception is made for empty function bodies or function bodies that only
  7.  * contain comments. This could be useful for the classes that implement an
  8.  * interface that defines multiple methods but the implementation only needs some
  9.  * of them.
  10.  *
  11.  * @author    Manuel Pichler <mapi@manuel-pichler.de>
  12.  * @author    Greg Sherwood <gsherwood@squiz.net>
  13.  * @copyright 2007-2014 Manuel Pichler. All rights reserved.
  14.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  15.  */
  16.  
  17. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  18.  
  19. use PHP_CodeSniffer\Sniffs\Sniff;
  20. use PHP_CodeSniffer\Files\File;
  21. use PHP_CodeSniffer\Util\Tokens;
  22.  
  23. class UnusedFunctionParameterSniff implements Sniff
  24. {
  25.  
  26.  
  27.     /**
  28.      * Returns an array of tokens this test wants to listen for.
  29.      *
  30.      * @return array 
  31.      */
  32.     public function register()
  33.     {
  34.         return array(T_FUNCTION);
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes this test, when one of its tokens is encountered.
  41.      *
  42.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  43.      * @param int                  $stackPtr  The position of the current token
  44.      *                                         in the stack passed in $tokens.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $tokens $phpcsFile->getTokens();
  51.         $token  $tokens[$stackPtr];
  52.  
  53.         // Skip broken function declarations.
  54.         if (isset($token['scope_opener']=== false || isset($token['parenthesis_opener']=== false{
  55.             return;
  56.         }
  57.  
  58.         $params = array();
  59.         foreach ($phpcsFile->getMethodParameters($stackPtras $param{
  60.             $params[$param['name']] $stackPtr;
  61.         }
  62.  
  63.         $next = ++$token['scope_opener'];
  64.         $end  = --$token['scope_closer'];
  65.  
  66.         $foundContent = false;
  67.         $validTokens  = array(
  68.                          T_HEREDOC              => T_HEREDOC,
  69.                          T_NOWDOC               => T_NOWDOC,
  70.                          T_END_HEREDOC          => T_END_HEREDOC,
  71.                          T_END_NOWDOC           => T_END_NOWDOC,
  72.                          T_DOUBLE_QUOTED_STRING => T_DOUBLE_QUOTED_STRING,
  73.                         );
  74.         $validTokens += Tokens::$emptyTokens;
  75.  
  76.         for ($next <= $end; ++$next{
  77.             $token $tokens[$next];
  78.             $code  $token['code'];
  79.  
  80.             // Ignorable tokens.
  81.             if (isset(Tokens::$emptyTokens[$code]=== true{
  82.                 continue;
  83.             }
  84.  
  85.             if ($foundContent === false{
  86.                 // A throw statement as the first content indicates an interface method.
  87.                 if ($code === T_THROW{
  88.                     return;
  89.                 }
  90.  
  91.                 // A return statement as the first content indicates an interface method.
  92.                 if ($code === T_RETURN{
  93.                     $tmp $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  94.                     if ($tmp === false{
  95.                         return;
  96.                     }
  97.  
  98.                     // There is a return.
  99.                     if ($tokens[$tmp]['code'=== T_SEMICOLON{
  100.                         return;
  101.                     }
  102.  
  103.                     $tmp $phpcsFile->findNext(Tokens::$emptyTokens($tmp + 1)nulltrue);
  104.                     if ($tmp !== false && $tokens[$tmp]['code'=== T_SEMICOLON{
  105.                         // There is a return <token>.
  106.                         return;
  107.                     }
  108.                 }//end if
  109.             }//end if
  110.  
  111.             $foundContent = true;
  112.  
  113.             if ($code === T_VARIABLE && isset($params[$token['content']]=== true{
  114.                 unset($params[$token['content']]);
  115.             else if ($code === T_DOLLAR{
  116.                 $nextToken $phpcsFile->findNext(T_WHITESPACE($next + 1)nulltrue);
  117.                 if ($tokens[$nextToken]['code'=== T_OPEN_CURLY_BRACKET{
  118.                     $nextToken $phpcsFile->findNext(T_WHITESPACE($nextToken + 1)nulltrue);
  119.                     if ($tokens[$nextToken]['code'=== T_STRING{
  120.                         $varContent '$'.$tokens[$nextToken]['content'];
  121.                         if (isset($params[$varContent]=== true{
  122.                             unset($params[$varContent]);
  123.                         }
  124.                     }
  125.                 }
  126.             else if ($code === T_DOUBLE_QUOTED_STRING
  127.                 || $code === T_START_HEREDOC
  128.                 || $code === T_START_NOWDOC
  129.             {
  130.                 // Tokenize strings that can contain variables.
  131.                 // Make sure the string is re-joined if it occurs over multiple lines.
  132.                 $content $token['content'];
  133.                 for ($i ($next + 1)$i <= $end$i++{
  134.                     if (isset($validTokens[$tokens[$i]['code']]=== true{
  135.                         $content .= $tokens[$i]['content'];
  136.                         $next++;
  137.                     else {
  138.                         break;
  139.                     }
  140.                 }
  141.  
  142.                 $stringTokens token_get_all(sprintf('<?php %s;?>'$content));
  143.                 foreach ($stringTokens as $stringPtr => $stringToken{
  144.                     if (is_array($stringToken=== false{
  145.                         continue;
  146.                     }
  147.  
  148.                     $varContent '';
  149.                     if ($stringToken[0=== T_DOLLAR_OPEN_CURLY_BRACES{
  150.                         $varContent '$'.$stringTokens[($stringPtr + 1)][1];
  151.                     else if ($stringToken[0=== T_VARIABLE{
  152.                         $varContent $stringToken[1];
  153.                     }
  154.  
  155.                     if ($varContent !== '' && isset($params[$varContent]=== true{
  156.                         unset($params[$varContent]);
  157.                     }
  158.                 }
  159.             }//end if
  160.         }//end for
  161.  
  162.         if ($foundContent === true && count($params> 0{
  163.             foreach ($params as $paramName => $position{
  164.                 $error 'The method parameter %s is never used';
  165.                 $data  = array($paramName);
  166.                 $phpcsFile->addWarning($error$position'Found'$data);
  167.             }
  168.         }
  169.  
  170.     }//end process()
  171.  
  172.  
  173. }//end class

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