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

ConcatenationSpacingSniff::process()   F

Complexity

Conditions 33
Paths 1896

Size

Total Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 98
rs 0
cc 33
nc 1896
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
 * Makes sure there are no spaces around the concatenation operator.
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\Squiz\Sniffs\Strings;
11
12
use PHP_CodeSniffer\Sniffs\Sniff;
13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Util\Tokens;
15
16
class ConcatenationSpacingSniff implements Sniff
17
{
18
19
    /**
20
     * The number of spaces before and after a string concat.
21
     *
22
     * @var integer
23
     */
24
    public $spacing = 0;
25
26
    /**
27
     * Allow newlines instead of spaces.
28
     *
29
     * @var boolean
30
     */
31
    public $ignoreNewlines = false;
32
33
34
    /**
35
     * Returns an array of tokens this test wants to listen for.
36
     *
37
     * @return array
38
     */
39
    public function register()
40
    {
41
        return [T_STRING_CONCAT];
42
43
    }//end register()
44
45
46
    /**
47
     * Processes this test, when one of its tokens is encountered.
48
     *
49
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
50
     * @param int                         $stackPtr  The position of the current token in the
51
     *                                               stack passed in $tokens.
52
     *
53
     * @return void
54
     */
55
    public function process(File $phpcsFile, $stackPtr)
56
    {
57
        $tokens = $phpcsFile->getTokens();
58
59
        $ignoreBefore = false;
60
        $prev         = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
61
        if ($tokens[$prev]['code'] === T_END_HEREDOC || $tokens[$prev]['code'] === T_END_NOWDOC) {
62
            // Spacing before must be preserved due to the here/nowdoc closing tag.
63
            $ignoreBefore = true;
64
        }
65
66
        $this->spacing = (int) $this->spacing;
67
68
        if ($ignoreBefore === false) {
69
            if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
70
                $before = 0;
71
            } else {
72
                if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) {
73
                    $before = 'newline';
74
                } else {
75
                    $before = $tokens[($stackPtr - 1)]['length'];
76
                }
77
            }
78
79
            $phpcsFile->recordMetric($stackPtr, 'Spacing before string concat', $before);
80
        }
81
82
        if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
83
            $after = 0;
84
        } else {
85
            if ($tokens[($stackPtr + 2)]['line'] !== $tokens[$stackPtr]['line']) {
86
                $after = 'newline';
87
            } else {
88
                $after = $tokens[($stackPtr + 1)]['length'];
89
            }
90
        }
91
92
        $phpcsFile->recordMetric($stackPtr, 'Spacing after string concat', $after);
93
94
        if (($ignoreBefore === true
95
            || $before === $this->spacing
96
            || ($before === 'newline'
97
            && $this->ignoreNewlines === true))
98
            && ($after === $this->spacing
99
            || ($after === 'newline'
100
            && $this->ignoreNewlines === true))
101
        ) {
102
            return;
103
        }
104
105
        if ($this->spacing === 0) {
106
            $message = 'Concat operator must not be surrounded by spaces';
107
            $data    = [];
108
        } else {
109
            if ($this->spacing > 1) {
110
                $message = 'Concat operator must be surrounded by %s spaces';
111
            } else {
112
                $message = 'Concat operator must be surrounded by a single space';
113
            }
114
115
            $data = [$this->spacing];
116
        }
117
118
        $fix = $phpcsFile->addFixableError($message, $stackPtr, 'PaddingFound', $data);
119
120
        if ($fix === true) {
121
            $padding = str_repeat(' ', $this->spacing);
122
            if ($ignoreBefore === false && ($before !== 'newline' || $this->ignoreNewlines === false)) {
123
                if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
124
                    $phpcsFile->fixer->beginChangeset();
125
                    $phpcsFile->fixer->replaceToken(($stackPtr - 1), $padding);
126
                    if ($this->spacing === 0
127
                        && ($tokens[($stackPtr - 2)]['code'] === T_LNUMBER
128
                        || $tokens[($stackPtr - 2)]['code'] === T_DNUMBER)
129
                    ) {
130
                        $phpcsFile->fixer->replaceToken(($stackPtr - 2), '('.$tokens[($stackPtr - 2)]['content'].')');
131
                    }
132
133
                    $phpcsFile->fixer->endChangeset();
134
                } else if ($this->spacing > 0) {
135
                    $phpcsFile->fixer->addContent(($stackPtr - 1), $padding);
136
                }
137
            }
138
139
            if ($after !== 'newline' || $this->ignoreNewlines === false) {
140
                if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
141
                    $phpcsFile->fixer->beginChangeset();
142
                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), $padding);
143
                    if ($this->spacing === 0
144
                        && ($tokens[($stackPtr + 2)]['code'] === T_LNUMBER
145
                        || $tokens[($stackPtr + 2)]['code'] === T_DNUMBER)
146
                    ) {
147
                        $phpcsFile->fixer->replaceToken(($stackPtr + 2), '('.$tokens[($stackPtr + 2)]['content'].')');
148
                    }
149
150
                    $phpcsFile->fixer->endChangeset();
151
                } else if ($this->spacing > 0) {
152
                    $phpcsFile->fixer->addContent($stackPtr, $padding);
153
                }
154
            }
155
        }//end if
156
157
    }//end process()
158
159
160
}//end class
161