PHP_CodeSniffer_File::addWarningOnLine PHP Method

addWarningOnLine() public method

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

Usage Example

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