|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodingStandard_Sniffs_WhiteSpace_CommaSpacingSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHP_CodeSniffer |
|
9
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
10
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
11
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace CodingStandard\Sniffs\WhiteSpace; |
|
15
|
|
|
|
|
16
|
|
|
use PHP_CodeSniffer\Files\File; |
|
17
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* CodingStandard_Sniffs_WhiteSpace_CommaSpacingSniff. |
|
21
|
|
|
* |
|
22
|
|
|
* Ensure there is single whitespace after comma and none before. |
|
23
|
|
|
* |
|
24
|
|
|
* @category PHP |
|
25
|
|
|
* @package PHP_CodeSniffer |
|
26
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
27
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
28
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
29
|
|
|
*/ |
|
30
|
|
|
class CommaSpacingSniff implements Sniff |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function register() |
|
40
|
|
|
{ |
|
41
|
1 |
|
return array(T_COMMA); |
|
42
|
|
|
}//end register() |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
47
|
|
|
* |
|
48
|
|
|
* @param File $phpcsFile The file being scanned. |
|
49
|
|
|
* @param int $stackPtr The position of the current token in the |
|
50
|
|
|
* stack passed in $tokens. |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->checkContentBefore($phpcsFile, $stackPtr); |
|
57
|
1 |
|
$this->checkContentAfter($phpcsFile, $stackPtr); |
|
58
|
1 |
|
}//end process() |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Checks spacing before comma. |
|
62
|
|
|
* |
|
63
|
|
|
* @param File $phpcsFile The file being scanned. |
|
64
|
|
|
* @param int $stackPtr The position of the current token |
|
65
|
|
|
* in the stack passed in $tokens. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function checkContentBefore(File $phpcsFile, $stackPtr) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
|
72
|
1 |
|
$prevToken = $tokens[($stackPtr - 1)]; |
|
73
|
|
|
|
|
74
|
1 |
|
if ($prevToken['content'] === '(') { |
|
75
|
1 |
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if ($prevToken['code'] === T_WHITESPACE && $tokens[($stackPtr - 2)]['code'] !== T_COMMA) { |
|
79
|
1 |
|
$error = 'Space found before comma'; |
|
80
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Before'); |
|
81
|
1 |
|
if ($fix === true) { |
|
82
|
1 |
|
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
|
83
|
1 |
|
} |
|
84
|
1 |
|
} |
|
85
|
1 |
|
}//end checkContentBefore() |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Checks spacing after comma. |
|
89
|
|
|
* |
|
90
|
|
|
* @param File $phpcsFile The file being scanned. |
|
91
|
|
|
* @param int $stackPtr The position of the current token |
|
92
|
|
|
* in the stack passed in $tokens. |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function checkContentAfter(File $phpcsFile, $stackPtr) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
|
99
|
1 |
|
$nextToken = $tokens[($stackPtr + 1)]; |
|
100
|
|
|
|
|
101
|
1 |
|
if ($nextToken['content'] === ')') { |
|
102
|
1 |
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
if ($nextToken['code'] !== T_WHITESPACE) { |
|
106
|
|
|
$error = 'No space found after comma'; |
|
107
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'After'); |
|
108
|
|
|
if ($fix === true) { |
|
109
|
|
|
$phpcsFile->fixer->addContent($stackPtr, ' '); |
|
110
|
|
|
} |
|
111
|
1 |
|
} elseif ($nextToken['content'] !== $phpcsFile->eolChar) { |
|
112
|
1 |
|
$spacingLength = $nextToken['length']; |
|
113
|
1 |
|
if ($spacingLength === 1) { |
|
114
|
1 |
|
$tokenAfterSpace = $tokens[($stackPtr + 2)]; |
|
115
|
1 |
|
if ($tokenAfterSpace['content'] === ')') { |
|
116
|
1 |
|
$error = 'Space found after comma'; |
|
117
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'After'); |
|
118
|
1 |
|
if ($fix === true) { |
|
119
|
1 |
|
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
|
120
|
1 |
|
} |
|
121
|
1 |
|
} |
|
122
|
1 |
|
} else { |
|
123
|
1 |
|
$error = 'Expected 1 space after comma; %s found'; |
|
124
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'After', array($spacingLength)); |
|
125
|
1 |
|
if ($fix === true) { |
|
126
|
1 |
|
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
|
127
|
1 |
|
} |
|
128
|
|
|
} |
|
129
|
1 |
|
} |
|
130
|
1 |
|
}//end checkContentAfter() |
|
131
|
|
|
}//end class |
|
132
|
|
|
|