~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.5/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: 1.5.5
 
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
                $continue = $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
 
94
                if ($continue === false) {
 
95
                    // Something went wrong and we should stop processing the file.
 
96
                    return;
 
97
                }
 
98
 
 
99
                $currentLineContent = $tokens[$tokenCount]['content'];
 
100
                $currentLine++;
 
101
            }
 
102
        }
 
103
 
 
104
        $currentLineContent = substr($currentLineContent, 0, $trim);
 
105
        $this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent);
 
106
 
 
107
    }//end process()
 
108
 
 
109
 
 
110
    /**
 
111
     * Checks if a line is too long.
 
112
     *
 
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.
 
116
     *
 
117
     * @return null|false
 
118
     */
 
119
    protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent)
 
120
    {
 
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) {
 
127
            return;
 
128
        }
 
129
 
 
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);
 
137
                }
 
138
            } else {
 
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');
 
141
                return false;
 
142
            }
 
143
        } else {
 
144
            $lineLength = strlen($lineContent);
 
145
        }
 
146
 
 
147
        if ($this->absoluteLineLimit > 0
 
148
            && $lineLength > $this->absoluteLineLimit
 
149
        ) {
 
150
            $data = array(
 
151
                     $this->absoluteLineLimit,
 
152
                     $lineLength,
 
153
                    );
 
154
 
 
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) {
 
158
            $data = array(
 
159
                     $this->lineLimit,
 
160
                     $lineLength,
 
161
                    );
 
162
 
 
163
            $warning = 'Line exceeds %s characters; contains %s characters';
 
164
            $phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
 
165
        }
 
166
 
 
167
    }//end checkLineLength()
 
168
 
 
169
 
 
170
}//end class
 
171