comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/TxtFileLineLengthSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 /** 2 /**
3 * Drupal_Sniffs_Files_TxtFileLineLengthSniff. 3 * \Drupal\Sniffs\Files\TxtFileLineLengthSniff.
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9 9
10 namespace Drupal\Sniffs\Files;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Sniffs\Sniff;
14
10 /** 15 /**
11 * Drupal_Sniffs_Files_TxtFileLineLengthSniff. 16 * \Drupal\Sniffs\Files\TxtFileLineLengthSniff.
12 * 17 *
13 * Checks all lines in a *.txt or *.md file and throws warnings if they are over 80 18 * Checks all lines in a *.txt or *.md file and throws warnings if they are over 80
14 * characters in length. 19 * characters in length.
15 * 20 *
16 * @category PHP 21 * @category PHP
17 * @package PHP_CodeSniffer 22 * @package PHP_CodeSniffer
18 * @link http://pear.php.net/package/PHP_CodeSniffer 23 * @link http://pear.php.net/package/PHP_CodeSniffer
19 */ 24 */
20 class Drupal_Sniffs_Files_TxtFileLineLengthSniff implements PHP_CodeSniffer_Sniff 25 class TxtFileLineLengthSniff implements Sniff
21 { 26 {
22 27
23 28
24 /** 29 /**
25 * Returns an array of tokens this test wants to listen for. 30 * Returns an array of tokens this test wants to listen for.
34 39
35 40
36 /** 41 /**
37 * Processes this test, when one of its tokens is encountered. 42 * Processes this test, when one of its tokens is encountered.
38 * 43 *
39 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 44 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
40 * @param int $stackPtr The position of the current token in the 45 * @param int $stackPtr The position of the current token in the
41 * stack passed in $tokens. 46 * stack passed in $tokens.
42 * 47 *
43 * @return void 48 * @return void
44 */ 49 */
45 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 50 public function process(File $phpcsFile, $stackPtr)
46 { 51 {
47 $fileExtension = strtolower(substr($phpcsFile->getFilename(), -3)); 52 $fileExtension = strtolower(substr($phpcsFile->getFilename(), -3));
48 if ($fileExtension === 'txt' || $fileExtension === '.md') { 53 if ($fileExtension === 'txt' || $fileExtension === '.md') {
49 $tokens = $phpcsFile->getTokens(); 54 $tokens = $phpcsFile->getTokens();
50 55
51 $content = rtrim($tokens[$stackPtr]['content']); 56 $content = rtrim($tokens[$stackPtr]['content']);
52 $lineLength = mb_strlen($content, 'UTF-8'); 57 $lineLength = mb_strlen($content, 'UTF-8');
53 // Lines without spaces are allowed to be longer (for example long URLs). 58 // Lines without spaces are allowed to be longer (for example long URLs).
54 if ($lineLength > 80 && preg_match('/[^ ]+ [^ ]+/', $content) === 1) { 59 // Markdown allowed to be longer for lines
60 // - without spaces
61 // - starting with #
62 // - containing URLs (https://)
63 // - starting with | (tables).
64 if ($lineLength > 80 && preg_match('/^([^ ]+$|#|.*https?:\/\/|\|)/', $content) === 0) {
55 $data = array( 65 $data = array(
56 80, 66 80,
57 $lineLength, 67 $lineLength,
58 ); 68 );
59 $warning = 'Line exceeds %s characters; contains %s characters'; 69 $warning = 'Line exceeds %s characters; contains %s characters';