Records a warning against a specific token in the file.
public addWarningOnLine ( string $warning, integer $line, string $code = '', array $data = [], integer $severity ) : boolean | ||
$warning | string | The error message. |
$line | integer | The line on which the warning occurred. |
$code | string | A violation code unique to the sniff message. |
$data | array | Replacements for the warning message. |
$severity | integer | The severity level for this warning. A value of 0 will be converted into the default severity level. |
Résultat | boolean |
/** * 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 jshint.js could not be run */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); $rhinoPath = PHP_CodeSniffer::getConfigData('rhino_path'); $jshintPath = PHP_CodeSniffer::getConfigData('jshint_path'); if ($rhinoPath === null || $jshintPath === null) { return; } $cmd = "{$rhinoPath} \"{$jshintPath}\" \"{$fileName}\""; $msg = exec($cmd, $output, $retval); if (is_array($output) === true) { foreach ($output as $finding) { $matches = array(); $numMatches = preg_match('/^(.+)\\(.+:([0-9]+).*:[0-9]+\\)$/', $finding, $matches); if ($numMatches === 0) { continue; } $line = (int) $matches[2]; $message = 'jshint says: ' . trim($matches[1]); $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); } } // 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 */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); $jslPath = PHP_CodeSniffer::getConfigData('jsl_path'); if (is_null($jslPath) === true) { return; } $cmd = '"' . $jslPath . '" -nologo -nofilelisting -nocontext -nosummary -output-format __LINE__:__ERROR__ -process "' . $fileName . '"'; $msg = exec($cmd, $output, $retval); // Variable $exitCode is the last line of $output if no error occurs, on // error it is numeric. Try to handle various error conditions and // provide useful error reporting. if ($retval === 2 || $retval === 4) { if (is_array($output) === true) { $msg = join('\\n', $output); } throw new PHP_CodeSniffer_Exception("Failed invoking JavaScript Lint, retval was [{$retval}], output was [{$msg}]"); } if (is_array($output) === true) { foreach ($output as $finding) { $split = strpos($finding, ':'); $line = substr($finding, 0, $split); $message = substr($finding, $split + 1); $phpcsFile->addWarningOnLine(trim($message), $line, 'ExternalTool'); } } // 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 */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); $csslintPath = PHP_CodeSniffer::getConfigData('csslint_path'); if ($csslintPath === null) { return; } $cmd = $csslintPath . ' ' . escapeshellarg($fileName); exec($cmd, $output, $retval); if (is_array($output) === false) { return; } $count = count($output); for ($i = 0; $i < $count; $i++) { $matches = array(); $numMatches = preg_match('/(error|warning) at line (\\d+)/', $output[$i], $matches); if ($numMatches === 0) { continue; } $line = (int) $matches[2]; $message = 'csslint says: ' . $output[$i + 1]; // First line is message with error line and error code. // Second is error message. // Third is wrong line in file. // Fourth is empty line. $i += 4; $phpcsFile->addWarningOnLine($message, $line, 'ExternalTool'); } //end for // Ignore the rest of the file. return $phpcsFile->numTokens + 1; }
/** * @param PHP_CodeSniffer_File $phpCsFile * @param array $token */ private function checkIfTokenIsCodeIgnoreAnnotation(\PHP_CodeSniffer_File $phpCsFile, $token) { $tagType = $this->getTagType($token['content']); switch ($tagType) { case 0: $errorMessage = $this->tagTypes[0] . "-tag detected at line: " . $token['line']; $phpCsFile->addWarningOnLine($errorMessage, $token['line']); break; case 1: $errorMessage = $this->tagTypes[1] . "-tag detected at line: " . $token['line']; $phpCsFile->addWarningOnLine($errorMessage, $token['line']); break; case 2: $errorMessage = $this->tagTypes[2] . "-tag detected at line: " . $token['line']; $phpCsFile->addWarningOnLine($errorMessage, $token['line']); break; } }
/** * 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; }
/** * 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 int */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $fileName = $phpcsFile->getFilename(); $analyzerPath = PHP_CodeSniffer::getConfigData('zend_ca_path'); if (is_null($analyzerPath) === true) { return; } // In the command, 2>&1 is important because the code analyzer sends its // findings to stderr. $output normally contains only stdout, so using 2>&1 // will pipe even stderr to stdout. $cmd = $analyzerPath . ' ' . $fileName . ' 2>&1'; // There is the possibility to pass "--ide" as an option to the analyzer. // This would result in an output format which would be easier to parse. // The problem here is that no cleartext error messages are returnwd; only // error-code-labels. So for a start we go for cleartext output. $exitCode = exec($cmd, $output, $retval); // Variable $exitCode is the last line of $output if no error occures, on // error it is numeric. Try to handle various error conditions and // provide useful error reporting. if (is_numeric($exitCode) === true && $exitCode > 0) { if (is_array($output) === true) { $msg = join('\\n', $output); } throw new PHP_CodeSniffer_Exception("Failed invoking ZendCodeAnalyzer, exitcode was [{$exitCode}], retval was [{$retval}], output was [{$msg}]"); } if (is_array($output) === true) { foreach ($output as $finding) { // The first two lines of analyzer output contain // something like this: // > Zend Code Analyzer 1.2.2 // > Analyzing <filename>... // So skip these... $res = preg_match("/^.+\\(line ([0-9]+)\\):(.+)\$/", $finding, $regs); if (empty($regs) === true || $res === false) { continue; } $phpcsFile->addWarningOnLine(trim($regs[2]), $regs[1], 'ExternalTool'); } } // Ignore the rest of the file. return $phpcsFile->numTokens + 1; }