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

Breadcrumb

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

function NamespacedDependencySniff::process

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

int|void

Overrides Sniff::process

File

vendor/drupal/coder/coder_sniffer/DrupalPractice/Sniffs/InfoFiles/NamespacedDependencySniff.php, line 50

Class

NamespacedDependencySniff
Checks that all declared dependencies are namespaced.

Namespace

DrupalPractice\Sniffs\InfoFiles

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $fileExtension = strtolower(substr($phpcsFile->getFilename(), -9));
    if ($fileExtension !== '.info.yml') {
        return $phpcsFile->numTokens + 1;
    }
    $contents = file_get_contents($phpcsFile->getFilename());
    try {
        $info = Yaml::parse($contents);
        // Themes are allowed to have not namespaced dependencies, see
        // https://www.drupal.org/project/drupal/issues/474684.
        if (isset($info['type']) === true && $info['type'] === 'theme') {
            return $phpcsFile->numTokens + 1;
        }
    } catch (ParseException $e) {
        // If the YAML is invalid we ignore this file.
        return $phpcsFile->numTokens + 1;
    }
    if (preg_match('/^dependencies:/', $tokens[$stackPtr]['content']) === 0) {
        return;
    }
    $nextLine = $stackPtr + 1;
    while (isset($tokens[$nextLine]) === true) {
        // Dependency line without namespace.
        if (preg_match('/^[\\s]+- [^:]+[\\s]*$/', $tokens[$nextLine]['content']) === 1) {
            $error = 'All dependencies must be prefixed with the project name, for example "drupal:"';
            $phpcsFile->addWarning($error, $nextLine, 'NonNamespaced');
        }
        else {
            if (preg_match('/^[\\s]+- [^:]+:[^:]+[\\s]*$/', $tokens[$nextLine]['content']) === 0 && preg_match('/^[\\s]*#.*$/', $tokens[$nextLine]['content']) === 0) {
                // Not a dependency line with namespace or comment - stop.
                return $nextLine;
            }
        }
        $nextLine++;
    }
}
RSS feed
Powered by Drupal