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

Source for file MethodScopeSniff.php

Documentation is available at MethodScopeSniff.php

  1. <?php
  2. /**
  3.  * Verifies that class methods have scope modifiers.
  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\Scope;
  11.  
  12. use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class MethodScopeSniff 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_INTERFACET_TRAIT)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 ($phpcsFile->hasCondition($stackPtrT_FUNCTION=== true{
  50.             // Ignore nested functions.
  51.             return;
  52.         }
  53.  
  54.         $modifier = null;
  55.         for ($i ($stackPtr - 1)$i > 0; $i--{
  56.             if ($tokens[$i]['line'$tokens[$stackPtr]['line']{
  57.                 break;
  58.             else if (isset(Tokens::$scopeModifiers[$tokens[$i]['code']]=== true{
  59.                 $modifier $i;
  60.                 break;
  61.             }
  62.         }
  63.  
  64.         if ($modifier === null{
  65.             $error 'Visibility must be declared on method "%s"';
  66.             $data  = array($methodName);
  67.             $phpcsFile->addError($error$stackPtr'Missing'$data);
  68.         }
  69.  
  70.     }//end processTokenWithinScope()
  71.  
  72.  
  73.     /**
  74.      * Processes a token that is found within the scope that this test is
  75.      * listening to.
  76.      *
  77.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
  78.      * @param int                         $stackPtr  The position in the stack where this
  79.      *                                                token was found.
  80.      *
  81.      * @return void 
  82.      */
  83.     protected function processTokenOutsideScope(File $phpcsFile$stackPtr)
  84.     {
  85.  
  86.     }//end processTokenOutsideScope()
  87.  
  88.  
  89. }//end class

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