PHP_CodeSniffer_File::addErrorOnLine PHP Method

addErrorOnLine() public method

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.
return boolean
    public function addErrorOnLine($error, $line, $code = '', $data = array(), $severity = 0)
    {
        return $this->_addError($error, $line, 1, $code, $data, $severity, false);
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of PHP_CodeSniffer_File::addErrorOnLine