PHP_CodeSniffer_File::addFixableError PHP Method

addFixableError() public method

Returns true if the error was recorded and should be fixed.
public addFixableError ( string $error, integer $stackPtr, string $code = '', array $data = [], integer $severity ) : boolean
$error string The error message.
$stackPtr integer The stack position where 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 addFixableError($error, $stackPtr, $code = '', $data = array(), $severity = 0)
    {
        $recorded = $this->addError($error, $stackPtr, $code, $data, $severity, true);
        if ($recorded === true && $this->fixer->enabled === true) {
            return true;
        }
        return 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)
 {
     $tokens = $phpcsFile->getTokens();
     $lastLineChecked = $tokens[$stackPtr]['line'];
     for ($i = $stackPtr + 1; $i < $tokens[$stackPtr]['comment_closer'] - 1; $i++) {
         // We are only interested in the beginning of the line.
         if ($tokens[$i]['line'] === $lastLineChecked) {
             continue;
         }
         // The first token on the line must be a whitespace followed by a star.
         if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {
             if ($tokens[$i + 1]['code'] !== T_DOC_COMMENT_STAR) {
                 $error = 'Doc comment star missing';
                 $fix = $phpcsFile->addFixableError($error, $i, 'StarMissing');
                 if ($fix === true) {
                     $phpcsFile->fixer->replaceToken($i, str_repeat(' ', $tokens[$stackPtr]['column']) . '* ');
                 }
             }
         } else {
             if ($tokens[$i]['code'] !== T_DOC_COMMENT_STAR) {
                 $error = 'Doc comment star missing';
                 $fix = $phpcsFile->addFixableError($error, $i, 'StarMissing');
                 if ($fix === true) {
                     $phpcsFile->fixer->addContentBefore($i, str_repeat(' ', $tokens[$stackPtr]['column']) . '* ');
                 }
             }
         }
         $lastLineChecked = $tokens[$i]['line'];
     }
     //end for
 }
All Usage Examples Of PHP_CodeSniffer_File::addFixableError