Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ClassNameSniff.php

class ClassNameSniff

Checks that classes without namespaces are properly prefixed with the module name.

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

Hierarchy

  • class \DrupalPractice\Sniffs\General\ClassNameSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ClassNameSniff

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/General/ClassNameSniff.php, line 24

Namespace

DrupalPractice\Sniffs\General
View source
class ClassNameSniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_CLASS,
            T_INTERFACE,
        ];
    }
    
    //end register()
    
    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the function
     *                                               name in the stack.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr) {
        // If there is a PHP 5.3 namespace declaration in the file we return
        // immediately as classes can be named arbitrary within a namespace.
        $namespace = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr - 1);
        if ($namespace !== false) {
            return;
        }
        $moduleName = Project::getName($phpcsFile);
        if ($moduleName === false) {
            return;
        }
        $tokens = $phpcsFile->getTokens();
        $className = $phpcsFile->findNext(T_STRING, $stackPtr);
        $name = trim($tokens[$className]['content']);
        // Underscores are omitted in class names. Also convert all characters
        // to lower case to compare them later.
        $classPrefix = strtolower(str_replace('_', '', $moduleName));
        // Views classes might have underscores in the name, which is also fine.
        $viewsPrefix = strtolower($moduleName);
        $name = strtolower($name);
        if (strpos($name, $classPrefix) !== 0 && strpos($name, $viewsPrefix) !== 0) {
            $warning = '%s name must be prefixed with the project name "%s"';
            $nameParts = explode('_', $moduleName);
            $camelName = '';
            foreach ($nameParts as &$part) {
                $camelName .= ucfirst($part);
            }
            $errorData = [
                ucfirst($tokens[$stackPtr]['content']),
                $camelName,
            ];
            $phpcsFile->addWarning($warning, $className, 'ClassPrefix', $errorData);
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ClassNameSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process
ClassNameSniff::register public function Returns an array of tokens this test wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal