Records an error against a specific line in the file.
public addErrorOnLine ( string $error, integer $line, string $code = '', array $data = [], integer $severity ) : boolean | ||
$error | string | The error message. |
$line | integer | The line on which the error occurred. |
$code | string | A violation code unique to the sniff message. |
$data | array | Replacements for the error message. |
$severity | integer | The severity level for this error. A value of 0 will be converted into the default severity level. |
리턴 | boolean |
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { if ($this->_phpPath === null) { $this->_phpPath = PHP_CodeSniffer::getConfigData('php_path'); if ($this->_phpPath === null) { // PHP_BINARY is available in PHP 5.4+. if (defined('PHP_BINARY') === true) { $this->_phpPath = PHP_BINARY; } else { return; } } } $fileName = $phpcsFile->getFilename(); $cmd = $this->_phpPath . " -l \"{$fileName}\" 2>&1"; $output = shell_exec($cmd); $matches = array(); if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/', trim($output), $matches) === 1) { $error = trim($matches[1]); $line = (int) $matches[2]; $phpcsFile->addErrorOnLine("PHP syntax error: {$error}", $line, 'PHPSyntax'); } // Ignore the rest of the file. return $phpcsFile->numTokens + 1; }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $phpPath = PHP_CodeSniffer::getConfigData('php_path'); if ($phpPath === null) { return; } $fileName = $phpcsFile->getFilename(); $cmd = "{$phpPath} -l \"{$fileName}\" 2>&1"; $output = shell_exec($cmd); $matches = array(); if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/', $output, $matches) === 1) { $error = trim($matches[1]); $line = (int) $matches[2]; $phpcsFile->addErrorOnLine("PHP syntax error: {$error}", $line, 'PHPSyntax'); } // Ignore the rest of the file. return $phpcsFile->numTokens + 1; }
/** * Processes the tokens that this sniff is interested in. * * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. * @param int $stackPtr The position in the stack where * the token was found. * * @return void * @throws PHP_CodeSniffer_Exception If jslint.js could not be run */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); $lintPath = PHP_CodeSniffer::getConfigData('gjslint_path'); if ($lintPath === null) { return; } $cmd = "{$lintPath} --nosummary --notime --unix_mode \"{$fileName}\""; $msg = exec($cmd, $output, $retval); if (is_array($output) === false) { return; } foreach ($output as $finding) { $matches = array(); $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 = array($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 + 1; }
/** * @param \PHP_CodeSniffer_File $phpCsFile * @param int $stackPointer * * @return void */ protected function addFixableMissingApiAnnotation(\PHP_CodeSniffer_File $phpCsFile, $stackPointer) { $fix = $phpCsFile->addFixableError('@api annotation is missing', $stackPointer); if ($fix) { $docCommentOpenerPosition = $phpCsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPointer); $firstDocCommentTagPosition = $phpCsFile->findNext(T_DOC_COMMENT_TAG, $docCommentOpenerPosition); if (!$firstDocCommentTagPosition) { $phpCsFile->addErrorOnLine('Cannot fix missing @api tag', $stackPointer); return; } $startPosition = $firstDocCommentTagPosition - 2; $phpCsFile->fixer->beginChangeset(); $phpCsFile->fixer->addContent($startPosition, ' @api'); $phpCsFile->fixer->addNewline($startPosition); $phpCsFile->fixer->addContent($startPosition, ' * '); $phpCsFile->fixer->addNewline($startPosition); $phpCsFile->fixer->addContent($startPosition, ' * '); $phpCsFile->fixer->endChangeset(); } }