Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_Files_LineLengthSniff.
|
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 * Checks comment lines in the file, and throws warnings if they are over 80
|
Chris@0
|
12 * characters in length.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @category PHP
|
Chris@0
|
15 * @package PHP_CodeSniffer
|
Chris@0
|
16 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
17 */
|
Chris@0
|
18 class Drupal_Sniffs_Files_LineLengthSniff extends Generic_Sniffs_Files_LineLengthSniff
|
Chris@0
|
19 {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The limit that the length of a line should not exceed.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var int
|
Chris@0
|
25 */
|
Chris@0
|
26 public $lineLimit = 80;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * The limit that the length of a line must not exceed.
|
Chris@0
|
30 * But just check the line length of comments....
|
Chris@0
|
31 *
|
Chris@0
|
32 * Set to zero (0) to disable.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var int
|
Chris@0
|
35 */
|
Chris@0
|
36 public $absoluteLineLimit = 0;
|
Chris@0
|
37
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Checks if a line is too long.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
43 * @param array $tokens The token stack.
|
Chris@0
|
44 * @param int $stackPtr The first token on the next line.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return void
|
Chris@0
|
47 */
|
Chris@0
|
48 protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr)
|
Chris@0
|
49 {
|
Chris@0
|
50 if (isset(PHP_CodeSniffer_Tokens::$commentTokens[$tokens[($stackPtr - 1)]['code']]) === true) {
|
Chris@0
|
51 $doc_comment_tag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, ($stackPtr - 1));
|
Chris@0
|
52 if ($doc_comment_tag !== false) {
|
Chris@0
|
53 // Allow doc comment tags such as long @param tags to exceed the 80
|
Chris@0
|
54 // character limit.
|
Chris@0
|
55 return;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if ($tokens[($stackPtr - 1)]['code'] === T_COMMENT
|
Chris@0
|
59 // Allow @link and @see documentation to exceed the 80 character
|
Chris@0
|
60 // limit.
|
Chris@0
|
61 && (preg_match('/^[[:space:]]*\/\/ @.+/', $tokens[($stackPtr - 1)]['content']) === 1
|
Chris@0
|
62 // Allow anything that does not contain spaces (like URLs) to be
|
Chris@0
|
63 // longer.
|
Chris@0
|
64 || strpos(trim($tokens[($stackPtr - 1)]['content'], "/ \n"), ' ') === false)
|
Chris@0
|
65 ) {
|
Chris@0
|
66 return;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 // Code examples between @code and @endcode are allowed to exceed 80
|
Chris@0
|
70 // characters.
|
Chris@0
|
71 if (isset($tokens[$stackPtr]) === true && $tokens[$stackPtr]['code'] === T_DOC_COMMENT_WHITESPACE) {
|
Chris@0
|
72 $tag = $phpcsFile->findPrevious(array(T_DOC_COMMENT_TAG, T_DOC_COMMENT_OPEN_TAG), ($stackPtr - 1));
|
Chris@0
|
73 if ($tokens[$tag]['content'] === '@code') {
|
Chris@0
|
74 return;
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 // Drupal 8 annotations can have long translatable descriptions and we
|
Chris@0
|
79 // allow them to exceed 80 characters.
|
Chris@0
|
80 if ($tokens[($stackPtr - 2)]['code'] === T_DOC_COMMENT_STRING
|
Chris@0
|
81 && (strpos($tokens[($stackPtr - 2)]['content'], '@Translation(') !== false
|
Chris@0
|
82 // Also allow anything without whitespace (like URLs) to exceed 80
|
Chris@0
|
83 // characters.
|
Chris@0
|
84 || strpos($tokens[($stackPtr - 2)]['content'], ' ') === false
|
Chris@0
|
85 // Allow long "Contains ..." comments in @file doc blocks.
|
Chris@0
|
86 || preg_match('/^Contains [a-zA-Z_\\\\.]+$/', $tokens[($stackPtr - 2)]['content']) === 1
|
Chris@0
|
87 // Allow long paths or namespaces in annotations such as
|
Chris@0
|
88 // "list_builder" = "Drupal\rules\Entity\Controller\RulesReactionListBuilder"
|
Chris@0
|
89 // cardinality = \Drupal\webform\WebformHandlerInterface::CARDINALITY_UNLIMITED.
|
Chris@0
|
90 || preg_match('#= ("|\')?\S+[\\\\/]\S+("|\')?,*$#', $tokens[($stackPtr - 2)]['content']) === 1)
|
Chris@0
|
91 // Allow @link tags in lists.
|
Chris@0
|
92 || strpos($tokens[($stackPtr - 2)]['content'], '- @link') !== false
|
Chris@0
|
93 // Allow hook implementation line to exceed 80 characters.
|
Chris@0
|
94 || preg_match('/^Implements hook_[a-zA-Z0-9_]+\(\)/', $tokens[($stackPtr - 2)]['content']) === 1
|
Chris@0
|
95 ) {
|
Chris@0
|
96 return;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
|
Chris@0
|
100 }//end if
|
Chris@0
|
101
|
Chris@0
|
102 }//end checkLineLength()
|
Chris@0
|
103
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Returns the length of a defined line.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
Chris@0
|
109 * @param int $currentLine The current line.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return int
|
Chris@0
|
112 */
|
Chris@0
|
113 public function getLineLength(PHP_CodeSniffer_File $phpcsFile, $currentLine)
|
Chris@0
|
114 {
|
Chris@0
|
115 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
116
|
Chris@0
|
117 $tokenCount = 0;
|
Chris@0
|
118 $currentLineContent = '';
|
Chris@0
|
119
|
Chris@0
|
120 $trim = (strlen($phpcsFile->eolChar) * -1);
|
Chris@0
|
121 for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
|
Chris@0
|
122 if ($tokens[$tokenCount]['line'] === $currentLine) {
|
Chris@0
|
123 $currentLineContent .= $tokens[$tokenCount]['content'];
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 return strlen($currentLineContent);
|
Chris@0
|
128
|
Chris@0
|
129 }//end getLineLength()
|
Chris@0
|
130
|
Chris@0
|
131
|
Chris@0
|
132 }//end class
|