PHP_CodeSniffer_File::addFixableWarning PHP Method

addFixableWarning() public method

Returns true if the warning was recorded and should be fixed.
public addFixableWarning ( string $warning, integer $stackPtr, string $code = '', array $data = [], integer $severity ) : boolean
$warning 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 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 addFixableWarning($warning, $stackPtr, $code = '', $data = array(), $severity = 0)
    {
        $recorded = $this->addWarning($warning, $stackPtr, $code, $data, $severity, true);
        if ($recorded === true && $this->fixer->enabled === true) {
            return true;
        }
        return 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
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $token = $tokens[$stackPtr];
     $comment = $token['content'];
     $isDoubleSlashComment = substr($comment, 0, 2) === '//';
     if ($isDoubleSlashComment) {
         $hasLeadingSpace = $tokens[$stackPtr]['content'][2] === ' ';
         if ($hasLeadingSpace) {
             $hasMoreThanOneLeadingSpace = $tokens[$stackPtr]['content'][3] === ' ';
             if ($hasMoreThanOneLeadingSpace) {
                 $fix = $phpcsFile->addFixableWarning('Double slash comments must start with a single space', $stackPtr, 'SingleLeadingSpaceNeeded');
                 if ($fix) {
                     $fixedComment = preg_replace('/[ ]+/', ' ', $comment);
                     $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
                 }
             }
         } else {
             $fix = $phpcsFile->addFixableWarning('Double slash comments must start with a space', $stackPtr, 'LeadingSpaceNeeded');
             if ($fix) {
                 $fixedComment = substr_replace($comment, '// ', 0, 2);
                 $phpcsFile->fixer->replaceToken($stackPtr, $fixedComment);
             }
         }
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::addFixableWarning