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