-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathControlStructureSpacingSniff.php
142 lines (122 loc) · 5.11 KB
/
ControlStructureSpacingSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Checks that control structures have the correct spacing around brackets.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class ControlStructureSpacingSniff implements Sniff
{
/**
* How many spaces should follow the opening bracket.
*
* @var integer
*/
public $requiredSpacesAfterOpen = 0;
/**
* How many spaces should precede the closing bracket.
*
* @var integer
*/
public $requiredSpacesBeforeClose = 0;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [
T_IF,
T_WHILE,
T_FOREACH,
T_FOR,
T_SWITCH,
T_ELSE,
T_ELSEIF,
T_CATCH,
T_MATCH,
];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
|| isset($tokens[$stackPtr]['parenthesis_closer']) === false
) {
return;
}
$parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
$parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
$nextContent = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), null, true);
if (in_array($tokens[$nextContent]['code'], Tokens::$commentTokens, true) === false) {
$spaceAfterOpen = 0;
if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) {
if (strpos($tokens[($parenOpener + 1)]['content'], $phpcsFile->eolChar) !== false) {
$spaceAfterOpen = 'newline';
} else {
$spaceAfterOpen = $tokens[($parenOpener + 1)]['length'];
}
}
$phpcsFile->recordMetric($stackPtr, 'Spaces after control structure open parenthesis', $spaceAfterOpen);
if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
$error = 'Expected %s spaces after opening bracket; %s found';
$data = [
$this->requiredSpacesAfterOpen,
$spaceAfterOpen,
];
$fix = $phpcsFile->addFixableError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
if ($fix === true) {
$padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
if ($spaceAfterOpen === 0) {
$phpcsFile->fixer->addContent($parenOpener, $padding);
} else if ($spaceAfterOpen === 'newline') {
$phpcsFile->fixer->replaceToken(($parenOpener + 1), '');
} else {
$phpcsFile->fixer->replaceToken(($parenOpener + 1), $padding);
}
}
}
}//end if
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($parenCloser - 1), $parenOpener, true);
if ($tokens[$prev]['line'] === $tokens[$parenCloser]['line']) {
$spaceBeforeClose = 0;
if ($tokens[($parenCloser - 1)]['code'] === T_WHITESPACE) {
$spaceBeforeClose = strlen(ltrim($tokens[($parenCloser - 1)]['content'], $phpcsFile->eolChar));
}
$phpcsFile->recordMetric($stackPtr, 'Spaces before control structure close parenthesis', $spaceBeforeClose);
if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
$error = 'Expected %s spaces before closing bracket; %s found';
$data = [
$this->requiredSpacesBeforeClose,
$spaceBeforeClose,
];
$fix = $phpcsFile->addFixableError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
if ($fix === true) {
$padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
if ($spaceBeforeClose === 0) {
$phpcsFile->fixer->addContentBefore($parenCloser, $padding);
} else {
$phpcsFile->fixer->replaceToken(($parenCloser - 1), $padding);
}
}
}
}//end if
}//end process()
}//end class