1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_Classes_ClassDeclarationSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Greg Sherwood <[email protected]> |
10
|
|
|
* @author Marc McIntyre <[email protected]> |
11
|
|
|
* @author Alexander Obuhovich <[email protected]> |
12
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
13
|
|
|
* @link https://github.com/aik099/CodingStandard |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace CodingStandard\Sniffs\Classes; |
17
|
|
|
|
18
|
|
|
use PHP_CodeSniffer\Files\File; |
19
|
|
|
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff as PSR2_ClassDeclarationSniff; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Declaration Test. |
23
|
|
|
* |
24
|
|
|
* Checks the declaration of the class and its inheritance is correct. |
25
|
|
|
* |
26
|
|
|
* @category PHP |
27
|
|
|
* @package PHP_CodeSniffer |
28
|
|
|
* @author Greg Sherwood <[email protected]> |
29
|
|
|
* @author Marc McIntyre <[email protected]> |
30
|
|
|
* @author Alexander Obuhovich <[email protected]> |
31
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
32
|
|
|
* @link https://github.com/aik099/CodingStandard |
33
|
|
|
*/ |
34
|
|
|
class ClassDeclarationSniff extends PSR2_ClassDeclarationSniff |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Processes this test, when one of its tokens is encountered. |
40
|
|
|
* |
41
|
|
|
* @param File $phpcsFile The file being scanned. |
42
|
|
|
* @param int $stackPtr The position of the current token in the |
43
|
|
|
* stack passed in $tokens. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
48
|
|
|
{ |
49
|
|
|
// We want all the errors from the PEAR standard, plus some of our own. |
50
|
1 |
|
parent::process($phpcsFile, $stackPtr); |
51
|
1 |
|
}//end process() |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Processes the opening section of a class declaration. |
56
|
|
|
* |
57
|
|
|
* @param File $phpcsFile The file being scanned. |
58
|
|
|
* @param int $stackPtr The position of the current token |
59
|
|
|
* in the stack passed in $tokens. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
1 |
|
public function processOpen(File $phpcsFile, $stackPtr) |
64
|
|
|
{ |
65
|
1 |
|
parent::processOpen($phpcsFile, $stackPtr); |
66
|
|
|
|
67
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
68
|
|
|
|
69
|
1 |
|
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
70
|
1 |
|
$prevContent = $tokens[($stackPtr - 1)]['content']; |
71
|
1 |
|
if ($prevContent !== $phpcsFile->eolChar) { |
72
|
1 |
|
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
73
|
1 |
|
$spaces = strlen($blankSpace); |
74
|
|
|
|
75
|
1 |
|
if (in_array($tokens[($stackPtr - 2)]['code'], array(T_ABSTRACT, T_FINAL)) === false) { |
76
|
1 |
|
if ($spaces !== 0) { |
77
|
1 |
|
$type = strtolower($tokens[$stackPtr]['content']); |
78
|
1 |
|
$error = 'Expected 0 spaces before %s keyword; %s found'; |
79
|
|
|
$data = array( |
80
|
1 |
|
$type, |
81
|
1 |
|
$spaces, |
82
|
1 |
|
); |
83
|
|
|
|
84
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data); |
85
|
1 |
|
if ($fix === true) { |
86
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
87
|
1 |
|
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
88
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
89
|
1 |
|
} |
90
|
1 |
|
} |
91
|
1 |
|
} |
92
|
1 |
|
}//end if |
93
|
1 |
|
}//end if |
94
|
1 |
|
}//end processOpen() |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Processes the closing section of a class declaration. |
99
|
|
|
* |
100
|
|
|
* @param File $phpcsFile The file being scanned. |
101
|
|
|
* @param int $stackPtr The position of the current token |
102
|
|
|
* in the stack passed in $tokens. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
1 |
|
public function processClose(File $phpcsFile, $stackPtr) |
107
|
|
|
{ |
108
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
109
|
|
|
|
110
|
|
|
// Just in case. |
111
|
1 |
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$closeBrace = $tokens[$stackPtr]['scope_closer']; |
116
|
1 |
|
if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { |
117
|
1 |
|
$prevContent = $tokens[($closeBrace - 1)]['content']; |
118
|
1 |
|
if ($prevContent !== $phpcsFile->eolChar) { |
119
|
1 |
|
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
120
|
1 |
|
$spaces = strlen($blankSpace); |
121
|
1 |
|
if ($spaces !== 0) { |
122
|
1 |
|
if ($tokens[($closeBrace - 1)]['line'] !== $tokens[$closeBrace]['line']) { |
123
|
1 |
|
$error = 'Expected 0 spaces before closing brace; newline found'; |
124
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewLineBeforeCloseBrace'); |
125
|
1 |
|
} else { |
126
|
1 |
|
$error = 'Expected 0 spaces before closing brace; %s found'; |
127
|
1 |
|
$data = array($spaces); |
128
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data); |
129
|
|
|
}//end if |
130
|
|
|
|
131
|
1 |
|
if ($fix === true) { |
132
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
133
|
1 |
|
$phpcsFile->fixer->replaceToken(($closeBrace - 1), ''); |
134
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
135
|
1 |
|
} |
136
|
1 |
|
}//end if |
137
|
1 |
|
}//end if |
138
|
1 |
|
}//end if |
139
|
|
|
|
140
|
|
|
// Check that the closing brace has one blank line after it. |
141
|
1 |
|
$nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($closeBrace + 1), null, true); |
142
|
1 |
|
if ($nextContent !== false) { |
143
|
1 |
|
$difference = ($tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1); |
144
|
1 |
|
if ($difference < 0) { |
145
|
1 |
|
$difference = 0; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
if ($difference !== 1) { |
149
|
1 |
|
$error = 'Closing brace of a %s must be followed by a single blank line; found %s'; |
150
|
|
|
$data = array( |
151
|
1 |
|
$tokens[$stackPtr]['content'], |
152
|
1 |
|
$difference, |
153
|
1 |
|
); |
154
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
155
|
1 |
|
if ($fix === true) { |
156
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
157
|
|
|
|
158
|
1 |
|
if ($difference > 1) { |
159
|
1 |
|
for ($i = ($closeBrace + 1); $i < $nextContent; $i++) { |
160
|
1 |
|
if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) { |
161
|
|
|
// Keep existing indentation. |
162
|
1 |
|
break; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
166
|
1 |
|
} |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
$phpcsFile->fixer->addNewline($closeBrace); |
170
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
171
|
1 |
|
} |
172
|
1 |
|
}//end if |
173
|
1 |
|
}//end if |
174
|
1 |
|
}//end processClose() |
175
|
|
|
}//end class |
176
|
|
|
|