You are here

class MethodScopeSniff in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php \Drupal\Sniffs\Scope\MethodScopeSniff
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php \Drupal\Sniffs\Scope\MethodScopeSniff

Verifies that class/interface/trait methods have scope modifiers.

Laregely copied from \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff to work on traits and have a fixer.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \Drupal\Sniffs\Scope\MethodScopeSniff extends \PHP_CodeSniffer\Sniffs\AbstractScopeSniff

Expanded class hierarchy of MethodScopeSniff

File

coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php, line 27

Namespace

Drupal\Sniffs\Scope
View source
class MethodScopeSniff extends AbstractScopeSniff {

  /**
   * Constructs a
   * \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff.
   */
  public function __construct() {
    parent::__construct(array(
      T_CLASS,
      T_INTERFACE,
      T_TRAIT,
    ), array(
      T_FUNCTION,
    ));
  }

  //end __construct()

  /**
   * Processes the function tokens within the class.
   *
   * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
   * @param int                         $stackPtr  The position where the token was found.
   * @param int                         $currScope The current scope opener token.
   *
   * @return void
   */
  protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) {
    $tokens = $phpcsFile
      ->getTokens();
    $methodName = $phpcsFile
      ->getDeclarationName($stackPtr);
    if ($methodName === null) {

      // Ignore closures.
      return;
    }
    if ($phpcsFile
      ->hasCondition($stackPtr, T_FUNCTION) === true) {

      // Ignore nested functions.
      return;
    }
    $modifier = null;
    for ($i = $stackPtr - 1; $i > 0; $i--) {
      if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
        break;
      }
      else {
        if (isset(Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
          $modifier = $i;
          break;
        }
      }
    }
    if ($modifier === null) {
      $error = 'Visibility must be declared on method "%s"';
      $data = array(
        $methodName,
      );
      $fix = $phpcsFile
        ->addFixableError($error, $stackPtr, 'Missing', $data);
      if ($fix === true) {

        // No scope modifier means the method is public in PHP, fix that
        // to be explicitly public.
        $phpcsFile->fixer
          ->addContentBefore($stackPtr, 'public ');
      }
    }
  }

  //end processTokenWithinScope()

  /**
   * Processes a token that is found outside the scope that this test is
   * listening to.
   *
   * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
   * @param int                         $stackPtr  The position in the stack where this
   *                                               token was found.
   *
   * @return void
   */
  protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MethodScopeSniff::processTokenOutsideScope protected function Processes a token that is found outside the scope that this test is listening to.
MethodScopeSniff::processTokenWithinScope protected function Processes the function tokens within the class.
MethodScopeSniff::__construct public function Constructs a \PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff.