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