/** * 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(File $phpcsFile, $stackPtr) { $rhinoPath = Config::getExecutablePath('jslint'); $jslintPath = Config::getExecutablePath('jslint'); if ($rhinoPath === null || $jslintPath === null) { return; } $fileName = $phpcsFile->getFilename(); $cmd = "{$rhinoPath} \"{$jslintPath}\" \"{$fileName}\""; $msg = exec($cmd, $output, $retval); if (is_array($output) === true) { foreach ($output as $finding) { $matches = array(); $numMatches = preg_match('/Lint at line ([0-9]+).*:(.*)$/', $finding, $matches); if ($numMatches === 0) { continue; } $line = (int) $matches[1]; $message = 'jslint says: ' . trim($matches[2]); $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(File $phpcsFile, $stackPtr) { $jslPath = Config::getExecutablePath('jsl'); if (is_null($jslPath) === true) { return; } $fileName = $phpcsFile->getFilename(); $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 RuntimeException("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(File $phpcsFile, $stackPtr) { $csslintPath = Config::getExecutablePath('csslint'); if ($csslintPath === null) { return; } $fileName = $phpcsFile->getFilename(); $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; }
/** * 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(File $phpcsFile, $stackPtr) { $analyzerPath = Config::getExecutablePath('zend_ca'); if (is_null($analyzerPath) === true) { return; } $fileName = $phpcsFile->getFilename(); // 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 RuntimeException("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; }
/** * 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(File $phpcsFile, $stackPtr) { $lintPath = Config::getExecutablePath('gjslint'); if ($lintPath === null) { return; } $fileName = $phpcsFile->getFilename(); $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; }