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

Breadcrumb

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

class ClosureLinterSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\Debug\ClosureLinterSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ClosureLinterSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Debug/ClosureLinterSniff.php, line 19

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\Debug
View source
class ClosureLinterSniff implements Sniff {
    
    /**
     * A list of error codes that should show errors.
     *
     * All other error codes will show warnings.
     *
     * @var array
     */
    public $errorCodes = [];
    
    /**
     * A list of error codes to ignore.
     *
     * @var array
     */
    public $ignoreCodes = [];
    
    /**
     * A list of tokenizers this sniff supports.
     *
     * @var array
     */
    public $supportedTokenizers = [
        'JS',
    ];
    
    /**
     * Returns the token types that this sniff is interested in.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_OPEN_TAG,
        ];
    }
    
    //end register()
    
    /**
     * Processes the tokens that this sniff is interested in.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
     * @param int                         $stackPtr  The position in the stack where
     *                                               the token was found.
     *
     * @return int
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If jslint.js could not be run.
     */
    public function process(File $phpcsFile, $stackPtr) {
        $lintPath = Config::getExecutablePath('gjslint');
        if ($lintPath === null) {
            return $phpcsFile->numTokens;
        }
        $fileName = $phpcsFile->getFilename();
        $lintPath = Common::escapeshellcmd($lintPath);
        $cmd = $lintPath . ' --nosummary --notime --unix_mode ' . escapeshellarg($fileName);
        exec($cmd, $output, $retval);
        if (is_array($output) === false) {
            return $phpcsFile->numTokens;
        }
        foreach ($output as $finding) {
            $matches = [];
            $numMatches = preg_match('/^(.*):([0-9]+):\\(.*?([0-9]+)\\)(.*)$/', $finding, $matches);
            if ($numMatches === 0) {
                continue;
            }
            // Skip error codes we are ignoring.
            $code = $matches[3];
            if (in_array($code, $this->ignoreCodes) === true) {
                continue;
            }
            $line = (int) $matches[2];
            $error = trim($matches[4]);
            $message = 'gjslint says: (%s) %s';
            $data = [
                $code,
                $error,
            ];
            if (in_array($code, $this->errorCodes) === true) {
                $phpcsFile->addErrorOnLine($message, $line, 'ExternalToolError', $data);
            }
            else {
                $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool', $data);
            }
        }
        
        //end foreach
        // Ignore the rest of the file.
        return $phpcsFile->numTokens;
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ClosureLinterSniff::$errorCodes public property A list of error codes that should show errors.
ClosureLinterSniff::$ignoreCodes public property A list of error codes to ignore.
ClosureLinterSniff::$supportedTokenizers public property A list of tokenizers this sniff supports.
ClosureLinterSniff::process public function Processes the tokens that this sniff is interested in. Overrides Sniff::process
ClosureLinterSniff::register public function Returns the token types that this sniff is interested in. Overrides Sniff::register
RSS feed
Powered by Drupal