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