You are here

public function TxtFileLineLengthSniff::process in Coder 8.2

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

Processes this test, 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/TxtFileLineLengthSniff.php, line 50

Class

TxtFileLineLengthSniff
\Drupal\Sniffs\Files\TxtFileLineLengthSniff.

Namespace

Drupal\Sniffs\Files

Code

public function process(File $phpcsFile, $stackPtr) {
  $fileExtension = strtolower(substr($phpcsFile
    ->getFilename(), -3));
  if ($fileExtension === 'txt' || $fileExtension === '.md') {
    $tokens = $phpcsFile
      ->getTokens();
    $content = rtrim($tokens[$stackPtr]['content']);
    $lineLength = mb_strlen($content, 'UTF-8');

    // Lines without spaces are allowed to be longer (for example long URLs).
    // Markdown allowed to be longer for lines
    // - without spaces
    // - starting with #
    // - containing URLs (https://)
    // - starting with | (tables).
    if ($lineLength > 80 && preg_match('/^([^ ]+$|#|.*https?:\\/\\/|\\|)/', $content) === 0) {
      $data = array(
        80,
        $lineLength,
      );
      $warning = 'Line exceeds %s characters; contains %s characters';
      $phpcsFile
        ->addWarning($warning, $stackPtr, 'TooLong', $data);
    }
  }
}