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

Source for file MethodDeclarationSniff.php

Documentation is available at MethodDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Checks that the method declaration is correct.
  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\PSR2\Sniffs\Methods;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  13. use PHP_CodeSniffer\Util\Tokens;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class MethodDeclarationSniff extends AbstractScopeSniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
  22.      */
  23.     public function __construct()
  24.     {
  25.         parent::__construct(array(T_CLASST_INTERFACE)array(T_FUNCTION));
  26.  
  27.     }//end __construct()
  28.  
  29.  
  30.     /**
  31.      * Processes the function tokens within the class.
  32.      *
  33.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  34.      * @param int                         $stackPtr  The position where the token was found.
  35.      * @param int                         $currScope The current scope opener token.
  36.      *
  37.      * @return void 
  38.      */
  39.     protected function processTokenWithinScope(File $phpcsFile$stackPtr$currScope)
  40.     {
  41.         $tokens $phpcsFile->getTokens();
  42.  
  43.         $methodName $phpcsFile->getDeclarationName($stackPtr);
  44.         if ($methodName === null{
  45.             // Ignore closures.
  46.             return;
  47.         }
  48.  
  49.         if ($methodName[0=== '_' && isset($methodName[1]=== true && $methodName[1!== '_'{
  50.             $error 'Method name "%s" should not be prefixed with an underscore to indicate visibility';
  51.             $data  = array($methodName);
  52.             $phpcsFile->addWarning($error$stackPtr'Underscore'$data);
  53.         }
  54.  
  55.         $visibility = 0;
  56.         $static     = 0;
  57.         $abstract   = 0;
  58.         $final      = 0;
  59.  
  60.         $find   = Tokens::$methodPrefixes;
  61.         $find[= T_WHITESPACE;
  62.         $prev   $phpcsFile->findPrevious($find($stackPtr - 1)nulltrue);
  63.  
  64.         $prefix $stackPtr;
  65.         while (($prefix $phpcsFile->findPrevious(Tokens::$methodPrefixes($prefix - 1)$prev)) !== false{
  66.             switch ($tokens[$prefix]['code']{
  67.             case T_STATIC:
  68.                 $static $prefix;
  69.                 break;
  70.             case T_ABSTRACT:
  71.                 $abstract $prefix;
  72.                 break;
  73.             case T_FINAL:
  74.                 $final $prefix;
  75.                 break;
  76.             default:
  77.                 $visibility $prefix;
  78.                 break;
  79.             }
  80.         }
  81.  
  82.         $fixes = array();
  83.  
  84.         if ($visibility !== 0 && $final $visibility{
  85.             $error 'The final declaration must precede the visibility declaration';
  86.             $fix   $phpcsFile->addFixableError($error$final'FinalAfterVisibility');
  87.             if ($fix === true{
  88.                 $fixes[$final]       '';
  89.                 $fixes[($final + 1)'';
  90.                 if (isset($fixes[$visibility]=== true{
  91.                     $fixes[$visibility'final '.$fixes[$visibility];
  92.                 else {
  93.                     $fixes[$visibility'final '.$tokens[$visibility]['content'];
  94.                 }
  95.             }
  96.         }
  97.  
  98.         if ($visibility !== 0 && $abstract $visibility{
  99.             $error 'The abstract declaration must precede the visibility declaration';
  100.             $fix   $phpcsFile->addFixableError($error$abstract'AbstractAfterVisibility');
  101.             if ($fix === true{
  102.                 $fixes[$abstract]       '';
  103.                 $fixes[($abstract + 1)'';
  104.                 if (isset($fixes[$visibility]=== true{
  105.                     $fixes[$visibility'abstract '.$fixes[$visibility];
  106.                 else {
  107.                     $fixes[$visibility'abstract '.$tokens[$visibility]['content'];
  108.                 }
  109.             }
  110.         }
  111.  
  112.         if ($static !== 0 && $static $visibility{
  113.             $error 'The static declaration must come after the visibility declaration';
  114.             $fix   $phpcsFile->addFixableError($error$static'StaticBeforeVisibility');
  115.             if ($fix === true{
  116.                 $fixes[$static]       '';
  117.                 $fixes[($static + 1)'';
  118.                 if (isset($fixes[$visibility]=== true{
  119.                     $fixes[$visibility$fixes[$visibility].' static';
  120.                 else {
  121.                     $fixes[$visibility$tokens[$visibility]['content'].' static';
  122.                 }
  123.             }
  124.         }
  125.  
  126.         // Batch all the fixes together to reduce the possibility of conflicts.
  127.         if (empty($fixes=== false{
  128.             $phpcsFile->fixer->beginChangeset();
  129.             foreach ($fixes as $stackPtr => $content{
  130.                 $phpcsFile->fixer->replaceToken($stackPtr$content);
  131.             }
  132.  
  133.             $phpcsFile->fixer->endChangeset();
  134.         }
  135.  
  136.     }//end processTokenWithinScope()
  137.  
  138.  
  139.     /**
  140.      * Processes a token that is found within the scope that this test is
  141.      * listening to.
  142.      *
  143.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  144.      * @param int                         $stackPtr  The position in the stack where this
  145.      *                                                token was found.
  146.      *
  147.      * @return void 
  148.      */
  149.     protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  150.     {
  151.  
  152.     }//end processTokenOutsideScope()
  153.  
  154.  
  155. }//end class

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