You are here

public function FileEncodingSniff::process in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Files/FileEncodingSniff.php \Drupal\Sniffs\Files\FileEncodingSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Files/FileEncodingSniff.php \Drupal\Sniffs\Files\FileEncodingSniff::process()

Processes this sniff, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token in: the stack passed in $tokens.

Return value

void

File

coder_sniffer/Drupal/Sniffs/Files/FileEncodingSniff.php, line 68

Class

FileEncodingSniff
\Drupal\Sniffs\Files\FileEncodingSniff.

Namespace

Drupal\Sniffs\Files

Code

public function process(File $phpcsFile, $stackPtr) {

  // Not all PHP installs have the multi byte extension - nothing we can do.
  if (function_exists('mb_check_encoding') === false) {
    return $phpcsFile->numTokens;
  }
  $fileContent = $phpcsFile
    ->getTokensAsString(0, $phpcsFile->numTokens);
  $validEncodingFound = false;
  foreach ($this->allowedEncodings as $encoding) {
    if (mb_check_encoding($fileContent, $encoding) === true) {
      $validEncodingFound = true;
    }
  }
  if ($validEncodingFound === false) {
    $warning = 'File encoding is invalid, expected %s';
    $data = array(
      implode(' or ', $this->allowedEncodings),
    );
    $phpcsFile
      ->addWarning($warning, $stackPtr, 'InvalidEncoding', $data);
  }
  return $phpcsFile->numTokens;
}