Completed
Branch development (b1b115)
by Johannes
10:28
created

DisallowTabIndentSniff::process()   F

Complexity

Conditions 32
Paths 1326

Size

Total Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 125
rs 0
cc 32
nc 1326
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Throws errors if tabs are used for indentation.
4
 *
5
 * @author    Greg Sherwood <[email protected]>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9
10
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace;
11
12
use PHP_CodeSniffer\Sniffs\Sniff;
13
use PHP_CodeSniffer\Files\File;
14
15
class DisallowTabIndentSniff implements Sniff
16
{
17
18
    /**
19
     * A list of tokenizers this sniff supports.
20
     *
21
     * @var array
22
     */
23
    public $supportedTokenizers = [
24
        'PHP',
25
        'JS',
26
        'CSS',
27
    ];
28
29
    /**
30
     * The --tab-width CLI value that is being used.
31
     *
32
     * @var integer
33
     */
34
    private $tabWidth = null;
35
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return array
41
     */
42
    public function register()
43
    {
44
        return [T_OPEN_TAG];
45
46
    }//end register()
47
48
49
    /**
50
     * Processes this test, when one of its tokens is encountered.
51
     *
52
     * @param \PHP_CodeSniffer\Files\File $phpcsFile All the tokens found in the document.
53
     * @param int                         $stackPtr  The position of the current token in
54
     *                                               the stack passed in $tokens.
55
     *
56
     * @return void
57
     */
58
    public function process(File $phpcsFile, $stackPtr)
59
    {
60
        if ($this->tabWidth === null) {
61
            if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) {
62
                // We have no idea how wide tabs are, so assume 4 spaces for metrics.
63
                $this->tabWidth = 4;
64
            } else {
65
                $this->tabWidth = $phpcsFile->config->tabWidth;
66
            }
67
        }
68
69
        $tokens      = $phpcsFile->getTokens();
70
        $checkTokens = [
71
            T_WHITESPACE             => true,
72
            T_INLINE_HTML            => true,
73
            T_DOC_COMMENT_WHITESPACE => true,
74
            T_DOC_COMMENT_STRING     => true,
75
            T_COMMENT                => true,
76
        ];
77
78
        for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
79
            if (isset($checkTokens[$tokens[$i]['code']]) === false) {
80
                continue;
81
            }
82
83
            // If tabs are being converted to spaces by the tokeniser, the
84
            // original content should be checked instead of the converted content.
85
            if (isset($tokens[$i]['orig_content']) === true) {
86
                $content = $tokens[$i]['orig_content'];
87
            } else {
88
                $content = $tokens[$i]['content'];
89
            }
90
91
            if ($content === '') {
92
                continue;
93
            }
94
95
            // If this is an inline HTML token or a subsequent line of a multi-line comment,
96
            // split off the indentation as that is the only part to take into account for the metrics.
97
            $indentation = $content;
98
            if (($tokens[$i]['code'] === T_INLINE_HTML
99
                || $tokens[$i]['code'] === T_COMMENT)
100
                && preg_match('`^(\s*)\S.*`s', $content, $matches) > 0
101
            ) {
102
                if (isset($matches[1]) === true) {
103
                    $indentation = $matches[1];
104
                }
105
            }
106
107
            if (($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE
108
                || $tokens[$i]['code'] === T_COMMENT)
109
                && $indentation === ' '
110
            ) {
111
                // Ignore all non-indented comments, especially for recording metrics.
112
                continue;
113
            }
114
115
            $recordMetrics = true;
116
            if ($content === $indentation
117
                && isset($tokens[($i + 1)]) === true
118
                && $tokens[$i]['line'] < $tokens[($i + 1)]['line']
119
            ) {
120
                // Don't record metrics for empty lines.
121
                $recordMetrics = false;
122
            }
123
124
            $foundTabs = substr_count($content, "\t");
125
126
            $error     = 'Spaces must be used to indent lines; tabs are not allowed';
127
            $errorCode = 'TabsUsed';
128
            if ($tokens[$i]['column'] === 1) {
129
                if ($recordMetrics === true) {
130
                    $foundIndentSpaces = substr_count($indentation, ' ');
131
                    $foundIndentTabs   = substr_count($indentation, "\t");
132
133
                    if ($foundIndentTabs > 0 && $foundIndentSpaces === 0) {
134
                        $phpcsFile->recordMetric($i, 'Line indent', 'tabs');
135
                    } else if ($foundIndentTabs === 0 && $foundIndentSpaces > 0) {
136
                        $phpcsFile->recordMetric($i, 'Line indent', 'spaces');
137
                    } else if ($foundIndentTabs > 0 && $foundIndentSpaces > 0) {
138
                        $spacePosition  = strpos($indentation, ' ');
139
                        $tabAfterSpaces = strpos($indentation, "\t", $spacePosition);
140
                        if ($tabAfterSpaces !== false) {
141
                            $phpcsFile->recordMetric($i, 'Line indent', 'mixed');
142
                        } else {
143
                            // Check for use of precision spaces.
144
                            $numTabs = (int) floor($foundIndentSpaces / $this->tabWidth);
145
                            if ($numTabs === 0) {
146
                                $phpcsFile->recordMetric($i, 'Line indent', 'tabs');
147
                            } else {
148
                                $phpcsFile->recordMetric($i, 'Line indent', 'mixed');
149
                            }
150
                        }
151
                    }
152
                }//end if
153
            } else {
154
                // Look for tabs so we can report and replace, but don't
155
                // record any metrics about them because they aren't
156
                // line indent tokens.
157
                if ($foundTabs > 0) {
158
                    $error     = 'Spaces must be used for alignment; tabs are not allowed';
159
                    $errorCode = 'NonIndentTabsUsed';
160
                }
161
            }//end if
162
163
            if ($foundTabs === 0) {
164
                continue;
165
            }
166
167
            $fix = $phpcsFile->addFixableError($error, $i, $errorCode);
168
            if ($fix === true) {
169
                if (isset($tokens[$i]['orig_content']) === true) {
170
                    // Use the replacement that PHPCS has already done.
171
                    $phpcsFile->fixer->replaceToken($i, $tokens[$i]['content']);
172
                } else {
173
                    // Replace tabs with spaces, using an indent of tabWidth spaces.
174
                    // Other sniffs can then correct the indent if they need to.
175
                    $newContent = str_replace("\t", str_repeat(' ', $this->tabWidth), $tokens[$i]['content']);
176
                    $phpcsFile->fixer->replaceToken($i, $newContent);
177
                }
178
            }
179
        }//end for
180
181
        // Ignore the rest of the file.
182
        return ($phpcsFile->numTokens + 1);
183
184
    }//end process()
185
186
187
}//end class
188