3
* Generic_Sniffs_Files_LineLengthSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 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
17
* Generic_Sniffs_Files_LineLengthSniff.
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.
24
* @package PHP_CodeSniffer
25
* @author Greg Sherwood <gsherwood@squiz.net>
26
* @author Marc McIntyre <mmcintyre@squiz.net>
27
* @copyright 2006-2014 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: 1.5.5
30
* @link http://pear.php.net/package/PHP_CodeSniffer
32
class Generic_Sniffs_Files_LineLengthSniff implements PHP_CodeSniffer_Sniff
36
* The limit that the length of a line should not exceed.
40
public $lineLimit = 80;
43
* The limit that the length of a line must not exceed.
45
* Set to zero (0) to disable.
49
public $absoluteLineLimit = 100;
53
* Returns an array of tokens this test wants to listen for.
57
public function register()
59
return array(T_OPEN_TAG);
65
* Processes this test, when one of its tokens is encountered.
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.
73
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
75
$tokens = $phpcsFile->getTokens();
77
// Make sure this is the first open tag.
78
$previousOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
79
if ($previousOpenTag !== false) {
84
$currentLineContent = '';
87
$trim = (strlen($phpcsFile->eolChar) * -1);
88
for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
89
if ($tokens[$tokenCount]['line'] === $currentLine) {
90
$currentLineContent .= $tokens[$tokenCount]['content'];
92
$currentLineContent = substr($currentLineContent, 0, $trim);
93
$continue = $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
94
if ($continue === false) {
95
// Something went wrong and we should stop processing the file.
99
$currentLineContent = $tokens[$tokenCount]['content'];
104
$currentLineContent = substr($currentLineContent, 0, $trim);
105
$this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
111
* Checks if a line is too long.
113
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
114
* @param int $stackPtr The token at the end of the line.
115
* @param string $lineContent The content of the line.
119
protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent)
121
// If the content is a CVS or SVN id in a version tag, or it is
122
// a license tag with a name and URL, or it is an SVN URL, there
123
// is nothing the developer can do to shorten the line,
124
// so don't throw errors.
125
$regex = '~@license|@version[^\$]+\$Id|\$(Head)?URL[:\$]~';
126
if (preg_match($regex, $lineContent) !== 0) {
130
if (PHP_CODESNIFFER_ENCODING !== 'iso-8859-1') {
131
if (function_exists('iconv_strlen') === true) {
132
// Not using the default encoding, so take a bit more care.
133
$lineLength = iconv_strlen($lineContent, PHP_CODESNIFFER_ENCODING);
134
if ($lineLength === false) {
135
// String contained invalid characters, so revert to default.
136
$lineLength = strlen($lineContent);
139
$error = 'Line length could not be checked in this file as the iconv module has been disabled in PHP';
140
$phpcsFile->addWarning($error, $stackPtr, 'MissingIconv');
144
$lineLength = strlen($lineContent);
147
if ($this->absoluteLineLimit > 0
148
&& $lineLength > $this->absoluteLineLimit
151
$this->absoluteLineLimit,
155
$error = 'Line exceeds maximum limit of %s characters; contains %s characters';
156
$phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
157
} else if ($lineLength > $this->lineLimit) {
163
$warning = 'Line exceeds %s characters; contains %s characters';
164
$phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
167
}//end checkLineLength()