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