You are here

ValidFunctionNameSniff.php in Coder 7.2

File

coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php
View source
<?php

/**
 * Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Serge Shirokov <bolter.fire@gmail.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
}

/**
 * Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff.
 *
 * Ensures method names are correct depending on whether they are public
 * or private, and that functions are named correctly.
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Serge Shirokov <bolter.fire@gmail.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @version   Release: 1.2.0RC3
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
class Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff {

  /**
   * Processes the tokens within the scope.
   *
   * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
   * @param int                  $stackPtr  The position where this token was
   *                                        found.
   * @param int                  $currScope The position of the current scope.
   *
   * @return void
   */
  protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) {
    $methodName = $phpcsFile
      ->getDeclarationName($stackPtr);
    if ($methodName === null) {

      // Ignore closures.
      return;
    }
    $className = $phpcsFile
      ->getDeclarationName($currScope);
    $errorData = array(
      $className . '::' . $methodName,
    );

    // Is this a magic method. i.e., is prefixed with "__" ?
    if (preg_match('|^__|', $methodName) !== 0) {
      $magicPart = strtolower(substr($methodName, 2));
      if (in_array($magicPart, $this->magicMethods) === false) {
        $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
        $phpcsFile
          ->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
      }
      return;
    }
    $methodProps = $phpcsFile
      ->getMethodProperties($stackPtr);
    $scope = $methodProps['scope'];
    $scopeSpecified = $methodProps['scope_specified'];

    // Methods should not contain underscores.
    if (strpos($methodName, '_') !== false) {
      if ($scopeSpecified === true) {
        $error = '%s method name "%s" is not in lowerCamel format, it must not contain underscores';
        $data = array(
          ucfirst($scope),
          $errorData[0],
        );
        $phpcsFile
          ->addError($error, $stackPtr, 'ScopeNotLowerCamel', $data);
      }
      else {
        $error = 'Method name "%s" is not in lowerCamel format, it must not contain underscores';
        $phpcsFile
          ->addError($error, $stackPtr, 'NotLowerCamel', $errorData);
      }
    }
  }

  //end processTokenWithinScope()

  /**
   * Processes the tokens outside the scope.
   *
   * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
   * @param int                  $stackPtr  The position where this token was
   *                                        found.
   *
   * @return void
   */
  protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {

    // Empty override, does not apply to Drupal.
  }

}

//end class

Classes

Namesort descending Description
Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff.