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
|
|
|
// @codeCoverageIgnoreStart |
17
|
|
|
if (class_exists('PSR2_Sniffs_Classes_ClassDeclarationSniff', true) === false) { |
18
|
|
|
$error = 'Class PSR2_Sniffs_Classes_ClassDeclarationSniff not found'; |
19
|
|
|
throw new PHP_CodeSniffer_Exception($error); |
20
|
|
|
} |
21
|
|
|
// @codeCoverageIgnoreEnd |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Declaration Test. |
25
|
|
|
* |
26
|
|
|
* Checks the declaration of the class and its inheritance is correct. |
27
|
|
|
* |
28
|
|
|
* @category PHP |
29
|
|
|
* @package PHP_CodeSniffer |
30
|
|
|
* @author Greg Sherwood <[email protected]> |
31
|
|
|
* @author Marc McIntyre <[email protected]> |
32
|
|
|
* @author Alexander Obuhovich <[email protected]> |
33
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
34
|
|
|
* @link https://github.com/aik099/CodingStandard |
35
|
|
|
*/ |
36
|
|
|
class CodingStandard_Sniffs_Classes_ClassDeclarationSniff extends PSR2_Sniffs_Classes_ClassDeclarationSniff |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Processes this test, when one of its tokens is encountered. |
42
|
|
|
* |
43
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
44
|
|
|
* @param int $stackPtr The position of the current token |
45
|
|
|
* in the stack passed in $tokens. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
1 |
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
50
|
|
|
{ |
51
|
|
|
// We want all the errors from the PEAR standard, plus some of our own. |
52
|
1 |
|
parent::process($phpcsFile, $stackPtr); |
53
|
|
|
|
54
|
1 |
|
}//end process() |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Processes the opening section of a class declaration. |
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
|
1 |
|
public function processOpen(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
67
|
|
|
{ |
68
|
1 |
|
parent::processOpen($phpcsFile, $stackPtr); |
69
|
|
|
|
70
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
71
|
|
|
|
72
|
1 |
|
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { |
73
|
1 |
|
$prevContent = $tokens[($stackPtr - 1)]['content']; |
74
|
1 |
|
if ($prevContent !== $phpcsFile->eolChar) { |
75
|
1 |
|
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
76
|
1 |
|
$spaces = strlen($blankSpace); |
77
|
|
|
|
78
|
1 |
|
if (in_array($tokens[($stackPtr - 2)]['code'], array(T_ABSTRACT, T_FINAL)) === false) { |
79
|
1 |
|
if ($spaces !== 0) { |
80
|
1 |
|
$type = strtolower($tokens[$stackPtr]['content']); |
81
|
1 |
|
$error = 'Expected 0 spaces before %s keyword; %s found'; |
82
|
|
|
$data = array( |
83
|
1 |
|
$type, |
84
|
1 |
|
$spaces, |
85
|
1 |
|
); |
86
|
|
|
|
87
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeKeyword', $data); |
88
|
1 |
|
if ($fix === true) { |
89
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
90
|
1 |
|
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
91
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
92
|
1 |
|
} |
93
|
1 |
|
} |
94
|
1 |
|
} |
95
|
1 |
|
}//end if |
96
|
1 |
|
}//end if |
97
|
|
|
|
98
|
1 |
|
}//end processOpen() |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Processes the closing section of a class declaration. |
103
|
|
|
* |
104
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
105
|
|
|
* @param int $stackPtr The position of the current token |
106
|
|
|
* in the stack passed in $tokens. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
1 |
|
public function processClose(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
111
|
|
|
{ |
112
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
113
|
|
|
|
114
|
|
|
// Just in case. |
115
|
1 |
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$closeBrace = $tokens[$stackPtr]['scope_closer']; |
120
|
1 |
|
if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { |
121
|
1 |
|
$prevContent = $tokens[($closeBrace - 1)]['content']; |
122
|
1 |
|
if ($prevContent !== $phpcsFile->eolChar) { |
123
|
1 |
|
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); |
124
|
1 |
|
$spaces = strlen($blankSpace); |
125
|
1 |
|
if ($spaces !== 0) { |
126
|
1 |
|
if ($tokens[($closeBrace - 1)]['line'] !== $tokens[$closeBrace]['line']) { |
127
|
1 |
|
$error = 'Expected 0 spaces before closing brace; newline found'; |
128
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewLineBeforeCloseBrace'); |
129
|
1 |
|
} else { |
130
|
1 |
|
$error = 'Expected 0 spaces before closing brace; %s found'; |
131
|
1 |
|
$data = array($spaces); |
132
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'SpaceBeforeCloseBrace', $data); |
133
|
|
|
}//end if |
134
|
|
|
|
135
|
1 |
|
if ($fix === true) { |
136
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
137
|
1 |
|
$phpcsFile->fixer->replaceToken(($closeBrace - 1), ''); |
138
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
139
|
1 |
|
} |
140
|
1 |
|
}//end if |
141
|
1 |
|
}//end if |
142
|
1 |
|
}//end if |
143
|
|
|
|
144
|
|
|
// Check that the closing brace has one blank line after it. |
145
|
1 |
|
$nextContent = $phpcsFile->findNext(array(T_WHITESPACE), ($closeBrace + 1), null, true); |
146
|
1 |
|
if ($nextContent !== false) { |
147
|
1 |
|
$difference = ($tokens[$nextContent]['line'] - $tokens[$closeBrace]['line'] - 1); |
148
|
1 |
|
if ($difference < 0) { |
149
|
1 |
|
$difference = 0; |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
|
if ($difference !== 1) { |
153
|
1 |
|
$error = 'Closing brace of a %s must be followed by a single blank line; found %s'; |
154
|
|
|
$data = array( |
155
|
1 |
|
$tokens[$stackPtr]['content'], |
156
|
1 |
|
$difference, |
157
|
1 |
|
); |
158
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'NewlinesAfterCloseBrace', $data); |
159
|
1 |
|
if ($fix === true) { |
160
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
161
|
|
|
|
162
|
1 |
|
if ($difference > 1) { |
163
|
1 |
|
for ($i = ($closeBrace + 1); $i < $nextContent; $i++) { |
164
|
1 |
|
if ($tokens[$i]['line'] === $tokens[$nextContent]['line']) { |
165
|
|
|
// Keep existing indentation. |
166
|
1 |
|
break; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
170
|
1 |
|
} |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
$phpcsFile->fixer->addNewline($closeBrace); |
174
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
175
|
1 |
|
} |
176
|
1 |
|
}//end if |
177
|
1 |
|
}//end if |
178
|
|
|
|
179
|
1 |
|
}//end processClose() |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
}//end class |
183
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.