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

Breadcrumb

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

function DependenciesArraySniff::process

Processes this test when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

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

Return value

int

Overrides Sniff::process

File

vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/InfoFiles/DependenciesArraySniff.php, line 47

Class

DependenciesArraySniff
Dependencies should be an array in .info.yml files.

Namespace

Drupal\Sniffs\InfoFiles

Code

public function process(File $phpcsFile, $stackPtr) {
    // Only run this sniff on .info.yml files.
    if (strtolower(substr($phpcsFile->getFilename(), -9)) !== '.info.yml') {
        return $phpcsFile->numTokens + 1;
    }
    try {
        $info = Yaml::parse(file_get_contents($phpcsFile->getFilename()));
    } catch (ParseException $e) {
        // If the YAML is invalid we ignore this file.
        return $phpcsFile->numTokens + 1;
    }
    if (isset($info['dependencies']) === true && is_array($info['dependencies']) === false) {
        // The $stackPtr will always indicate line 1, but we can get the actual line by
        // searching $tokens to find the dependencies item.
        $tokens = $phpcsFile->getTokens();
        // $tokens cannot be empty at this point, but PHPStan 10.1.4 does not know this and gives the error
        // "Variable $key might not be defined". So initialize it here.
        $key = $stackPtr;
        foreach ($tokens as $key => $token) {
            if (preg_match('/dependencies\\s*\\:/', $token['content']) === 1) {
                break;
            }
        }
        $error = '"dependencies" should be an array in the info yaml file';
        $phpcsFile->addError($error, $key, 'Dependencies');
    }
    return $phpcsFile->numTokens + 1;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal