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

Breadcrumb

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

class DependenciesArraySniff

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

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

Hierarchy

  • class \Drupal\Sniffs\InfoFiles\DependenciesArraySniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DependenciesArraySniff

File

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

Namespace

Drupal\Sniffs\InfoFiles
View source
class DependenciesArraySniff implements Sniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_INLINE_HTML,
        ];
    }
    
    //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 current token in the stack passed in $tokens.
     *
     * @return int
     */
    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;
    }
    
    //end process()

}

Members

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