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

OpeningFunctionBraceKernighanRitchieSniff::process()   F

Complexity

Conditions 27
Paths 1142

Size

Total Lines 109

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 109
rs 0
cc 27
nc 1142
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
 * Checks that the opening brace of a function is on the same line as the function declaration.
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\Functions;
11
12
use PHP_CodeSniffer\Sniffs\Sniff;
13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Util\Tokens;
15
16
class OpeningFunctionBraceKernighanRitchieSniff implements Sniff
17
{
18
19
20
    /**
21
     * Should this sniff check function braces?
22
     *
23
     * @var boolean
24
     */
25
    public $checkFunctions = true;
26
27
    /**
28
     * Should this sniff check closure braces?
29
     *
30
     * @var boolean
31
     */
32
    public $checkClosures = false;
33
34
35
    /**
36
     * Registers the tokens that this sniff wants to listen for.
37
     *
38
     * @return void
39
     */
40
    public function register()
41
    {
42
        return [
43
            T_FUNCTION,
44
            T_CLOSURE,
45
        ];
46
47
    }//end register()
48
49
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
54
     * @param int                         $stackPtr  The position of the current token in the
55
     *                                               stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(File $phpcsFile, $stackPtr)
60
    {
61
        $tokens = $phpcsFile->getTokens();
62
63
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
64
            return;
65
        }
66
67
        if (($tokens[$stackPtr]['code'] === T_FUNCTION
68
            && (bool) $this->checkFunctions === false)
69
            || ($tokens[$stackPtr]['code'] === T_CLOSURE
70
            && (bool) $this->checkClosures === false)
71
        ) {
72
            return;
73
        }
74
75
        $openingBrace = $tokens[$stackPtr]['scope_opener'];
76
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
77
        if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
78
            $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
79
            if ($use !== false) {
80
                $openBracket  = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
81
                $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
82
            }
83
        }
84
85
        $functionLine = $tokens[$closeBracket]['line'];
86
        $braceLine    = $tokens[$openingBrace]['line'];
87
88
        $lineDifference = ($braceLine - $functionLine);
89
90
        if ($lineDifference > 0) {
91
            $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'new line');
92
            $error = 'Opening brace should be on the same line as the declaration';
93
            $fix   = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
94
            if ($fix === true) {
95
                $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true);
96
                $phpcsFile->fixer->beginChangeset();
97
                $phpcsFile->fixer->addContent($prev, ' {');
98
                $phpcsFile->fixer->replaceToken($openingBrace, '');
99
                if ($tokens[($openingBrace + 1)]['code'] === T_WHITESPACE
100
                    && $tokens[($openingBrace + 2)]['line'] > $tokens[$openingBrace]['line']
101
                ) {
102
                    // Brace is followed by a new line, so remove it to ensure we don't
103
                    // leave behind a blank line at the top of the block.
104
                    $phpcsFile->fixer->replaceToken(($openingBrace + 1), '');
105
106
                    if ($tokens[($openingBrace - 1)]['code'] === T_WHITESPACE
107
                        && $tokens[($openingBrace - 1)]['line'] === $tokens[$openingBrace]['line']
108
                        && $tokens[($openingBrace - 2)]['line'] < $tokens[$openingBrace]['line']
109
                    ) {
110
                        // Brace is preceeded by indent, so remove it to ensure we don't
111
                        // leave behind more indent than is required for the first line.
112
                        $phpcsFile->fixer->replaceToken(($openingBrace - 1), '');
113
                    }
114
                }
115
116
                $phpcsFile->fixer->endChangeset();
117
            }//end if
118
        }//end if
119
120
        $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'same line');
121
122
        $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
123
        if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
124
            if ($next === $tokens[$stackPtr]['scope_closer']
125
                || $tokens[$next]['code'] === T_CLOSE_TAG
126
            ) {
127
                // Ignore empty functions.
128
                return;
129
            }
130
131
            $error = 'Opening brace must be the last content on the line';
132
            $fix   = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
133
            if ($fix === true) {
134
                $phpcsFile->fixer->addNewline($openingBrace);
135
            }
136
        }
137
138
        // Only continue checking if the opening brace looks good.
139
        if ($lineDifference > 0) {
140
            return;
141
        }
142
143
        // We are looking for tabs, even if they have been replaced, because
144
        // we enforce a space here.
145
        if (isset($tokens[($openingBrace - 1)]['orig_content']) === true) {
146
            $spacing = $tokens[($openingBrace - 1)]['orig_content'];
147
        } else {
148
            $spacing = $tokens[($openingBrace - 1)]['content'];
149
        }
150
151
        if ($tokens[($openingBrace - 1)]['code'] !== T_WHITESPACE) {
152
            $length = 0;
153
        } else if ($spacing === "\t") {
154
            $length = '\t';
155
        } else {
156
            $length = strlen($spacing);
157
        }
158
159
        if ($length !== 1) {
160
            $error = 'Expected 1 space before opening brace; found %s';
161
            $data  = [$length];
162
            $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceBeforeBrace', $data);
163
            if ($fix === true) {
164
                if ($length === 0 || $length === '\t') {
165
                    $phpcsFile->fixer->addContentBefore($openingBrace, ' ');
166
                } else {
167
                    $phpcsFile->fixer->replaceToken(($openingBrace - 1), ' ');
168
                }
169
            }
170
        }
171
172
    }//end process()
173
174
175
}//end class
176