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-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
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 <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: @package_version@
30 * @link      http://pear.php.net/package/PHP_CodeSniffer
31 */
32class 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 int
72     */
73    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
74    {
75        $tokens = $phpcsFile->getTokens();
76        for ($i = 1; $i < $phpcsFile->numTokens; $i++) {
77            if ($tokens[$i]['column'] === 1) {
78                $this->checkLineLength($phpcsFile, $tokens, $i);
79            }
80        }
81
82        $this->checkLineLength($phpcsFile, $tokens, $i);
83
84        // Ignore the rest of the file.
85        return ($phpcsFile->numTokens + 1);
86
87    }//end process()
88
89
90    /**
91     * Checks if a line is too long.
92     *
93     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
94     * @param array                $tokens    The token stack.
95     * @param int                  $stackPtr  The first token on the next line.
96     *
97     * @return null|false
98     */
99    protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr)
100    {
101        // The passed token is the first on the line.
102        $stackPtr--;
103
104        if ($tokens[$stackPtr]['column'] === 1
105            && $tokens[$stackPtr]['length'] === 0
106        ) {
107            // Blank line.
108            return;
109        }
110
111        if ($tokens[$stackPtr]['column'] !== 1
112            && $tokens[$stackPtr]['content'] === $phpcsFile->eolChar
113        ) {
114            $stackPtr--;
115        }
116
117        $lineLength = ($tokens[$stackPtr]['column'] + $tokens[$stackPtr]['length'] - 1);
118
119        // Record metrics for common line length groupings.
120        if ($lineLength <= 80) {
121            $phpcsFile->recordMetric($stackPtr, 'Line length', '80 or less');
122        } else if ($lineLength <= 120) {
123            $phpcsFile->recordMetric($stackPtr, 'Line length', '81-120');
124        } else if ($lineLength <= 150) {
125            $phpcsFile->recordMetric($stackPtr, 'Line length', '121-150');
126        } else {
127            $phpcsFile->recordMetric($stackPtr, 'Line length', '151 or more');
128        }
129
130        // If this is a long comment, check if it can be broken up onto multiple lines.
131        // Some comments contain unbreakable strings like URLs and so it makes sense
132        // to ignore the line length in these cases if the URL would be longer than the max
133        // line length once you indent it to the correct level.
134        if ($lineLength > $this->lineLimit
135            && ($tokens[$stackPtr]['code'] === T_COMMENT
136            || $tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING)
137        ) {
138            $oldLength = strlen($tokens[$stackPtr]['content']);
139            $newLength = strlen(ltrim($tokens[$stackPtr]['content'], "/#\t "));
140            $indent    = (($tokens[$stackPtr]['column'] - 1) + ($oldLength - $newLength));
141
142            $nonBreakingLength = $tokens[$stackPtr]['length'];
143
144            $space = strrpos($tokens[$stackPtr]['content'], ' ');
145            if ($space !== false) {
146                $nonBreakingLength -= ($space + 1);
147            }
148
149            if (($nonBreakingLength + $indent) > $this->lineLimit) {
150                return;
151            }
152        }
153
154        if ($this->absoluteLineLimit > 0
155            && $lineLength > $this->absoluteLineLimit
156        ) {
157            $data = array(
158                     $this->absoluteLineLimit,
159                     $lineLength,
160                    );
161
162            $error = 'Line exceeds maximum limit of %s characters; contains %s characters';
163            $phpcsFile->addError($error, $stackPtr, 'MaxExceeded', $data);
164        } else if ($lineLength > $this->lineLimit) {
165            $data = array(
166                     $this->lineLimit,
167                     $lineLength,
168                    );
169
170            $warning = 'Line exceeds %s characters; contains %s characters';
171            $phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
172        }
173
174    }//end checkLineLength()
175
176
177}//end class
178