|
1 |
| -<?php |
2 |
| -/** |
3 |
| - * Generic_Sniffs_Files_LineLengthSniff. |
4 |
| - * |
5 |
| - * PHP version 5 |
6 |
| - * |
7 |
| - * @category PHP |
8 |
| - * @package PHP_CodeSniffer |
9 |
| - * @author Greg Sherwood <[email protected]> |
10 |
| - * @author Marc McIntyre <[email protected]> |
11 |
| - * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
12 |
| - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
13 |
| - * @link http://pear.php.net/package/PHP_CodeSniffer |
14 |
| - */ |
15 |
| - |
16 |
| -/** |
17 |
| - * Generic_Sniffs_Files_LineLengthSniff. |
18 |
| - * |
19 |
| - * Checks all lines in the file, and throws warnings if they are over 80 |
20 |
| - * characters in length and errors if they are over 100. Both these |
21 |
| - * figures can be changed by extending this sniff in your own standard. |
22 |
| - * |
23 |
| - * @category PHP |
24 |
| - * @package PHP_CodeSniffer |
25 |
| - * @author Greg Sherwood <[email protected]> |
26 |
| - * @author Marc McIntyre <[email protected]> |
27 |
| - * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
28 |
| - * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
29 |
| - * @version Release: @package_version@ |
30 |
| - * @link http://pear.php.net/package/PHP_CodeSniffer |
31 |
| - */ |
32 |
| -class Generic_Sniffs_Files_LineLengthSniff implements PHP_CodeSniffer_Sniff |
33 |
| -{ |
34 |
| - |
35 |
| - /** |
36 |
| - * The limit that the length of a line should not exceed. |
37 |
| - * |
38 |
| - * @var int |
39 |
| - */ |
40 |
| - public $lineLimit = 80; |
41 |
| - |
42 |
| - /** |
43 |
| - * The limit that the length of a line must not exceed. |
44 |
| - * |
45 |
| - * Set to zero (0) to disable. |
46 |
| - * |
47 |
| - * @var int |
48 |
| - */ |
49 |
| - public $absoluteLineLimit = 100; |
50 |
| - |
51 |
| - |
52 |
| - /** |
53 |
| - * Returns an array of tokens this test wants to listen for. |
54 |
| - * |
55 |
| - * @return array |
56 |
| - */ |
57 |
| - public function register() |
58 |
| - { |
59 |
| - return array(T_OPEN_TAG); |
60 |
| - |
61 |
| - }//end register() |
62 |
| - |
63 |
| - |
64 |
| - /** |
65 |
| - * Processes this test, when one of its tokens is encountered. |
66 |
| - * |
67 |
| - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
68 |
| - * @param int $stackPtr The position of the current token in |
69 |
| - * the stack passed in $tokens. |
70 |
| - * |
71 |
| - * @return void |
72 |
| - */ |
73 |
| - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
74 |
| - { |
75 |
| - $tokens = $phpcsFile->getTokens(); |
76 |
| - |
77 |
| - // Make sure this is the first open tag. |
78 |
| - $previousOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
79 |
| - if ($previousOpenTag !== false) { |
80 |
| - return; |
81 |
| - } |
82 |
| - |
83 |
| - $tokenCount = 0; |
84 |
| - $currentLineContent = ''; |
85 |
| - $currentLine = 1; |
86 |
| - |
87 |
| - $trim = (strlen($phpcsFile->eolChar) * -1); |
88 |
| - for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) { |
89 |
| - if ($tokens[$tokenCount]['line'] === $currentLine) { |
90 |
| - $currentLineContent .= $tokens[$tokenCount]['content']; |
91 |
| - } else { |
92 |
| - $currentLineContent = substr($currentLineContent, 0, $trim); |
93 |
| - $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
94 |
| - $currentLineContent = $tokens[$tokenCount]['content']; |
95 |
| - $currentLine++; |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - $currentLineContent = substr($currentLineContent, 0, $trim); |
100 |
| - $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
101 |
| - |
102 |
| - }//end process() |
103 |
| - |
104 |
| - |
105 |
| - /** |
106 |
| - * Checks if a line is too long. |
107 |
| - * |
108 |
| - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
109 |
| - * @param int $stackPtr The token at the end of the line. |
110 |
| - * @param string $lineContent The content of the line. |
111 |
| - * |
112 |
| - * @return void |
113 |
| - */ |
114 |
| - protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) |
115 |
| - { |
116 |
| - // If the content is a CVS or SVN id in a version tag, or it is |
117 |
| - // a license tag with a name and URL, or it is an SVN URL, there |
118 |
| - // is nothing the developer can do to shorten the line, |
119 |
| - // so don't throw errors. |
120 |
| - $regex = '~@license|@version[^\$]+\$Id|\$(Head)?URL[:\$]~'; |
121 |
| - if (preg_match($regex, $lineContent) !== 0) { |
122 |
| - return; |
123 |
| - } |
124 |
| - |
125 |
| - if (PHP_CODESNIFFER_ENCODING !== 'iso-8859-1') { |
126 |
| - // Not using the default encoding, so take a bit more care. |
127 |
| - $lineLength = iconv_strlen($lineContent, PHP_CODESNIFFER_ENCODING); |
128 |
| - if ($lineLength === false) { |
129 |
| - // String contained invalid characters, so revert to default. |
130 |
| - $lineLength = strlen($lineContent); |
131 |
| - } |
132 |
| - } else { |
133 |
| - $lineLength = strlen($lineContent); |
134 |
| - } |
135 |
| - |
136 |
| - if ($this->absoluteLineLimit > 0 |
137 |
| - && $lineLength > $this->absoluteLineLimit |
138 |
| - ) { |
139 |
| - $data = array( |
140 |
| - $this->absoluteLineLimit, |
141 |
| - $lineLength, |
142 |
| - ); |
143 |
| - |
144 |
| - $error = 'Line exceeds maximum limit of %s characters; contains %s characters'; |
145 |
| - $phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data); |
146 |
| - } else if ($lineLength > $this->lineLimit) { |
147 |
| - $data = array( |
148 |
| - $this->lineLimit, |
149 |
| - $lineLength, |
150 |
| - ); |
151 |
| - |
152 |
| - $warning = 'Line exceeds %s characters; contains %s characters'; |
153 |
| - $phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data); |
154 |
| - } |
155 |
| - |
156 |
| - }//end checkLineLength() |
157 |
| - |
158 |
| - |
159 |
| -}//end class |
160 |
| -
|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Generic_Sniffs_Files_LineLengthSniff. |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category PHP |
| 8 | + * @package PHP_CodeSniffer |
| 9 | + * @author Greg Sherwood <[email protected]> |
| 10 | + * @author Marc McIntyre <[email protected]> |
| 11 | + * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
| 12 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 13 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Generic_Sniffs_Files_LineLengthSniff. |
| 18 | + * |
| 19 | + * Checks all lines in the file, and throws warnings if they are over 80 |
| 20 | + * characters in length and errors if they are over 100. Both these |
| 21 | + * figures can be changed by extending this sniff in your own standard. |
| 22 | + * |
| 23 | + * @category PHP |
| 24 | + * @package PHP_CodeSniffer |
| 25 | + * @author Greg Sherwood <[email protected]> |
| 26 | + * @author Marc McIntyre <[email protected]> |
| 27 | + * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) |
| 28 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 29 | + * @version Release: @package_version@ |
| 30 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 31 | + */ |
| 32 | +class Generic_Sniffs_Files_LineLengthSniff implements PHP_CodeSniffer_Sniff |
| 33 | +{ |
| 34 | + |
| 35 | + /** |
| 36 | + * The limit that the length of a line should not exceed. |
| 37 | + * |
| 38 | + * @var int |
| 39 | + */ |
| 40 | + public $lineLimit = 80; |
| 41 | + |
| 42 | + /** |
| 43 | + * The limit that the length of a line must not exceed. |
| 44 | + * |
| 45 | + * Set to zero (0) to disable. |
| 46 | + * |
| 47 | + * @var int |
| 48 | + */ |
| 49 | + public $absoluteLineLimit = 100; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns an array of tokens this test wants to listen for. |
| 54 | + * |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function register() |
| 58 | + { |
| 59 | + return array(T_OPEN_TAG); |
| 60 | + |
| 61 | + }//end register() |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Processes this test, when one of its tokens is encountered. |
| 66 | + * |
| 67 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 68 | + * @param int $stackPtr The position of the current token in |
| 69 | + * the stack passed in $tokens. |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 74 | + { |
| 75 | + $tokens = $phpcsFile->getTokens(); |
| 76 | + |
| 77 | + // Make sure this is the first open tag. |
| 78 | + $previousOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
| 79 | + if ($previousOpenTag !== false) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $tokenCount = 0; |
| 84 | + $currentLineContent = ''; |
| 85 | + $currentLine = 1; |
| 86 | + |
| 87 | + $trim = (strlen($phpcsFile->eolChar) * -1); |
| 88 | + for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) { |
| 89 | + if ($tokens[$tokenCount]['line'] === $currentLine) { |
| 90 | + $currentLineContent .= $tokens[$tokenCount]['content']; |
| 91 | + } else { |
| 92 | + $currentLineContent = substr($currentLineContent, 0, $trim); |
| 93 | + $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
| 94 | + $currentLineContent = $tokens[$tokenCount]['content']; |
| 95 | + $currentLine++; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $currentLineContent = substr($currentLineContent, 0, $trim); |
| 100 | + $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
| 101 | + |
| 102 | + }//end process() |
| 103 | + |
| 104 | + |
| 105 | + /** |
| 106 | + * Checks if a line is too long. |
| 107 | + * |
| 108 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 109 | + * @param int $stackPtr The token at the end of the line. |
| 110 | + * @param string $lineContent The content of the line. |
| 111 | + * |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) |
| 115 | + { |
| 116 | + // If the content is a CVS or SVN id in a version tag, or it is |
| 117 | + // a license tag with a name and URL, or it is an SVN URL, there |
| 118 | + // is nothing the developer can do to shorten the line, |
| 119 | + // so don't throw errors. |
| 120 | + $regex = '~@license|@version[^\$]+\$Id|\$(Head)?URL[:\$]~'; |
| 121 | + if (preg_match($regex, $lineContent) !== 0) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (PHP_CODESNIFFER_ENCODING !== 'iso-8859-1') { |
| 126 | + // Not using the default encoding, so take a bit more care. |
| 127 | + $lineLength = iconv_strlen($lineContent, PHP_CODESNIFFER_ENCODING); |
| 128 | + if ($lineLength === false) { |
| 129 | + // String contained invalid characters, so revert to default. |
| 130 | + $lineLength = strlen($lineContent); |
| 131 | + } |
| 132 | + } else { |
| 133 | + $lineLength = strlen($lineContent); |
| 134 | + } |
| 135 | + |
| 136 | + if ($this->absoluteLineLimit > 0 |
| 137 | + && $lineLength > $this->absoluteLineLimit |
| 138 | + ) { |
| 139 | + $data = array( |
| 140 | + $this->absoluteLineLimit, |
| 141 | + $lineLength, |
| 142 | + ); |
| 143 | + |
| 144 | + $error = 'Line exceeds maximum limit of %s characters; contains %s characters'; |
| 145 | + $phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data); |
| 146 | + } else if ($lineLength > $this->lineLimit) { |
| 147 | + $data = array( |
| 148 | + $this->lineLimit, |
| 149 | + $lineLength, |
| 150 | + ); |
| 151 | + |
| 152 | + $warning = 'Line exceeds %s characters; contains %s characters'; |
| 153 | + $phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data); |
| 154 | + } |
| 155 | + |
| 156 | + }//end checkLineLength() |
| 157 | + |
| 158 | + |
| 159 | +}//end class |
| 160 | + |
0 commit comments