5 |
aurelien |
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 <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: LineLengthSniff.php,v 1.16 2008/06/27 01:58:38 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Generic_Sniffs_Files_LineLengthSniff.
|
|
|
19 |
*
|
|
|
20 |
* Checks all lines in the file, and throws warnings if they are over 80
|
|
|
21 |
* characters in length and errors if they are over 100. Both these
|
|
|
22 |
* figures can be changed by extending this sniff in your own standard.
|
|
|
23 |
*
|
|
|
24 |
* @category PHP
|
|
|
25 |
* @package PHP_CodeSniffer
|
|
|
26 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
27 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
28 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
29 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
30 |
* @version Release: 1.2.0RC1
|
|
|
31 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
32 |
*/
|
|
|
33 |
class Generic_Sniffs_Files_LineLengthSniff implements PHP_CodeSniffer_Sniff
|
|
|
34 |
{
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* The limit that the length of a line should not exceed.
|
|
|
38 |
*
|
|
|
39 |
* @var int
|
|
|
40 |
*/
|
|
|
41 |
protected $lineLimit = 80;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* The limit that the length of a line must not exceed.
|
|
|
45 |
*
|
|
|
46 |
* Set to zero (0) to disable.
|
|
|
47 |
*
|
|
|
48 |
* @var int
|
|
|
49 |
*/
|
|
|
50 |
protected $absoluteLineLimit = 100;
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Returns an array of tokens this test wants to listen for.
|
|
|
55 |
*
|
|
|
56 |
* @return array
|
|
|
57 |
*/
|
|
|
58 |
public function register()
|
|
|
59 |
{
|
|
|
60 |
return array(T_OPEN_TAG);
|
|
|
61 |
|
|
|
62 |
}//end register()
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Processes this test, when one of its tokens is encountered.
|
|
|
67 |
*
|
|
|
68 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
69 |
* @param int $stackPtr The position of the current token in the
|
|
|
70 |
* stack passed in $tokens.
|
|
|
71 |
*
|
|
|
72 |
* @return void
|
|
|
73 |
*/
|
|
|
74 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
75 |
{
|
|
|
76 |
$tokens = $phpcsFile->getTokens();
|
|
|
77 |
|
|
|
78 |
// Make sure this is the first open tag.
|
|
|
79 |
$previousOpenTag = $phpcsFile->findPrevious(array(T_OPEN_TAG), ($stackPtr - 1));
|
|
|
80 |
if ($previousOpenTag !== false) {
|
|
|
81 |
return;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$tokenCount = 0;
|
|
|
85 |
$currentLineContent = '';
|
|
|
86 |
$currentLine = 1;
|
|
|
87 |
|
|
|
88 |
for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
|
|
|
89 |
if ($tokens[$tokenCount]['line'] === $currentLine) {
|
|
|
90 |
$currentLineContent .= $tokens[$tokenCount]['content'];
|
|
|
91 |
} else {
|
|
|
92 |
$currentLineContent = trim($currentLineContent, $phpcsFile->eolChar);
|
|
|
93 |
$this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
|
|
|
94 |
$currentLineContent = $tokens[$tokenCount]['content'];
|
|
|
95 |
$currentLine++;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
|
|
|
100 |
|
|
|
101 |
}//end process()
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Checks if a line is too long.
|
|
|
106 |
*
|
|
|
107 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
108 |
* @param int $stackPtr The token at the end of the line.
|
|
|
109 |
* @param string $lineContent The content of the line.
|
|
|
110 |
*
|
|
|
111 |
* @return void
|
|
|
112 |
*/
|
|
|
113 |
protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent)
|
|
|
114 |
{
|
|
|
115 |
// If the content is a CVS or SVN id in a version tag, or it is
|
|
|
116 |
// a license tag with a name and URL, there is nothing the
|
|
|
117 |
// developer can do to shorten the line, so don't throw errors.
|
|
|
118 |
if (preg_match('|@version[^\$]+\$Id|', $lineContent) === 0 && preg_match('|@license|', $lineContent) === 0) {
|
|
|
119 |
$lineLength = strlen($lineContent);
|
|
|
120 |
if ($this->absoluteLineLimit > 0 && $lineLength > $this->absoluteLineLimit) {
|
|
|
121 |
$error = 'Line exceeds maximum limit of '.$this->absoluteLineLimit." characters; contains $lineLength characters";
|
|
|
122 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
123 |
} else if ($lineLength > $this->lineLimit) {
|
|
|
124 |
$warning = 'Line exceeds '.$this->lineLimit." characters; contains $lineLength characters";
|
|
|
125 |
$phpcsFile->addWarning($warning, $stackPtr);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
}//end checkLineLength()
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
}//end class
|
|
|
133 |
|
|
|
134 |
?>
|