@@ -14,7 +14,7 @@ |
||
14 | 14 | /** |
15 | 15 | * Returns an array of tokens this test wants to listen for. |
16 | 16 | * |
17 | - * @return array |
|
17 | + * @return integer[] |
|
18 | 18 | */ |
19 | 19 | public function register() { |
20 | 20 | // We skip T_FOR, T_WHILE for now as they can have valid inline assignment |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | */ |
19 | 19 | public function register() { |
20 | 20 | // We skip T_FOR, T_WHILE for now as they can have valid inline assignment |
21 | - return [T_FOREACH, T_IF, T_SWITCH, T_OBJECT_OPERATOR, T_DOUBLE_COLON]; |
|
21 | + return [ T_FOREACH, T_IF, T_SWITCH, T_OBJECT_OPERATOR, T_DOUBLE_COLON ]; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -29,14 +29,14 @@ discard block |
||
29 | 29 | * in the stack passed in $tokens. |
30 | 30 | * @return void |
31 | 31 | */ |
32 | - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
32 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
33 | 33 | $tokens = $phpcsFile->getTokens(); |
34 | - if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR || $tokens[$stackPtr]['code'] === T_DOUBLE_COLON) { |
|
35 | - $this->checkMethodCalls($phpcsFile, $stackPtr); |
|
34 | + if ( $tokens[ $stackPtr ][ 'code' ] === T_OBJECT_OPERATOR || $tokens[ $stackPtr ][ 'code' ] === T_DOUBLE_COLON ) { |
|
35 | + $this->checkMethodCalls( $phpcsFile, $stackPtr ); |
|
36 | 36 | return; |
37 | 37 | } |
38 | 38 | |
39 | - $this->checkConditions($phpcsFile, $stackPtr); |
|
39 | + $this->checkConditions( $phpcsFile, $stackPtr ); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -44,25 +44,25 @@ discard block |
||
44 | 44 | * @param int $stackPtr |
45 | 45 | * @return void |
46 | 46 | */ |
47 | - protected function checkMethodCalls(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { |
|
47 | + protected function checkMethodCalls( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
|
48 | 48 | $tokens = $phpcsFile->getTokens(); |
49 | 49 | |
50 | - $openingBraceIndex = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1), $stackPtr + 4); |
|
51 | - if (!$openingBraceIndex) { |
|
50 | + $openingBraceIndex = $phpcsFile->findNext( T_OPEN_PARENTHESIS, ( $stackPtr + 1 ), $stackPtr + 4 ); |
|
51 | + if ( ! $openingBraceIndex ) { |
|
52 | 52 | return; |
53 | 53 | } |
54 | - if (empty($tokens[$openingBraceIndex]['parenthesis_closer'])) { |
|
54 | + if ( empty( $tokens[ $openingBraceIndex ][ 'parenthesis_closer' ] ) ) { |
|
55 | 55 | return; |
56 | 56 | } |
57 | 57 | |
58 | - $closingBraceIndex = $tokens[$openingBraceIndex]['parenthesis_closer']; |
|
58 | + $closingBraceIndex = $tokens[ $openingBraceIndex ][ 'parenthesis_closer' ]; |
|
59 | 59 | |
60 | - $hasInlineAssignment = $this->contains($phpcsFile, T_EQUAL, $openingBraceIndex + 1, $closingBraceIndex - 1); |
|
61 | - if (!$hasInlineAssignment) { |
|
60 | + $hasInlineAssignment = $this->contains( $phpcsFile, T_EQUAL, $openingBraceIndex + 1, $closingBraceIndex - 1 ); |
|
61 | + if ( ! $hasInlineAssignment ) { |
|
62 | 62 | return; |
63 | 63 | } |
64 | 64 | |
65 | - $phpcsFile->addError('Inline assignment not allowed', $stackPtr); |
|
65 | + $phpcsFile->addError( 'Inline assignment not allowed', $stackPtr ); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -70,25 +70,25 @@ discard block |
||
70 | 70 | * @param int $stackPtr |
71 | 71 | * @return void |
72 | 72 | */ |
73 | - protected function checkConditions($phpcsFile, $stackPtr) { |
|
73 | + protected function checkConditions( $phpcsFile, $stackPtr ) { |
|
74 | 74 | $tokens = $phpcsFile->getTokens(); |
75 | 75 | |
76 | - $openingBraceIndex = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
77 | - if (!$openingBraceIndex) { |
|
76 | + $openingBraceIndex = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); |
|
77 | + if ( ! $openingBraceIndex ) { |
|
78 | 78 | return; |
79 | 79 | } |
80 | - if (empty($tokens[$openingBraceIndex]['parenthesis_closer'])) { |
|
80 | + if ( empty( $tokens[ $openingBraceIndex ][ 'parenthesis_closer' ] ) ) { |
|
81 | 81 | return; |
82 | 82 | } |
83 | 83 | |
84 | - $closingBraceIndex = $tokens[$openingBraceIndex]['parenthesis_closer']; |
|
84 | + $closingBraceIndex = $tokens[ $openingBraceIndex ][ 'parenthesis_closer' ]; |
|
85 | 85 | |
86 | - $hasInlineAssignment = $this->contains($phpcsFile, T_EQUAL, $openingBraceIndex + 1, $closingBraceIndex - 1); |
|
87 | - if (!$hasInlineAssignment) { |
|
86 | + $hasInlineAssignment = $this->contains( $phpcsFile, T_EQUAL, $openingBraceIndex + 1, $closingBraceIndex - 1 ); |
|
87 | + if ( ! $hasInlineAssignment ) { |
|
88 | 88 | return; |
89 | 89 | } |
90 | 90 | |
91 | - $phpcsFile->addError('Conditional inline assignment not allowed', $stackPtr); |
|
91 | + $phpcsFile->addError( 'Conditional inline assignment not allowed', $stackPtr ); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
@@ -101,27 +101,27 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @return bool |
103 | 103 | */ |
104 | - protected function isFixableInlineAssignment(PHP_CodeSniffer_File $phpcsFile, $startIndex, $endIndex, &$indexEqualSign) { |
|
104 | + protected function isFixableInlineAssignment( PHP_CodeSniffer_File $phpcsFile, $startIndex, $endIndex, &$indexEqualSign ) { |
|
105 | 105 | $tokens = $phpcsFile->getTokens(); |
106 | 106 | |
107 | 107 | $hasInlineAssignment = false; |
108 | - for ($i = $startIndex; $i < $endIndex; $i++) { |
|
109 | - $currentToken = $tokens[$i]; |
|
108 | + for ( $i = $startIndex; $i < $endIndex; $i++ ) { |
|
109 | + $currentToken = $tokens[ $i ]; |
|
110 | 110 | |
111 | 111 | // We need to skip for complex assignments |
112 | - if ($this->isGivenKind(PHP_CodeSniffer_Tokens::$booleanOperators, $tokens[$currentToken])) { |
|
112 | + if ( $this->isGivenKind( PHP_CodeSniffer_Tokens::$booleanOperators, $tokens[ $currentToken ] ) ) { |
|
113 | 113 | $hasInlineAssignment = false; |
114 | 114 | break; |
115 | 115 | } |
116 | 116 | |
117 | 117 | // Negations we also cannot handle just yet |
118 | - if ($tokens[$currentToken]['code'] === T_BOOLEAN_NOT) { |
|
118 | + if ( $tokens[ $currentToken ][ 'code' ] === T_BOOLEAN_NOT ) { |
|
119 | 119 | $hasInlineAssignment = false; |
120 | 120 | break; |
121 | 121 | } |
122 | 122 | |
123 | 123 | // Comparison inside is also more complex |
124 | - if ($this->isGivenKind(PHP_CodeSniffer_Tokens::$comparisonTokens, $tokens[$currentToken])) { |
|
124 | + if ( $this->isGivenKind( PHP_CodeSniffer_Tokens::$comparisonTokens, $tokens[ $currentToken ] ) ) { |
|
125 | 125 | $hasInlineAssignment = false; |
126 | 126 | break; |
127 | 127 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | * Checks if the given token scope contains a single or multiple token codes/types. |
29 | 29 | * |
30 | 30 | * @param \PHP_CodeSniffer_File $phpcsFile |
31 | - * @param string|array $search |
|
31 | + * @param string $search |
|
32 | 32 | * @param int $start |
33 | 33 | * @param int $end |
34 | 34 | * @param bool $skipNested |
@@ -13,12 +13,12 @@ discard block |
||
13 | 13 | * @param array $token |
14 | 14 | * @return bool |
15 | 15 | */ |
16 | - protected function isGivenKind($search, array $token) { |
|
17 | - $search = (array)$search; |
|
18 | - if (in_array($token['code'], $search, true)) { |
|
16 | + protected function isGivenKind( $search, array $token ) { |
|
17 | + $search = (array) $search; |
|
18 | + if ( in_array( $token[ 'code' ], $search, true ) ) { |
|
19 | 19 | return true; |
20 | 20 | } |
21 | - if (in_array($token['type'], $search, true)) { |
|
21 | + if ( in_array( $token[ 'type' ], $search, true ) ) { |
|
22 | 22 | return true; |
23 | 23 | } |
24 | 24 | return false; |
@@ -34,24 +34,24 @@ discard block |
||
34 | 34 | * @param bool $skipNested |
35 | 35 | * @return bool |
36 | 36 | */ |
37 | - protected function contains(PHP_CodeSniffer_File $phpcsFile, $search, $start, $end, $skipNested = true) { |
|
37 | + protected function contains( PHP_CodeSniffer_File $phpcsFile, $search, $start, $end, $skipNested = true ) { |
|
38 | 38 | $tokens = $phpcsFile->getTokens(); |
39 | 39 | |
40 | - for ($i = $start; $i <= $end; $i++) { |
|
41 | - if ($skipNested && $tokens[$i]['code'] === T_OPEN_PARENTHESIS) { |
|
42 | - $i = $tokens[$i]['parenthesis_closer']; |
|
40 | + for ( $i = $start; $i <= $end; $i++ ) { |
|
41 | + if ( $skipNested && $tokens[ $i ][ 'code' ] === T_OPEN_PARENTHESIS ) { |
|
42 | + $i = $tokens[ $i ][ 'parenthesis_closer' ]; |
|
43 | 43 | continue; |
44 | 44 | } |
45 | - if ($skipNested && $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
46 | - $i = $tokens[$i]['bracket_closer']; |
|
45 | + if ( $skipNested && $tokens[ $i ][ 'code' ] === T_OPEN_SHORT_ARRAY ) { |
|
46 | + $i = $tokens[ $i ][ 'bracket_closer' ]; |
|
47 | 47 | continue; |
48 | 48 | } |
49 | - if ($skipNested && $tokens[$i]['code'] === T_OPEN_CURLY_BRACKET) { |
|
50 | - $i = $tokens[$i]['bracket_closer']; |
|
49 | + if ( $skipNested && $tokens[ $i ][ 'code' ] === T_OPEN_CURLY_BRACKET ) { |
|
50 | + $i = $tokens[ $i ][ 'bracket_closer' ]; |
|
51 | 51 | continue; |
52 | 52 | } |
53 | 53 | |
54 | - if ($this->isGivenKind($search, $tokens[$i])) { |
|
54 | + if ( $this->isGivenKind( $search, $tokens[ $i ] ) ) { |
|
55 | 55 | return true; |
56 | 56 | } |
57 | 57 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @param int $closingBraceIndex |
68 | 68 | * @return bool |
69 | 69 | */ |
70 | - protected function needsBrackets(PHP_CodeSniffer_File $phpcsFile, $openingBraceIndex, $closingBraceIndex) { |
|
70 | + protected function needsBrackets( PHP_CodeSniffer_File $phpcsFile, $openingBraceIndex, $closingBraceIndex ) { |
|
71 | 71 | $tokens = $phpcsFile->getTokens(); |
72 | 72 | |
73 | 73 | $whitelistedCodes = [ |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | T_OBJECT_OPERATOR, |
85 | 85 | ]; |
86 | 86 | |
87 | - for ($i = $openingBraceIndex + 1; $i < $closingBraceIndex; $i++) { |
|
88 | - if ($tokens[$i]['type'] === 'T_OPEN_PARENTHESIS') { |
|
89 | - $i = $tokens[$i]['parenthesis_closer']; |
|
87 | + for ( $i = $openingBraceIndex + 1; $i < $closingBraceIndex; $i++ ) { |
|
88 | + if ( $tokens[ $i ][ 'type' ] === 'T_OPEN_PARENTHESIS' ) { |
|
89 | + $i = $tokens[ $i ][ 'parenthesis_closer' ]; |
|
90 | 90 | continue; |
91 | 91 | } |
92 | - if (in_array($tokens[$i]['code'], $whitelistedCodes)) { |
|
92 | + if ( in_array( $tokens[ $i ][ 'code' ], $whitelistedCodes ) ) { |
|
93 | 93 | continue; |
94 | 94 | } |
95 | 95 | |
@@ -105,16 +105,16 @@ discard block |
||
105 | 105 | * |
106 | 106 | * @return int|null Stackpointer value of docblock end tag, or null if cannot be found |
107 | 107 | */ |
108 | - protected function findRelatedDocBlock(PHP_CodeSniffer_File $phpCsFile, $stackPointer) { |
|
108 | + protected function findRelatedDocBlock( PHP_CodeSniffer_File $phpCsFile, $stackPointer ) { |
|
109 | 109 | $tokens = $phpCsFile->getTokens(); |
110 | 110 | |
111 | - $line = $tokens[$stackPointer]['line']; |
|
111 | + $line = $tokens[ $stackPointer ][ 'line' ]; |
|
112 | 112 | $beginningOfLine = $stackPointer; |
113 | - while (!empty($tokens[$beginningOfLine - 1]) && $tokens[$beginningOfLine - 1]['line'] === $line) { |
|
113 | + while ( ! empty( $tokens[ $beginningOfLine - 1 ] ) && $tokens[ $beginningOfLine - 1 ][ 'line' ] === $line ) { |
|
114 | 114 | $beginningOfLine--; |
115 | 115 | } |
116 | 116 | |
117 | - if (!empty($tokens[$beginningOfLine - 2]) && $tokens[$beginningOfLine - 2]['type'] === 'T_DOC_COMMENT_CLOSE_TAG') { |
|
117 | + if ( ! empty( $tokens[ $beginningOfLine - 2 ] ) && $tokens[ $beginningOfLine - 2 ][ 'type' ] === 'T_DOC_COMMENT_CLOSE_TAG' ) { |
|
118 | 118 | return $beginningOfLine - 2; |
119 | 119 | } |
120 | 120 | |
@@ -127,15 +127,15 @@ discard block |
||
127 | 127 | * @param int $count |
128 | 128 | * @return void |
129 | 129 | */ |
130 | - protected function outdent(PHP_CodeSniffer_File $phpcsFile, $index, $count = 1) { |
|
130 | + protected function outdent( PHP_CodeSniffer_File $phpcsFile, $index, $count = 1 ) { |
|
131 | 131 | $tokens = $phpcsFile->getTokens(); |
132 | - $char = $this->getIndentationCharacter($tokens[$index]['content'], true); |
|
132 | + $char = $this->getIndentationCharacter( $tokens[ $index ][ 'content' ], true ); |
|
133 | 133 | |
134 | - $content = $tokens[$index]['content']; |
|
135 | - for ($i = 0; $i < $count; $i++) { |
|
136 | - $content = $this->strReplaceOnce($char, '', $content); |
|
134 | + $content = $tokens[ $index ][ 'content' ]; |
|
135 | + for ( $i = 0; $i < $count; $i++ ) { |
|
136 | + $content = $this->strReplaceOnce( $char, '', $content ); |
|
137 | 137 | } |
138 | - $phpcsFile->fixer->replaceToken($index, $content); |
|
138 | + $phpcsFile->fixer->replaceToken( $index, $content ); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -144,12 +144,12 @@ discard block |
||
144 | 144 | * @param int $count |
145 | 145 | * @return void |
146 | 146 | */ |
147 | - protected function indent(PHP_CodeSniffer_File $phpcsFile, $index, $count = 1) { |
|
147 | + protected function indent( PHP_CodeSniffer_File $phpcsFile, $index, $count = 1 ) { |
|
148 | 148 | $tokens = $phpcsFile->getTokens(); |
149 | - $char = $this->getIndentationCharacter($tokens[$index]['content'], true); |
|
149 | + $char = $this->getIndentationCharacter( $tokens[ $index ][ 'content' ], true ); |
|
150 | 150 | |
151 | - $content = str_repeat($char, $count) . $tokens[$index]['content']; |
|
152 | - $phpcsFile->fixer->replaceToken($index, $content); |
|
151 | + $content = str_repeat( $char, $count ) . $tokens[ $index ][ 'content' ]; |
|
152 | + $phpcsFile->fixer->replaceToken( $index, $content ); |
|
153 | 153 | } |
154 | 154 | |
155 | 155 | /** |
@@ -158,13 +158,13 @@ discard block |
||
158 | 158 | * @param string $subject |
159 | 159 | * @return string |
160 | 160 | */ |
161 | - protected function strReplaceOnce($search, $replace, $subject) { |
|
162 | - $pos = strpos($subject, $search); |
|
163 | - if ($pos === false) { |
|
161 | + protected function strReplaceOnce( $search, $replace, $subject ) { |
|
162 | + $pos = strpos( $subject, $search ); |
|
163 | + if ( $pos === false ) { |
|
164 | 164 | return $subject; |
165 | 165 | } |
166 | 166 | |
167 | - return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search)); |
|
167 | + return substr( $subject, 0, $pos ) . $replace . substr( $subject, $pos + strlen( $search ) ); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -174,19 +174,19 @@ discard block |
||
174 | 174 | * @param int $index |
175 | 175 | * @return int |
176 | 176 | */ |
177 | - protected function getIndentationLevel(PHP_CodeSniffer_File $phpcsFile, $index) { |
|
177 | + protected function getIndentationLevel( PHP_CodeSniffer_File $phpcsFile, $index ) { |
|
178 | 178 | $tokens = $phpcsFile->getTokens(); |
179 | 179 | |
180 | - $whitespace = $this->getIndentationWhitespace($phpcsFile, $index); |
|
181 | - $char = $this->getIndentationCharacter($whitespace); |
|
180 | + $whitespace = $this->getIndentationWhitespace( $phpcsFile, $index ); |
|
181 | + $char = $this->getIndentationCharacter( $whitespace ); |
|
182 | 182 | |
183 | - $level = $tokens[$index]['column'] - 1; |
|
183 | + $level = $tokens[ $index ][ 'column' ] - 1; |
|
184 | 184 | |
185 | - if ($char === "\t") { |
|
185 | + if ( $char === "\t" ) { |
|
186 | 186 | return $level; |
187 | 187 | } |
188 | 188 | |
189 | - return (int)($level / 4); |
|
189 | + return (int) ( $level / 4 ); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -194,22 +194,22 @@ discard block |
||
194 | 194 | * @param bool $correctLength |
195 | 195 | * @return string |
196 | 196 | */ |
197 | - protected function getIndentationCharacter($content, $correctLength = false) { |
|
198 | - if (strpos($content, "\n")) { |
|
199 | - $parts = explode("\n", $content); |
|
200 | - array_shift($parts); |
|
197 | + protected function getIndentationCharacter( $content, $correctLength = false ) { |
|
198 | + if ( strpos( $content, "\n" ) ) { |
|
199 | + $parts = explode( "\n", $content ); |
|
200 | + array_shift( $parts ); |
|
201 | 201 | } else { |
202 | - $parts = (array)$content; |
|
202 | + $parts = (array) $content; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | $char = "\t"; |
206 | 206 | $countTabs = $countSpaces = 0; |
207 | - foreach ($parts as $part) { |
|
208 | - $countTabs += substr_count($content, $char); |
|
209 | - $countSpaces += (int)(substr_count($content, ' ') / 4); |
|
207 | + foreach ( $parts as $part ) { |
|
208 | + $countTabs += substr_count( $content, $char ); |
|
209 | + $countSpaces += (int) ( substr_count( $content, ' ' ) / 4 ); |
|
210 | 210 | } |
211 | 211 | |
212 | - if ($countSpaces > 3 && $countSpaces > $countTabs) { |
|
212 | + if ( $countSpaces > 3 && $countSpaces > $countTabs ) { |
|
213 | 213 | $char = $correctLength ? ' ' : ' '; |
214 | 214 | } |
215 | 215 | |
@@ -221,13 +221,13 @@ discard block |
||
221 | 221 | * @param int $index |
222 | 222 | * @return string |
223 | 223 | */ |
224 | - protected function getIndentationWhitespace(PHP_CodeSniffer_File $phpcsFile, $index) { |
|
224 | + protected function getIndentationWhitespace( PHP_CodeSniffer_File $phpcsFile, $index ) { |
|
225 | 225 | $tokens = $phpcsFile->getTokens(); |
226 | 226 | |
227 | - $firstIndex = $this->getFirstTokenOfLine($tokens, $index); |
|
227 | + $firstIndex = $this->getFirstTokenOfLine( $tokens, $index ); |
|
228 | 228 | $whitespace = ''; |
229 | - if ($tokens[$firstIndex]['type'] === 'T_WHITESPACE' || $tokens[$firstIndex]['type'] === 'T_DOC_COMMENT_WHITESPACE') { |
|
230 | - $whitespace = $tokens[$firstIndex]['content']; |
|
229 | + if ( $tokens[ $firstIndex ][ 'type' ] === 'T_WHITESPACE' || $tokens[ $firstIndex ][ 'type' ] === 'T_DOC_COMMENT_WHITESPACE' ) { |
|
230 | + $whitespace = $tokens[ $firstIndex ][ 'content' ]; |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | return $whitespace; |
@@ -238,16 +238,16 @@ discard block |
||
238 | 238 | * @param int $index |
239 | 239 | * @return int |
240 | 240 | */ |
241 | - protected function getIndentationColumn(PHP_CodeSniffer_File $phpcsFile, $index) { |
|
241 | + protected function getIndentationColumn( PHP_CodeSniffer_File $phpcsFile, $index ) { |
|
242 | 242 | $tokens = $phpcsFile->getTokens(); |
243 | 243 | |
244 | - $firstIndex = $this->getFirstTokenOfLine($tokens, $index); |
|
244 | + $firstIndex = $this->getFirstTokenOfLine( $tokens, $index ); |
|
245 | 245 | |
246 | - $nextIndex = $phpcsFile->findNext(T_WHITESPACE, ($firstIndex + 1), null, true); |
|
247 | - if ($tokens[$nextIndex]['line'] !== $tokens[$index]['line']) { |
|
246 | + $nextIndex = $phpcsFile->findNext( T_WHITESPACE, ( $firstIndex + 1 ), null, true ); |
|
247 | + if ( $tokens[ $nextIndex ][ 'line' ] !== $tokens[ $index ][ 'line' ] ) { |
|
248 | 248 | return 0; |
249 | 249 | } |
250 | - return $tokens[$nextIndex]['column'] - 1; |
|
250 | + return $tokens[ $nextIndex ][ 'column' ] - 1; |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | /** |
@@ -255,11 +255,11 @@ discard block |
||
255 | 255 | * @param int $index |
256 | 256 | * @return int |
257 | 257 | */ |
258 | - protected function getFirstTokenOfLine(array $tokens, $index) { |
|
259 | - $line = $tokens[$index]['line']; |
|
258 | + protected function getFirstTokenOfLine( array $tokens, $index ) { |
|
259 | + $line = $tokens[ $index ][ 'line' ]; |
|
260 | 260 | |
261 | 261 | $currentIndex = $index; |
262 | - while ($tokens[$currentIndex - 1]['line'] === $line) { |
|
262 | + while ( $tokens[ $currentIndex - 1 ][ 'line' ] === $line ) { |
|
263 | 263 | $currentIndex--; |
264 | 264 | } |
265 | 265 | |
@@ -270,17 +270,17 @@ discard block |
||
270 | 270 | * @param \PHP_CodeSniffer_File $phpCsFile |
271 | 271 | * @return bool |
272 | 272 | */ |
273 | - protected function hasNamespace(PHP_CodeSniffer_File $phpCsFile) { |
|
274 | - return $this->findNamespaceIndex($phpCsFile) !== null; |
|
273 | + protected function hasNamespace( PHP_CodeSniffer_File $phpCsFile ) { |
|
274 | + return $this->findNamespaceIndex( $phpCsFile ) !== null; |
|
275 | 275 | } |
276 | 276 | |
277 | 277 | /** |
278 | 278 | * @param \PHP_CodeSniffer_File $phpCsFile |
279 | 279 | * @return int|null |
280 | 280 | */ |
281 | - protected function findNamespaceIndex(PHP_CodeSniffer_File $phpCsFile) { |
|
282 | - $namespacePosition = $phpCsFile->findNext(T_NAMESPACE, 0); |
|
283 | - if (!$namespacePosition) { |
|
281 | + protected function findNamespaceIndex( PHP_CodeSniffer_File $phpCsFile ) { |
|
282 | + $namespacePosition = $phpCsFile->findNext( T_NAMESPACE, 0 ); |
|
283 | + if ( ! $namespacePosition ) { |
|
284 | 284 | return null; |
285 | 285 | } |
286 | 286 | return $namespacePosition; |
@@ -290,21 +290,21 @@ discard block |
||
290 | 290 | * @param \PHP_CodeSniffer_File $phpcsFile |
291 | 291 | * @return array |
292 | 292 | */ |
293 | - protected function getNamespaceInfo(PHP_CodeSniffer_File $phpcsFile) { |
|
294 | - $startIndex = $this->findNamespaceIndex($phpcsFile); |
|
293 | + protected function getNamespaceInfo( PHP_CodeSniffer_File $phpcsFile ) { |
|
294 | + $startIndex = $this->findNamespaceIndex( $phpcsFile ); |
|
295 | 295 | |
296 | 296 | $endIndex = 0; |
297 | - if ($startIndex) { |
|
298 | - $endIndex = $phpcsFile->findNext(T_SEMICOLON, $startIndex + 1); |
|
297 | + if ( $startIndex ) { |
|
298 | + $endIndex = $phpcsFile->findNext( T_SEMICOLON, $startIndex + 1 ); |
|
299 | 299 | } |
300 | 300 | |
301 | - if (empty($startIndex) || empty($endIndex)) { |
|
302 | - return []; |
|
301 | + if ( empty( $startIndex ) || empty( $endIndex ) ) { |
|
302 | + return [ ]; |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | return [ |
306 | 306 | 'start' => $startIndex, |
307 | - 'namespace' => $this->getNamespaceAsString($phpcsFile, $startIndex + 1, $endIndex - 1), |
|
307 | + 'namespace' => $this->getNamespaceAsString( $phpcsFile, $startIndex + 1, $endIndex - 1 ), |
|
308 | 308 | 'end' => $endIndex |
309 | 309 | ]; |
310 | 310 | } |
@@ -316,15 +316,15 @@ discard block |
||
316 | 316 | * |
317 | 317 | * @return string |
318 | 318 | */ |
319 | - protected function getNamespaceAsString(PHP_CodeSniffer_File $phpCsFile, $startIndex, $endIndex) { |
|
319 | + protected function getNamespaceAsString( PHP_CodeSniffer_File $phpCsFile, $startIndex, $endIndex ) { |
|
320 | 320 | $tokens = $phpCsFile->getTokens(); |
321 | 321 | |
322 | 322 | $namespace = ''; |
323 | - for ($i = $startIndex; $i <= $endIndex; $i++) { |
|
324 | - $namespace .= $tokens[$i]['content']; |
|
323 | + for ( $i = $startIndex; $i <= $endIndex; $i++ ) { |
|
324 | + $namespace .= $tokens[ $i ][ 'content' ]; |
|
325 | 325 | } |
326 | 326 | |
327 | - return trim($namespace); |
|
327 | + return trim( $namespace ); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | } |
@@ -6,7 +6,6 @@ |
||
6 | 6 | |
7 | 7 | /** |
8 | 8 | * @param string $foo |
9 | - * @param array $b |
|
10 | 9 | * |
11 | 10 | * @return void |
12 | 11 | */ |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | * |
11 | 11 | * @return void |
12 | 12 | */ |
13 | - public function replaceMe($foo, $bar) { |
|
13 | + public function replaceMe( $foo, $bar ) { |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | /** |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * |
21 | 21 | * @return void |
22 | 22 | */ |
23 | - public function report($foo, $bar = null) { |
|
23 | + public function report( $foo, $bar = null ) { |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return void |
32 | 32 | */ |
33 | - public function correctMe2($foo) { |
|
33 | + public function correctMe2( $foo ) { |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | /** |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | * |
41 | 41 | * @return void |
42 | 42 | */ |
43 | - public function ok($foo) { |
|
43 | + public function ok( $foo ) { |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
47 | 47 | * @param int $threshold; |
48 | 48 | * @param bool $re; Re |
49 | 49 | */ |
50 | - public function __construct($threshold, $re) { |
|
50 | + public function __construct( $threshold, $re ) { |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | * |
56 | 56 | * @return void |
57 | 57 | */ |
58 | - public function ignoreMe(Foo $foo) { |
|
58 | + public function ignoreMe( Foo $foo ) { |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | } |
@@ -3,8 +3,6 @@ |
||
3 | 3 | namespace Fixtures\NoInlineFullyQualifiedClassNameSniff\Input; |
4 | 4 | |
5 | 5 | use Php\Foo\Bar; |
6 | -use DateTime; |
|
7 | -use X\Y\Z; |
|
8 | 6 | use Faa\SomeTrait; |
9 | 7 | |
10 | 8 | class TestClass1Input extends \Foo\Bar implements \Bar\Baz, \Bar\Xxx { |
@@ -15,12 +15,12 @@ |
||
15 | 15 | * @return void |
16 | 16 | */ |
17 | 17 | public function fixMe() { |
18 | - $a = function () use ($x) { |
|
18 | + $a = function() use ( $x ) { |
|
19 | 19 | return $x; |
20 | 20 | }; |
21 | 21 | |
22 | - $x = new \Event\Event('Controller.initialize'); |
|
23 | - if ($x) { |
|
22 | + $x = new \Event\Event( 'Controller.initialize' ); |
|
23 | + if ( $x ) { |
|
24 | 24 | throw new \Exception(); |
25 | 25 | } else { |
26 | 26 | throw new \Exception(); |
@@ -10,7 +10,7 @@ |
||
10 | 10 | /** |
11 | 11 | * Registers the tokens that this sniff wants to listen for. |
12 | 12 | * |
13 | - * @return int[] |
|
13 | + * @return integer[] |
|
14 | 14 | */ |
15 | 15 | public function register() { |
16 | 16 | return [ T_ARRAY ]; |
@@ -4,7 +4,6 @@ |
||
4 | 4 | |
5 | 5 | use PHP_CodeSniffer_File; |
6 | 6 | use PHP_CodeSniffer_Sniff; |
7 | -use PHP_CodeSniffer_Tokens; |
|
8 | 7 | |
9 | 8 | class ForceShortArraysSniff implements PHP_CodeSniffer_Sniff { |
10 | 9 | /** |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | /** |
33 | 33 | * Returns the token types that this sniff is interested in. |
34 | 34 | * |
35 | - * @return int[] |
|
35 | + * @return integer[] |
|
36 | 36 | */ |
37 | 37 | public function register() { |
38 | 38 | return array(T_OPEN_TAG); |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @param int $stackPtr The position in the stack where |
46 | 46 | * the token was found. |
47 | 47 | * |
48 | - * @return void |
|
48 | + * @return null|integer |
|
49 | 49 | * @throws PHP_CodeSniffer_Exception If jslint.js could not be run |
50 | 50 | */ |
51 | 51 | public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | * @return int[] |
36 | 36 | */ |
37 | 37 | public function register() { |
38 | - return array(T_OPEN_TAG); |
|
38 | + return array( T_OPEN_TAG ); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | // Attempt to autodetect. |
60 | 60 | $candidates = glob( '.eslintrc{.js,.yaml,.yml,.json}', GLOB_BRACE ); |
61 | 61 | if ( ! empty( $candidates ) ) { |
62 | - $config_file = $candidates[0]; |
|
62 | + $config_file = $candidates[ 0 ]; |
|
63 | 63 | } else { |
64 | 64 | $config_file = static::DEFAULT_CONFIG; |
65 | 65 | } |
@@ -84,11 +84,11 @@ discard block |
||
84 | 84 | $process = proc_open( $cmd, $descriptors, $pipes ); |
85 | 85 | |
86 | 86 | // Ignore stdin. |
87 | - fclose( $pipes[0] ); |
|
88 | - $stdout = stream_get_contents( $pipes[1] ); |
|
89 | - $stderr = stream_get_contents( $pipes[2] ); |
|
90 | - fclose( $pipes[1] ); |
|
91 | - fclose( $pipes[2] ); |
|
87 | + fclose( $pipes[ 0 ] ); |
|
88 | + $stdout = stream_get_contents( $pipes[ 1 ] ); |
|
89 | + $stderr = stream_get_contents( $pipes[ 2 ] ); |
|
90 | + fclose( $pipes[ 1 ] ); |
|
91 | + fclose( $pipes[ 2 ] ); |
|
92 | 92 | |
93 | 93 | // Close, and start working! |
94 | 94 | $code = proc_close( $process ); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | $phpcsFile->addError( $error, $stackPtr, 'CouldNotStart', [ $stdout ] ); |
102 | 102 | } else { |
103 | 103 | // Data is a list of files, but we only pass a single one. |
104 | - $messages = $data[0]->messages; |
|
104 | + $messages = $data[ 0 ]->messages; |
|
105 | 105 | foreach ( $messages as $error ) { |
106 | 106 | if ( ! empty( $error->fatal ) || $error->severity === 2 ) { |
107 | 107 | $phpcsFile->addErrorOnLine( $error->message, $error->line, $error->ruleId ); |
@@ -113,6 +113,6 @@ discard block |
||
113 | 113 | } |
114 | 114 | |
115 | 115 | // Ignore the rest of the file. |
116 | - return ($phpcsFile->numTokens + 1); |
|
116 | + return ( $phpcsFile->numTokens + 1 ); |
|
117 | 117 | } |
118 | 118 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | /** |
12 | 12 | * Returns an array of tokens this test wants to listen for. |
13 | 13 | * |
14 | - * @return array |
|
14 | + * @return string[] |
|
15 | 15 | */ |
16 | 16 | public function register() { |
17 | 17 | return array( T_NAMESPACE ); |
@@ -30,16 +30,16 @@ discard block |
||
30 | 30 | $tokens = $phpcsFile->getTokens(); |
31 | 31 | $namespace = ''; |
32 | 32 | |
33 | - $name_ptr = $phpcsFile->findNext( T_STRING, 0); |
|
33 | + $name_ptr = $phpcsFile->findNext( T_STRING, 0 ); |
|
34 | 34 | if ( ! $name_ptr ) { |
35 | 35 | // Non-namespaced, skip check. |
36 | 36 | return; |
37 | 37 | } |
38 | 38 | |
39 | 39 | do { |
40 | - $namespace .= $tokens[ $name_ptr ]['content']; |
|
40 | + $namespace .= $tokens[ $name_ptr ][ 'content' ]; |
|
41 | 41 | $name_ptr++; |
42 | - } while ( in_array( $tokens[ $name_ptr ]['code'], [ T_STRING, T_NS_SEPARATOR ] ) ); |
|
42 | + } while ( in_array( $tokens[ $name_ptr ][ 'code' ], [ T_STRING, T_NS_SEPARATOR ] ) ); |
|
43 | 43 | |
44 | 44 | $full = $phpcsFile->getFileName(); |
45 | 45 | $filename = basename( $full ); |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | return; |
57 | 57 | } |
58 | 58 | |
59 | - $inc_position = $matches[0][1]; |
|
59 | + $inc_position = $matches[ 0 ][ 1 ]; |
|
60 | 60 | $after_inc = substr( $directory, $inc_position + strlen( '/inc' ) ); |
61 | 61 | if ( empty( $after_inc ) ) { |
62 | 62 | // Base inc directory, skip checks. |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | /** |
475 | 475 | * Sets the internal CLI object. |
476 | 476 | * |
477 | - * @param object $cli The CLI object controlling the run. |
|
477 | + * @param PHP_CodeSniffer_CLI $cli The CLI object controlling the run. |
|
478 | 478 | * |
479 | 479 | * @return void |
480 | 480 | */ |
@@ -491,7 +491,7 @@ discard block |
||
491 | 491 | * @param string|array $files The files and directories to process. For |
492 | 492 | * directories, each sub directory will also |
493 | 493 | * be traversed for source files. |
494 | - * @param string|array $standards The set of code sniffs we are testing |
|
494 | + * @param string $standards The set of code sniffs we are testing |
|
495 | 495 | * against. |
496 | 496 | * @param array $restrictions The sniff codes to restrict the |
497 | 497 | * violations to. |
@@ -2324,7 +2324,7 @@ discard block |
||
2324 | 2324 | * |
2325 | 2325 | * @param string $standard The name of the coding standard. |
2326 | 2326 | * |
2327 | - * @return string|null |
|
2327 | + * @return string |
|
2328 | 2328 | */ |
2329 | 2329 | public static function getInstalledStandardPath($standard) |
2330 | 2330 | { |
@@ -2493,7 +2493,7 @@ discard block |
||
2493 | 2493 | * |
2494 | 2494 | * @param string $path The path to use. |
2495 | 2495 | * |
2496 | - * @return mixed |
|
2496 | + * @return boolean |
|
2497 | 2497 | */ |
2498 | 2498 | public static function isPharFile($path) |
2499 | 2499 | { |
@@ -2513,7 +2513,7 @@ discard block |
||
2513 | 2513 | * |
2514 | 2514 | * @param string $path The path to use. |
2515 | 2515 | * |
2516 | - * @return mixed |
|
2516 | + * @return string|false |
|
2517 | 2517 | */ |
2518 | 2518 | public static function realpath($path) |
2519 | 2519 | { |
@@ -17,27 +17,27 @@ discard block |
||
17 | 17 | spl_autoload_register(array('PHP_CodeSniffer', 'autoload')); |
18 | 18 | |
19 | 19 | if (class_exists('PHP_CodeSniffer_Exception', true) === false) { |
20 | - throw new Exception('Class PHP_CodeSniffer_Exception not found'); |
|
20 | + throw new Exception('Class PHP_CodeSniffer_Exception not found'); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | if (class_exists('PHP_CodeSniffer_File', true) === false) { |
24 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_File not found'); |
|
24 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_File not found'); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | if (class_exists('PHP_CodeSniffer_Fixer', true) === false) { |
28 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Fixer not found'); |
|
28 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Fixer not found'); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | if (class_exists('PHP_CodeSniffer_Tokens', true) === false) { |
32 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Tokens not found'); |
|
32 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Tokens not found'); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | if (class_exists('PHP_CodeSniffer_CLI', true) === false) { |
36 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CLI not found'); |
|
36 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CLI not found'); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | if (interface_exists('PHP_CodeSniffer_Sniff', true) === false) { |
40 | - throw new PHP_CodeSniffer_Exception('Interface PHP_CodeSniffer_Sniff not found'); |
|
40 | + throw new PHP_CodeSniffer_Exception('Interface PHP_CodeSniffer_Sniff not found'); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -68,2511 +68,2511 @@ discard block |
||
68 | 68 | class PHP_CodeSniffer |
69 | 69 | { |
70 | 70 | |
71 | - /** |
|
72 | - * The current version. |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - const VERSION = '2.7.1'; |
|
77 | - |
|
78 | - /** |
|
79 | - * Package stability; either stable, beta or alpha. |
|
80 | - * |
|
81 | - * @var string |
|
82 | - */ |
|
83 | - const STABILITY = 'stable'; |
|
84 | - |
|
85 | - /** |
|
86 | - * The file or directory that is currently being processed. |
|
87 | - * |
|
88 | - * @var string |
|
89 | - */ |
|
90 | - protected $file = ''; |
|
91 | - |
|
92 | - /** |
|
93 | - * The directories that the processed rulesets are in. |
|
94 | - * |
|
95 | - * This is declared static because it is also used in the |
|
96 | - * autoloader to look for sniffs outside the PHPCS install. |
|
97 | - * This way, standards designed to be installed inside PHPCS can |
|
98 | - * also be used from outside the PHPCS Standards directory. |
|
99 | - * |
|
100 | - * @var string |
|
101 | - */ |
|
102 | - protected static $rulesetDirs = array(); |
|
103 | - |
|
104 | - /** |
|
105 | - * The CLI object controlling the run. |
|
106 | - * |
|
107 | - * @var PHP_CodeSniffer_CLI |
|
108 | - */ |
|
109 | - public $cli = null; |
|
110 | - |
|
111 | - /** |
|
112 | - * The Reporting object controlling report generation. |
|
113 | - * |
|
114 | - * @var PHP_CodeSniffer_Reporting |
|
115 | - */ |
|
116 | - public $reporting = null; |
|
117 | - |
|
118 | - /** |
|
119 | - * An array of sniff objects that are being used to check files. |
|
120 | - * |
|
121 | - * @var array(PHP_CodeSniffer_Sniff) |
|
122 | - */ |
|
123 | - protected $listeners = array(); |
|
124 | - |
|
125 | - /** |
|
126 | - * An array of sniffs that are being used to check files. |
|
127 | - * |
|
128 | - * @var array(string) |
|
129 | - */ |
|
130 | - protected $sniffs = array(); |
|
131 | - |
|
132 | - /** |
|
133 | - * A mapping of sniff codes to fully qualified class names. |
|
134 | - * |
|
135 | - * The key is the sniff code and the value |
|
136 | - * is the fully qualified name of the sniff class. |
|
137 | - * |
|
138 | - * @var array<string, string> |
|
139 | - */ |
|
140 | - public $sniffCodes = array(); |
|
141 | - |
|
142 | - /** |
|
143 | - * The listeners array, indexed by token type. |
|
144 | - * |
|
145 | - * @var array |
|
146 | - */ |
|
147 | - private $_tokenListeners = array(); |
|
148 | - |
|
149 | - /** |
|
150 | - * An array of rules from the ruleset.xml file. |
|
151 | - * |
|
152 | - * It may be empty, indicating that the ruleset does not override |
|
153 | - * any of the default sniff settings. |
|
154 | - * |
|
155 | - * @var array |
|
156 | - */ |
|
157 | - protected $ruleset = array(); |
|
158 | - |
|
159 | - /** |
|
160 | - * An array of patterns to use for skipping files. |
|
161 | - * |
|
162 | - * @var array |
|
163 | - */ |
|
164 | - protected $ignorePatterns = array(); |
|
165 | - |
|
166 | - /** |
|
167 | - * An array of extensions for files we will check. |
|
168 | - * |
|
169 | - * @var array |
|
170 | - */ |
|
171 | - public $allowedFileExtensions = array(); |
|
172 | - |
|
173 | - /** |
|
174 | - * An array of default extensions and associated tokenizers. |
|
175 | - * |
|
176 | - * If no extensions are set, these will be used as the defaults. |
|
177 | - * If extensions are set, these will be used when the correct tokenizer |
|
178 | - * can not be determined, such as when checking a passed filename instead |
|
179 | - * of files in a directory. |
|
180 | - * |
|
181 | - * @var array |
|
182 | - */ |
|
183 | - public $defaultFileExtensions = array( |
|
184 | - 'php' => 'PHP', |
|
185 | - 'inc' => 'PHP', |
|
186 | - 'js' => 'JS', |
|
187 | - 'css' => 'CSS', |
|
188 | - ); |
|
189 | - |
|
190 | - /** |
|
191 | - * An array of variable types for param/var we will check. |
|
192 | - * |
|
193 | - * @var array(string) |
|
194 | - */ |
|
195 | - public static $allowedTypes = array( |
|
196 | - 'array', |
|
197 | - 'boolean', |
|
198 | - 'float', |
|
199 | - 'integer', |
|
200 | - 'mixed', |
|
201 | - 'object', |
|
202 | - 'string', |
|
203 | - 'resource', |
|
204 | - 'callable', |
|
205 | - ); |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * Constructs a PHP_CodeSniffer object. |
|
210 | - * |
|
211 | - * @param int $verbosity The verbosity level. |
|
212 | - * 1: Print progress information. |
|
213 | - * 2: Print tokenizer debug information. |
|
214 | - * 3: Print sniff debug information. |
|
215 | - * @param int $tabWidth The number of spaces each tab represents. |
|
216 | - * If greater than zero, tabs will be replaced |
|
217 | - * by spaces before testing each file. |
|
218 | - * @param string $encoding The charset of the sniffed files. |
|
219 | - * This is important for some reports that output |
|
220 | - * with utf-8 encoding as you don't want it double |
|
221 | - * encoding messages. |
|
222 | - * @param bool $interactive If TRUE, will stop after each file with errors |
|
223 | - * and wait for user input. |
|
224 | - * |
|
225 | - * @see process() |
|
226 | - */ |
|
227 | - public function __construct( |
|
228 | - $verbosity=0, |
|
229 | - $tabWidth=0, |
|
230 | - $encoding='iso-8859-1', |
|
231 | - $interactive=false |
|
232 | - ) { |
|
233 | - if ($verbosity !== null) { |
|
234 | - $this->setVerbosity($verbosity); |
|
235 | - } |
|
236 | - |
|
237 | - if ($tabWidth !== null) { |
|
238 | - $this->setTabWidth($tabWidth); |
|
239 | - } |
|
240 | - |
|
241 | - if ($encoding !== null) { |
|
242 | - $this->setEncoding($encoding); |
|
243 | - } |
|
244 | - |
|
245 | - if ($interactive !== null) { |
|
246 | - $this->setInteractive($interactive); |
|
247 | - } |
|
248 | - |
|
249 | - if (defined('PHPCS_DEFAULT_ERROR_SEV') === false) { |
|
250 | - define('PHPCS_DEFAULT_ERROR_SEV', 5); |
|
251 | - } |
|
252 | - |
|
253 | - if (defined('PHPCS_DEFAULT_WARN_SEV') === false) { |
|
254 | - define('PHPCS_DEFAULT_WARN_SEV', 5); |
|
255 | - } |
|
256 | - |
|
257 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
258 | - define('PHP_CODESNIFFER_CBF', false); |
|
259 | - } |
|
260 | - |
|
261 | - // Set default CLI object in case someone is running us |
|
262 | - // without using the command line script. |
|
263 | - $this->cli = new PHP_CodeSniffer_CLI(); |
|
264 | - $this->cli->errorSeverity = PHPCS_DEFAULT_ERROR_SEV; |
|
265 | - $this->cli->warningSeverity = PHPCS_DEFAULT_WARN_SEV; |
|
266 | - $this->cli->dieOnUnknownArg = false; |
|
267 | - |
|
268 | - $this->reporting = new PHP_CodeSniffer_Reporting(); |
|
269 | - |
|
270 | - }//end __construct() |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * Autoload static method for loading classes and interfaces. |
|
275 | - * |
|
276 | - * @param string $className The name of the class or interface. |
|
277 | - * |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public static function autoload($className) |
|
281 | - { |
|
282 | - if (substr($className, 0, 4) === 'PHP_') { |
|
283 | - $newClassName = substr($className, 4); |
|
284 | - } else { |
|
285 | - $newClassName = $className; |
|
286 | - } |
|
287 | - |
|
288 | - $path = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $newClassName).'.php'; |
|
289 | - |
|
290 | - if (is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.$path) === true) { |
|
291 | - // Check standard file locations based on class name. |
|
292 | - include dirname(__FILE__).DIRECTORY_SEPARATOR.$path; |
|
293 | - return; |
|
294 | - } else { |
|
295 | - // Check for included sniffs. |
|
296 | - $installedPaths = PHP_CodeSniffer::getInstalledStandardPaths(); |
|
297 | - foreach ($installedPaths as $installedPath) { |
|
298 | - if (is_file($installedPath.DIRECTORY_SEPARATOR.$path) === true) { |
|
299 | - include $installedPath.DIRECTORY_SEPARATOR.$path; |
|
300 | - return; |
|
301 | - } |
|
302 | - } |
|
303 | - |
|
304 | - // Check standard file locations based on the loaded rulesets. |
|
305 | - foreach (self::$rulesetDirs as $rulesetDir) { |
|
306 | - if (is_file(dirname($rulesetDir).DIRECTORY_SEPARATOR.$path) === true) { |
|
307 | - include_once dirname($rulesetDir).DIRECTORY_SEPARATOR.$path; |
|
308 | - return; |
|
309 | - } |
|
310 | - } |
|
311 | - }//end if |
|
312 | - |
|
313 | - // Everything else. |
|
314 | - @include $path; |
|
315 | - |
|
316 | - }//end autoload() |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Sets the verbosity level. |
|
321 | - * |
|
322 | - * @param int $verbosity The verbosity level. |
|
323 | - * 1: Print progress information. |
|
324 | - * 2: Print tokenizer debug information. |
|
325 | - * 3: Print sniff debug information. |
|
326 | - * |
|
327 | - * @return void |
|
328 | - */ |
|
329 | - public function setVerbosity($verbosity) |
|
330 | - { |
|
331 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
332 | - define('PHP_CODESNIFFER_VERBOSITY', $verbosity); |
|
333 | - } |
|
334 | - |
|
335 | - }//end setVerbosity() |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Sets the tab width. |
|
340 | - * |
|
341 | - * @param int $tabWidth The number of spaces each tab represents. |
|
342 | - * If greater than zero, tabs will be replaced |
|
343 | - * by spaces before testing each file. |
|
344 | - * |
|
345 | - * @return void |
|
346 | - */ |
|
347 | - public function setTabWidth($tabWidth) |
|
348 | - { |
|
349 | - if (defined('PHP_CODESNIFFER_TAB_WIDTH') === false) { |
|
350 | - define('PHP_CODESNIFFER_TAB_WIDTH', $tabWidth); |
|
351 | - } |
|
352 | - |
|
353 | - }//end setTabWidth() |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Sets the encoding. |
|
358 | - * |
|
359 | - * @param string $encoding The charset of the sniffed files. |
|
360 | - * This is important for some reports that output |
|
361 | - * with utf-8 encoding as you don't want it double |
|
362 | - * encoding messages. |
|
363 | - * |
|
364 | - * @return void |
|
365 | - */ |
|
366 | - public function setEncoding($encoding) |
|
367 | - { |
|
368 | - if (defined('PHP_CODESNIFFER_ENCODING') === false) { |
|
369 | - define('PHP_CODESNIFFER_ENCODING', $encoding); |
|
370 | - } |
|
371 | - |
|
372 | - }//end setEncoding() |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Sets the interactive flag. |
|
377 | - * |
|
378 | - * @param bool $interactive If TRUE, will stop after each file with errors |
|
379 | - * and wait for user input. |
|
380 | - * |
|
381 | - * @return void |
|
382 | - */ |
|
383 | - public function setInteractive($interactive) |
|
384 | - { |
|
385 | - if (defined('PHP_CODESNIFFER_INTERACTIVE') === false) { |
|
386 | - define('PHP_CODESNIFFER_INTERACTIVE', $interactive); |
|
387 | - } |
|
388 | - |
|
389 | - }//end setInteractive() |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Sets an array of file extensions that we will allow checking of. |
|
394 | - * |
|
395 | - * If the extension is one of the defaults, a specific tokenizer |
|
396 | - * will be used. Otherwise, the PHP tokenizer will be used for |
|
397 | - * all extensions passed. |
|
398 | - * |
|
399 | - * @param array $extensions An array of file extensions. |
|
400 | - * |
|
401 | - * @return void |
|
402 | - */ |
|
403 | - public function setAllowedFileExtensions(array $extensions) |
|
404 | - { |
|
405 | - $newExtensions = array(); |
|
406 | - foreach ($extensions as $ext) { |
|
407 | - $slash = strpos($ext, '/'); |
|
408 | - if ($slash !== false) { |
|
409 | - // They specified the tokenizer too. |
|
410 | - list($ext, $tokenizer) = explode('/', $ext); |
|
411 | - $newExtensions[$ext] = strtoupper($tokenizer); |
|
412 | - continue; |
|
413 | - } |
|
414 | - |
|
415 | - if (isset($this->allowedFileExtensions[$ext]) === true) { |
|
416 | - $newExtensions[$ext] = $this->allowedFileExtensions[$ext]; |
|
417 | - } else if (isset($this->defaultFileExtensions[$ext]) === true) { |
|
418 | - $newExtensions[$ext] = $this->defaultFileExtensions[$ext]; |
|
419 | - } else { |
|
420 | - $newExtensions[$ext] = 'PHP'; |
|
421 | - } |
|
422 | - } |
|
423 | - |
|
424 | - $this->allowedFileExtensions = $newExtensions; |
|
425 | - |
|
426 | - }//end setAllowedFileExtensions() |
|
427 | - |
|
428 | - |
|
429 | - /** |
|
430 | - * Sets an array of ignore patterns that we use to skip files and folders. |
|
431 | - * |
|
432 | - * Patterns are not case sensitive. |
|
433 | - * |
|
434 | - * @param array $patterns An array of ignore patterns. The pattern is the key |
|
435 | - * and the value is either "absolute" or "relative", |
|
436 | - * depending on how the pattern should be applied to a |
|
437 | - * file path. |
|
438 | - * |
|
439 | - * @return void |
|
440 | - */ |
|
441 | - public function setIgnorePatterns(array $patterns) |
|
442 | - { |
|
443 | - $this->ignorePatterns = $patterns; |
|
444 | - |
|
445 | - }//end setIgnorePatterns() |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * Gets the array of ignore patterns. |
|
450 | - * |
|
451 | - * Optionally takes a listener to get ignore patterns specified |
|
452 | - * for that sniff only. |
|
453 | - * |
|
454 | - * @param string $listener The listener to get patterns for. If NULL, all |
|
455 | - * patterns are returned. |
|
456 | - * |
|
457 | - * @return array |
|
458 | - */ |
|
459 | - public function getIgnorePatterns($listener=null) |
|
460 | - { |
|
461 | - if ($listener === null) { |
|
462 | - return $this->ignorePatterns; |
|
463 | - } |
|
464 | - |
|
465 | - if (isset($this->ignorePatterns[$listener]) === true) { |
|
466 | - return $this->ignorePatterns[$listener]; |
|
467 | - } |
|
468 | - |
|
469 | - return array(); |
|
470 | - |
|
471 | - }//end getIgnorePatterns() |
|
472 | - |
|
473 | - |
|
474 | - /** |
|
475 | - * Sets the internal CLI object. |
|
476 | - * |
|
477 | - * @param object $cli The CLI object controlling the run. |
|
478 | - * |
|
479 | - * @return void |
|
480 | - */ |
|
481 | - public function setCli($cli) |
|
482 | - { |
|
483 | - $this->cli = $cli; |
|
484 | - |
|
485 | - }//end setCli() |
|
486 | - |
|
487 | - |
|
488 | - /** |
|
489 | - * Start a PHP_CodeSniffer run. |
|
490 | - * |
|
491 | - * @param string|array $files The files and directories to process. For |
|
492 | - * directories, each sub directory will also |
|
493 | - * be traversed for source files. |
|
494 | - * @param string|array $standards The set of code sniffs we are testing |
|
495 | - * against. |
|
496 | - * @param array $restrictions The sniff codes to restrict the |
|
497 | - * violations to. |
|
498 | - * @param boolean $local If true, don't recurse into directories. |
|
499 | - * |
|
500 | - * @return void |
|
501 | - */ |
|
502 | - public function process($files, $standards, array $restrictions=array(), $local=false) |
|
503 | - { |
|
504 | - $files = (array) $files; |
|
505 | - $this->initStandard($standards, $restrictions); |
|
506 | - $this->processFiles($files, $local); |
|
507 | - |
|
508 | - }//end process() |
|
509 | - |
|
510 | - |
|
511 | - /** |
|
512 | - * Initialise the standard that the run will use. |
|
513 | - * |
|
514 | - * @param string|array $standards The set of code sniffs we are testing |
|
515 | - * against. |
|
516 | - * @param array $restrictions The sniff codes to restrict the testing to. |
|
517 | - * @param array $exclusions The sniff codes to exclude from testing. |
|
518 | - * |
|
519 | - * @return void |
|
520 | - */ |
|
521 | - public function initStandard($standards, array $restrictions=array(), array $exclusions=array()) |
|
522 | - { |
|
523 | - $standards = (array) $standards; |
|
524 | - |
|
525 | - // Reset the members. |
|
526 | - $this->listeners = array(); |
|
527 | - $this->sniffs = array(); |
|
528 | - $this->ruleset = array(); |
|
529 | - $this->_tokenListeners = array(); |
|
530 | - self::$rulesetDirs = array(); |
|
531 | - |
|
532 | - // Ensure this option is enabled or else line endings will not always |
|
533 | - // be detected properly for files created on a Mac with the /r line ending. |
|
534 | - ini_set('auto_detect_line_endings', true); |
|
535 | - |
|
536 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) { |
|
537 | - // Should be one standard and one sniff being tested at a time. |
|
538 | - $installed = $this->getInstalledStandardPath($standards[0]); |
|
539 | - if ($installed !== null) { |
|
540 | - $standard = $installed; |
|
541 | - } else { |
|
542 | - $standard = self::realpath($standards[0]); |
|
543 | - if (is_dir($standard) === true |
|
544 | - && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
545 | - ) { |
|
546 | - $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
547 | - } |
|
548 | - } |
|
549 | - |
|
550 | - $sniffs = $this->_expandRulesetReference($restrictions[0], dirname($standard)); |
|
551 | - } else { |
|
552 | - $sniffs = array(); |
|
553 | - foreach ($standards as $standard) { |
|
554 | - $installed = $this->getInstalledStandardPath($standard); |
|
555 | - if ($installed !== null) { |
|
556 | - $standard = $installed; |
|
557 | - } else { |
|
558 | - $standard = self::realpath($standard); |
|
559 | - if (is_dir($standard) === true |
|
560 | - && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
561 | - ) { |
|
562 | - $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
563 | - } |
|
564 | - } |
|
565 | - |
|
566 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
567 | - $ruleset = simplexml_load_string(file_get_contents($standard)); |
|
568 | - if ($ruleset !== false) { |
|
569 | - $standardName = (string) $ruleset['name']; |
|
570 | - } |
|
571 | - |
|
572 | - echo "Registering sniffs in the $standardName standard... "; |
|
573 | - if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) { |
|
574 | - echo PHP_EOL; |
|
575 | - } |
|
576 | - } |
|
577 | - |
|
578 | - $sniffs = array_merge($sniffs, $this->processRuleset($standard)); |
|
579 | - }//end foreach |
|
580 | - }//end if |
|
581 | - |
|
582 | - $sniffRestrictions = array(); |
|
583 | - foreach ($restrictions as $sniffCode) { |
|
584 | - $parts = explode('.', strtolower($sniffCode)); |
|
585 | - $sniffRestrictions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
586 | - } |
|
587 | - |
|
588 | - $sniffExclusions = array(); |
|
589 | - foreach ($exclusions as $sniffCode) { |
|
590 | - $parts = explode('.', strtolower($sniffCode)); |
|
591 | - $sniffExclusions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
592 | - } |
|
593 | - |
|
594 | - $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions); |
|
595 | - $this->populateTokenListeners(); |
|
596 | - |
|
597 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
598 | - $numSniffs = count($this->sniffs); |
|
599 | - echo "DONE ($numSniffs sniffs registered)".PHP_EOL; |
|
600 | - } |
|
601 | - |
|
602 | - }//end initStandard() |
|
603 | - |
|
604 | - |
|
605 | - /** |
|
606 | - * Processes the files/directories that PHP_CodeSniffer was constructed with. |
|
607 | - * |
|
608 | - * @param string|array $files The files and directories to process. For |
|
609 | - * directories, each sub directory will also |
|
610 | - * be traversed for source files. |
|
611 | - * @param boolean $local If true, don't recurse into directories. |
|
612 | - * |
|
613 | - * @return void |
|
614 | - * @throws PHP_CodeSniffer_Exception If files are invalid. |
|
615 | - */ |
|
616 | - public function processFiles($files, $local=false) |
|
617 | - { |
|
618 | - $files = (array) $files; |
|
619 | - $cliValues = $this->cli->getCommandLineValues(); |
|
620 | - $showProgress = $cliValues['showProgress']; |
|
621 | - $useColors = $cliValues['colors']; |
|
622 | - |
|
623 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
624 | - echo 'Creating file list... '; |
|
625 | - } |
|
626 | - |
|
627 | - if (empty($this->allowedFileExtensions) === true) { |
|
628 | - $this->allowedFileExtensions = $this->defaultFileExtensions; |
|
629 | - } |
|
630 | - |
|
631 | - $todo = $this->getFilesToProcess($files, $local); |
|
632 | - $numFiles = count($todo); |
|
633 | - |
|
634 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
635 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
636 | - } |
|
637 | - |
|
638 | - $numProcessed = 0; |
|
639 | - $dots = 0; |
|
640 | - $maxLength = strlen($numFiles); |
|
641 | - $lastDir = ''; |
|
642 | - foreach ($todo as $file) { |
|
643 | - $this->file = $file; |
|
644 | - $currDir = dirname($file); |
|
645 | - if ($lastDir !== $currDir) { |
|
646 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true) { |
|
647 | - echo 'Changing into directory '.$currDir.PHP_EOL; |
|
648 | - } |
|
649 | - |
|
650 | - $lastDir = $currDir; |
|
651 | - } |
|
652 | - |
|
653 | - $phpcsFile = $this->processFile($file, null); |
|
654 | - $numProcessed++; |
|
655 | - |
|
656 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
657 | - || PHP_CODESNIFFER_INTERACTIVE === true |
|
658 | - || $showProgress === false |
|
659 | - ) { |
|
660 | - continue; |
|
661 | - } |
|
662 | - |
|
663 | - // Show progress information. |
|
664 | - if ($phpcsFile === null) { |
|
665 | - echo 'S'; |
|
666 | - } else { |
|
667 | - $errors = $phpcsFile->getErrorCount(); |
|
668 | - $warnings = $phpcsFile->getWarningCount(); |
|
669 | - if ($errors > 0) { |
|
670 | - if ($useColors === true) { |
|
671 | - echo "\033[31m"; |
|
672 | - } |
|
673 | - |
|
674 | - echo 'E'; |
|
675 | - } else if ($warnings > 0) { |
|
676 | - if ($useColors === true) { |
|
677 | - echo "\033[33m"; |
|
678 | - } |
|
679 | - |
|
680 | - echo 'W'; |
|
681 | - } else { |
|
682 | - echo '.'; |
|
683 | - } |
|
684 | - |
|
685 | - if ($useColors === true) { |
|
686 | - echo "\033[0m"; |
|
687 | - } |
|
688 | - }//end if |
|
689 | - |
|
690 | - $dots++; |
|
691 | - if ($dots === 60) { |
|
692 | - $padding = ($maxLength - strlen($numProcessed)); |
|
693 | - echo str_repeat(' ', $padding); |
|
694 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
695 | - echo " $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
696 | - $dots = 0; |
|
697 | - } |
|
698 | - }//end foreach |
|
699 | - |
|
700 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
701 | - && PHP_CODESNIFFER_INTERACTIVE === false |
|
702 | - && $showProgress === true |
|
703 | - ) { |
|
704 | - echo PHP_EOL.PHP_EOL; |
|
705 | - } |
|
706 | - |
|
707 | - }//end processFiles() |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * Processes a single ruleset and returns a list of the sniffs it represents. |
|
712 | - * |
|
713 | - * Rules founds within the ruleset are processed immediately, but sniff classes |
|
714 | - * are not registered by this method. |
|
715 | - * |
|
716 | - * @param string $rulesetPath The path to a ruleset XML file. |
|
717 | - * @param int $depth How many nested processing steps we are in. This |
|
718 | - * is only used for debug output. |
|
719 | - * |
|
720 | - * @return array |
|
721 | - * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid. |
|
722 | - */ |
|
723 | - public function processRuleset($rulesetPath, $depth=0) |
|
724 | - { |
|
725 | - $rulesetPath = self::realpath($rulesetPath); |
|
726 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
727 | - echo str_repeat("\t", $depth); |
|
728 | - echo "Processing ruleset $rulesetPath".PHP_EOL; |
|
729 | - } |
|
730 | - |
|
731 | - $ruleset = simplexml_load_string(file_get_contents($rulesetPath)); |
|
732 | - if ($ruleset === false) { |
|
733 | - throw new PHP_CodeSniffer_Exception("Ruleset $rulesetPath is not valid"); |
|
734 | - } |
|
735 | - |
|
736 | - $ownSniffs = array(); |
|
737 | - $includedSniffs = array(); |
|
738 | - $excludedSniffs = array(); |
|
739 | - $cliValues = $this->cli->getCommandLineValues(); |
|
740 | - |
|
741 | - $rulesetDir = dirname($rulesetPath); |
|
742 | - self::$rulesetDirs[] = $rulesetDir; |
|
743 | - |
|
744 | - if (is_dir($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs') === true) { |
|
745 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
746 | - echo str_repeat("\t", $depth); |
|
747 | - echo "\tAdding sniff files from \"/.../".basename($rulesetDir)."/Sniffs/\" directory".PHP_EOL; |
|
748 | - } |
|
749 | - |
|
750 | - $ownSniffs = $this->_expandSniffDirectory($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs', $depth); |
|
751 | - } |
|
752 | - |
|
753 | - // Process custom sniff config settings. |
|
754 | - foreach ($ruleset->{'config'} as $config) { |
|
755 | - if ($this->_shouldProcessElement($config) === false) { |
|
756 | - continue; |
|
757 | - } |
|
758 | - |
|
759 | - $this->setConfigData((string) $config['name'], (string) $config['value'], true); |
|
760 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
761 | - echo str_repeat("\t", $depth); |
|
762 | - echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL; |
|
763 | - } |
|
764 | - } |
|
765 | - |
|
766 | - foreach ($ruleset->rule as $rule) { |
|
767 | - if (isset($rule['ref']) === false |
|
768 | - || $this->_shouldProcessElement($rule) === false |
|
769 | - ) { |
|
770 | - continue; |
|
771 | - } |
|
772 | - |
|
773 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
774 | - echo str_repeat("\t", $depth); |
|
775 | - echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL; |
|
776 | - } |
|
777 | - |
|
778 | - $includedSniffs = array_merge( |
|
779 | - $includedSniffs, |
|
780 | - $this->_expandRulesetReference($rule['ref'], $rulesetDir, $depth) |
|
781 | - ); |
|
782 | - |
|
783 | - if (isset($rule->exclude) === true) { |
|
784 | - foreach ($rule->exclude as $exclude) { |
|
785 | - if ($this->_shouldProcessElement($exclude) === false) { |
|
786 | - continue; |
|
787 | - } |
|
788 | - |
|
789 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
790 | - echo str_repeat("\t", $depth); |
|
791 | - echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL; |
|
792 | - } |
|
793 | - |
|
794 | - // Check if a single code is being excluded, which is a shortcut |
|
795 | - // for setting the severity of the message to 0. |
|
796 | - $parts = explode('.', $exclude['name']); |
|
797 | - if (count($parts) === 4) { |
|
798 | - $this->ruleset[(string) $exclude['name']]['severity'] = 0; |
|
799 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
800 | - echo str_repeat("\t", $depth); |
|
801 | - echo "\t\t=> severity set to 0".PHP_EOL; |
|
802 | - } |
|
803 | - } else { |
|
804 | - $excludedSniffs = array_merge( |
|
805 | - $excludedSniffs, |
|
806 | - $this->_expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1)) |
|
807 | - ); |
|
808 | - } |
|
809 | - }//end foreach |
|
810 | - }//end if |
|
811 | - |
|
812 | - $this->_processRule($rule, $depth); |
|
813 | - }//end foreach |
|
814 | - |
|
815 | - // Process custom command line arguments. |
|
816 | - $cliArgs = array(); |
|
817 | - foreach ($ruleset->{'arg'} as $arg) { |
|
818 | - if ($this->_shouldProcessElement($arg) === false) { |
|
819 | - continue; |
|
820 | - } |
|
821 | - |
|
822 | - if (isset($arg['name']) === true) { |
|
823 | - $argString = '--'.(string) $arg['name']; |
|
824 | - if (isset($arg['value']) === true) { |
|
825 | - $argString .= '='.(string) $arg['value']; |
|
826 | - } |
|
827 | - } else { |
|
828 | - $argString = '-'.(string) $arg['value']; |
|
829 | - } |
|
830 | - |
|
831 | - $cliArgs[] = $argString; |
|
832 | - |
|
833 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
834 | - echo str_repeat("\t", $depth); |
|
835 | - echo "\t=> set command line value $argString".PHP_EOL; |
|
836 | - } |
|
837 | - }//end foreach |
|
838 | - |
|
839 | - // Set custom php ini values as CLI args. |
|
840 | - foreach ($ruleset->{'ini'} as $arg) { |
|
841 | - if ($this->_shouldProcessElement($arg) === false) { |
|
842 | - continue; |
|
843 | - } |
|
844 | - |
|
845 | - if (isset($arg['name']) === false) { |
|
846 | - continue; |
|
847 | - } |
|
848 | - |
|
849 | - $name = (string) $arg['name']; |
|
850 | - $argString = $name; |
|
851 | - if (isset($arg['value']) === true) { |
|
852 | - $value = (string) $arg['value']; |
|
853 | - $argString .= "=$value"; |
|
854 | - } else { |
|
855 | - $value = 'true'; |
|
856 | - } |
|
857 | - |
|
858 | - $cliArgs[] = '-d'; |
|
859 | - $cliArgs[] = $argString; |
|
860 | - |
|
861 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
862 | - echo str_repeat("\t", $depth); |
|
863 | - echo "\t=> set PHP ini value $name to $value".PHP_EOL; |
|
864 | - } |
|
865 | - }//end foreach |
|
866 | - |
|
867 | - if (empty($cliValues['files']) === true && $cliValues['stdin'] === null) { |
|
868 | - // Process hard-coded file paths. |
|
869 | - foreach ($ruleset->{'file'} as $file) { |
|
870 | - $file = (string) $file; |
|
871 | - $cliArgs[] = $file; |
|
872 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
873 | - echo str_repeat("\t", $depth); |
|
874 | - echo "\t=> added \"$file\" to the file list".PHP_EOL; |
|
875 | - } |
|
876 | - } |
|
877 | - } |
|
878 | - |
|
879 | - if (empty($cliArgs) === false) { |
|
880 | - // Change the directory so all relative paths are worked |
|
881 | - // out based on the location of the ruleset instead of |
|
882 | - // the location of the user. |
|
883 | - $inPhar = self::isPharFile($rulesetDir); |
|
884 | - if ($inPhar === false) { |
|
885 | - $currentDir = getcwd(); |
|
886 | - chdir($rulesetDir); |
|
887 | - } |
|
888 | - |
|
889 | - $this->cli->setCommandLineValues($cliArgs); |
|
890 | - |
|
891 | - if ($inPhar === false) { |
|
892 | - chdir($currentDir); |
|
893 | - } |
|
894 | - } |
|
895 | - |
|
896 | - // Process custom ignore pattern rules. |
|
897 | - foreach ($ruleset->{'exclude-pattern'} as $pattern) { |
|
898 | - if ($this->_shouldProcessElement($pattern) === false) { |
|
899 | - continue; |
|
900 | - } |
|
901 | - |
|
902 | - if (isset($pattern['type']) === false) { |
|
903 | - $pattern['type'] = 'absolute'; |
|
904 | - } |
|
905 | - |
|
906 | - $this->ignorePatterns[(string) $pattern] = (string) $pattern['type']; |
|
907 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
908 | - echo str_repeat("\t", $depth); |
|
909 | - echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
910 | - } |
|
911 | - } |
|
912 | - |
|
913 | - $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs)); |
|
914 | - $excludedSniffs = array_unique($excludedSniffs); |
|
915 | - |
|
916 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
917 | - $included = count($includedSniffs); |
|
918 | - $excluded = count($excludedSniffs); |
|
919 | - echo str_repeat("\t", $depth); |
|
920 | - echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL; |
|
921 | - } |
|
922 | - |
|
923 | - // Merge our own sniff list with our externally included |
|
924 | - // sniff list, but filter out any excluded sniffs. |
|
925 | - $files = array(); |
|
926 | - foreach ($includedSniffs as $sniff) { |
|
927 | - if (in_array($sniff, $excludedSniffs) === true) { |
|
928 | - continue; |
|
929 | - } else { |
|
930 | - $files[] = self::realpath($sniff); |
|
931 | - } |
|
932 | - } |
|
933 | - |
|
934 | - return $files; |
|
935 | - |
|
936 | - }//end processRuleset() |
|
937 | - |
|
938 | - |
|
939 | - /** |
|
940 | - * Expands a directory into a list of sniff files within. |
|
941 | - * |
|
942 | - * @param string $directory The path to a directory. |
|
943 | - * @param int $depth How many nested processing steps we are in. This |
|
944 | - * is only used for debug output. |
|
945 | - * |
|
946 | - * @return array |
|
947 | - */ |
|
948 | - private function _expandSniffDirectory($directory, $depth=0) |
|
949 | - { |
|
950 | - $sniffs = array(); |
|
951 | - |
|
952 | - if (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') === true) { |
|
953 | - $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS); |
|
954 | - } else { |
|
955 | - $rdi = new RecursiveDirectoryIterator($directory); |
|
956 | - } |
|
957 | - |
|
958 | - $di = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD); |
|
959 | - |
|
960 | - $dirLen = strlen($directory); |
|
961 | - |
|
962 | - foreach ($di as $file) { |
|
963 | - $filename = $file->getFilename(); |
|
964 | - |
|
965 | - // Skip hidden files. |
|
966 | - if (substr($filename, 0, 1) === '.') { |
|
967 | - continue; |
|
968 | - } |
|
969 | - |
|
970 | - // We are only interested in PHP and sniff files. |
|
971 | - $fileParts = explode('.', $filename); |
|
972 | - if (array_pop($fileParts) !== 'php') { |
|
973 | - continue; |
|
974 | - } |
|
975 | - |
|
976 | - $basename = basename($filename, '.php'); |
|
977 | - if (substr($basename, -5) !== 'Sniff') { |
|
978 | - continue; |
|
979 | - } |
|
980 | - |
|
981 | - $path = $file->getPathname(); |
|
982 | - |
|
983 | - // Skip files in hidden directories within the Sniffs directory of this |
|
984 | - // standard. We use the offset with strpos() to allow hidden directories |
|
985 | - // before, valid example: |
|
986 | - // /home/foo/.composer/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/... |
|
987 | - if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) { |
|
988 | - continue; |
|
989 | - } |
|
990 | - |
|
991 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
992 | - echo str_repeat("\t", $depth); |
|
993 | - echo "\t\t=> $path".PHP_EOL; |
|
994 | - } |
|
995 | - |
|
996 | - $sniffs[] = $path; |
|
997 | - }//end foreach |
|
998 | - |
|
999 | - return $sniffs; |
|
1000 | - |
|
1001 | - }//end _expandSniffDirectory() |
|
1002 | - |
|
1003 | - |
|
1004 | - /** |
|
1005 | - * Expands a ruleset reference into a list of sniff files. |
|
1006 | - * |
|
1007 | - * @param string $ref The reference from the ruleset XML file. |
|
1008 | - * @param string $rulesetDir The directory of the ruleset XML file, used to |
|
1009 | - * evaluate relative paths. |
|
1010 | - * @param int $depth How many nested processing steps we are in. This |
|
1011 | - * is only used for debug output. |
|
1012 | - * |
|
1013 | - * @return array |
|
1014 | - * @throws PHP_CodeSniffer_Exception If the reference is invalid. |
|
1015 | - */ |
|
1016 | - private function _expandRulesetReference($ref, $rulesetDir, $depth=0) |
|
1017 | - { |
|
1018 | - // Ignore internal sniffs codes as they are used to only |
|
1019 | - // hide and change internal messages. |
|
1020 | - if (substr($ref, 0, 9) === 'Internal.') { |
|
1021 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1022 | - echo str_repeat("\t", $depth); |
|
1023 | - echo "\t\t* ignoring internal sniff code *".PHP_EOL; |
|
1024 | - } |
|
1025 | - |
|
1026 | - return array(); |
|
1027 | - } |
|
1028 | - |
|
1029 | - // As sniffs can't begin with a full stop, assume references in |
|
1030 | - // this format are relative paths and attempt to convert them |
|
1031 | - // to absolute paths. If this fails, let the reference run through |
|
1032 | - // the normal checks and have it fail as normal. |
|
1033 | - if (substr($ref, 0, 1) === '.') { |
|
1034 | - $realpath = self::realpath($rulesetDir.'/'.$ref); |
|
1035 | - if ($realpath !== false) { |
|
1036 | - $ref = $realpath; |
|
1037 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1038 | - echo str_repeat("\t", $depth); |
|
1039 | - echo "\t\t=> $ref".PHP_EOL; |
|
1040 | - } |
|
1041 | - } |
|
1042 | - } |
|
1043 | - |
|
1044 | - // As sniffs can't begin with a tilde, assume references in |
|
1045 | - // this format at relative to the user's home directory. |
|
1046 | - if (substr($ref, 0, 2) === '~/') { |
|
1047 | - $realpath = self::realpath($ref); |
|
1048 | - if ($realpath !== false) { |
|
1049 | - $ref = $realpath; |
|
1050 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1051 | - echo str_repeat("\t", $depth); |
|
1052 | - echo "\t\t=> $ref".PHP_EOL; |
|
1053 | - } |
|
1054 | - } |
|
1055 | - } |
|
1056 | - |
|
1057 | - if (is_file($ref) === true) { |
|
1058 | - if (substr($ref, -9) === 'Sniff.php') { |
|
1059 | - // A single external sniff. |
|
1060 | - self::$rulesetDirs[] = dirname(dirname(dirname($ref))); |
|
1061 | - return array($ref); |
|
1062 | - } |
|
1063 | - } else { |
|
1064 | - // See if this is a whole standard being referenced. |
|
1065 | - $path = $this->getInstalledStandardPath($ref); |
|
1066 | - if (self::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { |
|
1067 | - // If the ruleset exists inside the phar file, use it. |
|
1068 | - if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1069 | - $path = $path.DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
1070 | - } else { |
|
1071 | - $path = null; |
|
1072 | - } |
|
1073 | - } |
|
1074 | - |
|
1075 | - if ($path !== null) { |
|
1076 | - $ref = $path; |
|
1077 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1078 | - echo str_repeat("\t", $depth); |
|
1079 | - echo "\t\t=> $ref".PHP_EOL; |
|
1080 | - } |
|
1081 | - } else if (is_dir($ref) === false) { |
|
1082 | - // Work out the sniff path. |
|
1083 | - $sepPos = strpos($ref, DIRECTORY_SEPARATOR); |
|
1084 | - if ($sepPos !== false) { |
|
1085 | - $stdName = substr($ref, 0, $sepPos); |
|
1086 | - $path = substr($ref, $sepPos); |
|
1087 | - } else { |
|
1088 | - $parts = explode('.', $ref); |
|
1089 | - $stdName = $parts[0]; |
|
1090 | - if (count($parts) === 1) { |
|
1091 | - // A whole standard? |
|
1092 | - $path = ''; |
|
1093 | - } else if (count($parts) === 2) { |
|
1094 | - // A directory of sniffs? |
|
1095 | - $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1]; |
|
1096 | - } else { |
|
1097 | - // A single sniff? |
|
1098 | - $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php'; |
|
1099 | - } |
|
1100 | - } |
|
1101 | - |
|
1102 | - $newRef = false; |
|
1103 | - $stdPath = $this->getInstalledStandardPath($stdName); |
|
1104 | - if ($stdPath !== null && $path !== '') { |
|
1105 | - if (self::isPharFile($stdPath) === true |
|
1106 | - && strpos($stdPath, 'ruleset.xml') === false |
|
1107 | - ) { |
|
1108 | - // Phar files can only return the directory, |
|
1109 | - // since ruleset can be omitted if building one standard. |
|
1110 | - $newRef = self::realpath($stdPath.$path); |
|
1111 | - } else { |
|
1112 | - $newRef = self::realpath(dirname($stdPath).$path); |
|
1113 | - } |
|
1114 | - } |
|
1115 | - |
|
1116 | - if ($newRef === false) { |
|
1117 | - // The sniff is not locally installed, so check if it is being |
|
1118 | - // referenced as a remote sniff outside the install. We do this |
|
1119 | - // by looking through all directories where we have found ruleset |
|
1120 | - // files before, looking for ones for this particular standard, |
|
1121 | - // and seeing if it is in there. |
|
1122 | - foreach (self::$rulesetDirs as $dir) { |
|
1123 | - if (strtolower(basename($dir)) !== strtolower($stdName)) { |
|
1124 | - continue; |
|
1125 | - } |
|
1126 | - |
|
1127 | - $newRef = self::realpath($dir.$path); |
|
1128 | - |
|
1129 | - if ($newRef !== false) { |
|
1130 | - $ref = $newRef; |
|
1131 | - } |
|
1132 | - } |
|
1133 | - } else { |
|
1134 | - $ref = $newRef; |
|
1135 | - } |
|
1136 | - |
|
1137 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1138 | - echo str_repeat("\t", $depth); |
|
1139 | - echo "\t\t=> $ref".PHP_EOL; |
|
1140 | - } |
|
1141 | - }//end if |
|
1142 | - }//end if |
|
1143 | - |
|
1144 | - if (is_dir($ref) === true) { |
|
1145 | - if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1146 | - // We are referencing an external coding standard. |
|
1147 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1148 | - echo str_repeat("\t", $depth); |
|
1149 | - echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL; |
|
1150 | - } |
|
1151 | - |
|
1152 | - return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2)); |
|
1153 | - } else { |
|
1154 | - // We are referencing a whole directory of sniffs. |
|
1155 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1156 | - echo str_repeat("\t", $depth); |
|
1157 | - echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL; |
|
1158 | - echo str_repeat("\t", $depth); |
|
1159 | - echo "\t\tAdding sniff files from directory".PHP_EOL; |
|
1160 | - } |
|
1161 | - |
|
1162 | - return $this->_expandSniffDirectory($ref, ($depth + 1)); |
|
1163 | - } |
|
1164 | - } else { |
|
1165 | - if (is_file($ref) === false) { |
|
1166 | - $error = "Referenced sniff \"$ref\" does not exist"; |
|
1167 | - throw new PHP_CodeSniffer_Exception($error); |
|
1168 | - } |
|
1169 | - |
|
1170 | - if (substr($ref, -9) === 'Sniff.php') { |
|
1171 | - // A single sniff. |
|
1172 | - return array($ref); |
|
1173 | - } else { |
|
1174 | - // Assume an external ruleset.xml file. |
|
1175 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1176 | - echo str_repeat("\t", $depth); |
|
1177 | - echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL; |
|
1178 | - } |
|
1179 | - |
|
1180 | - return $this->processRuleset($ref, ($depth + 2)); |
|
1181 | - } |
|
1182 | - }//end if |
|
1183 | - |
|
1184 | - }//end _expandRulesetReference() |
|
1185 | - |
|
1186 | - |
|
1187 | - /** |
|
1188 | - * Processes a rule from a ruleset XML file, overriding built-in defaults. |
|
1189 | - * |
|
1190 | - * @param SimpleXMLElement $rule The rule object from a ruleset XML file. |
|
1191 | - * @param int $depth How many nested processing steps we are in. |
|
1192 | - * This is only used for debug output. |
|
1193 | - * |
|
1194 | - * @return void |
|
1195 | - */ |
|
1196 | - private function _processRule($rule, $depth=0) |
|
1197 | - { |
|
1198 | - $code = (string) $rule['ref']; |
|
1199 | - |
|
1200 | - // Custom severity. |
|
1201 | - if (isset($rule->severity) === true |
|
1202 | - && $this->_shouldProcessElement($rule->severity) === true |
|
1203 | - ) { |
|
1204 | - if (isset($this->ruleset[$code]) === false) { |
|
1205 | - $this->ruleset[$code] = array(); |
|
1206 | - } |
|
1207 | - |
|
1208 | - $this->ruleset[$code]['severity'] = (int) $rule->severity; |
|
1209 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1210 | - echo str_repeat("\t", $depth); |
|
1211 | - echo "\t\t=> severity set to ".(int) $rule->severity.PHP_EOL; |
|
1212 | - } |
|
1213 | - } |
|
1214 | - |
|
1215 | - // Custom message type. |
|
1216 | - if (isset($rule->type) === true |
|
1217 | - && $this->_shouldProcessElement($rule->type) === true |
|
1218 | - ) { |
|
1219 | - if (isset($this->ruleset[$code]) === false) { |
|
1220 | - $this->ruleset[$code] = array(); |
|
1221 | - } |
|
1222 | - |
|
1223 | - $this->ruleset[$code]['type'] = (string) $rule->type; |
|
1224 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1225 | - echo str_repeat("\t", $depth); |
|
1226 | - echo "\t\t=> message type set to ".(string) $rule->type.PHP_EOL; |
|
1227 | - } |
|
1228 | - } |
|
1229 | - |
|
1230 | - // Custom message. |
|
1231 | - if (isset($rule->message) === true |
|
1232 | - && $this->_shouldProcessElement($rule->message) === true |
|
1233 | - ) { |
|
1234 | - if (isset($this->ruleset[$code]) === false) { |
|
1235 | - $this->ruleset[$code] = array(); |
|
1236 | - } |
|
1237 | - |
|
1238 | - $this->ruleset[$code]['message'] = (string) $rule->message; |
|
1239 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1240 | - echo str_repeat("\t", $depth); |
|
1241 | - echo "\t\t=> message set to ".(string) $rule->message.PHP_EOL; |
|
1242 | - } |
|
1243 | - } |
|
1244 | - |
|
1245 | - // Custom properties. |
|
1246 | - if (isset($rule->properties) === true |
|
1247 | - && $this->_shouldProcessElement($rule->properties) === true |
|
1248 | - ) { |
|
1249 | - foreach ($rule->properties->property as $prop) { |
|
1250 | - if ($this->_shouldProcessElement($prop) === false) { |
|
1251 | - continue; |
|
1252 | - } |
|
1253 | - |
|
1254 | - if (isset($this->ruleset[$code]) === false) { |
|
1255 | - $this->ruleset[$code] = array( |
|
1256 | - 'properties' => array(), |
|
1257 | - ); |
|
1258 | - } else if (isset($this->ruleset[$code]['properties']) === false) { |
|
1259 | - $this->ruleset[$code]['properties'] = array(); |
|
1260 | - } |
|
1261 | - |
|
1262 | - $name = (string) $prop['name']; |
|
1263 | - if (isset($prop['type']) === true |
|
1264 | - && (string) $prop['type'] === 'array' |
|
1265 | - ) { |
|
1266 | - $value = (string) $prop['value']; |
|
1267 | - $values = array(); |
|
1268 | - foreach (explode(',', $value) as $val) { |
|
1269 | - $v = ''; |
|
1270 | - |
|
1271 | - list($k,$v) = explode('=>', $val.'=>'); |
|
1272 | - if ($v !== '') { |
|
1273 | - $values[$k] = $v; |
|
1274 | - } else { |
|
1275 | - $values[] = $k; |
|
1276 | - } |
|
1277 | - } |
|
1278 | - |
|
1279 | - $this->ruleset[$code]['properties'][$name] = $values; |
|
1280 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1281 | - echo str_repeat("\t", $depth); |
|
1282 | - echo "\t\t=> array property \"$name\" set to \"$value\"".PHP_EOL; |
|
1283 | - } |
|
1284 | - } else { |
|
1285 | - $this->ruleset[$code]['properties'][$name] = (string) $prop['value']; |
|
1286 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1287 | - echo str_repeat("\t", $depth); |
|
1288 | - echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'.PHP_EOL; |
|
1289 | - } |
|
1290 | - }//end if |
|
1291 | - }//end foreach |
|
1292 | - }//end if |
|
1293 | - |
|
1294 | - // Ignore patterns. |
|
1295 | - foreach ($rule->{'exclude-pattern'} as $pattern) { |
|
1296 | - if ($this->_shouldProcessElement($pattern) === false) { |
|
1297 | - continue; |
|
1298 | - } |
|
1299 | - |
|
1300 | - if (isset($this->ignorePatterns[$code]) === false) { |
|
1301 | - $this->ignorePatterns[$code] = array(); |
|
1302 | - } |
|
1303 | - |
|
1304 | - if (isset($pattern['type']) === false) { |
|
1305 | - $pattern['type'] = 'absolute'; |
|
1306 | - } |
|
1307 | - |
|
1308 | - $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type']; |
|
1309 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1310 | - echo str_repeat("\t", $depth); |
|
1311 | - echo "\t\t=> added sniff-specific ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
1312 | - } |
|
1313 | - } |
|
1314 | - |
|
1315 | - }//end _processRule() |
|
1316 | - |
|
1317 | - |
|
1318 | - /** |
|
1319 | - * Determine if an element should be processed or ignored. |
|
1320 | - * |
|
1321 | - * @param SimpleXMLElement $element An object from a ruleset XML file. |
|
1322 | - * @param int $depth How many nested processing steps we are in. |
|
1323 | - * This is only used for debug output. |
|
1324 | - * |
|
1325 | - * @return bool |
|
1326 | - */ |
|
1327 | - private function _shouldProcessElement($element, $depth=0) |
|
1328 | - { |
|
1329 | - if (isset($element['phpcbf-only']) === false |
|
1330 | - && isset($element['phpcs-only']) === false |
|
1331 | - ) { |
|
1332 | - // No exceptions are being made. |
|
1333 | - return true; |
|
1334 | - } |
|
1335 | - |
|
1336 | - if (PHP_CODESNIFFER_CBF === true |
|
1337 | - && isset($element['phpcbf-only']) === true |
|
1338 | - && (string) $element['phpcbf-only'] === 'true' |
|
1339 | - ) { |
|
1340 | - return true; |
|
1341 | - } |
|
1342 | - |
|
1343 | - if (PHP_CODESNIFFER_CBF === false |
|
1344 | - && isset($element['phpcs-only']) === true |
|
1345 | - && (string) $element['phpcs-only'] === 'true' |
|
1346 | - ) { |
|
1347 | - return true; |
|
1348 | - } |
|
1349 | - |
|
1350 | - return false; |
|
1351 | - |
|
1352 | - }//end _shouldProcessElement() |
|
1353 | - |
|
1354 | - |
|
1355 | - /** |
|
1356 | - * Loads and stores sniffs objects used for sniffing files. |
|
1357 | - * |
|
1358 | - * @param array $files Paths to the sniff files to register. |
|
1359 | - * @param array $restrictions The sniff class names to restrict the allowed |
|
1360 | - * listeners to. |
|
1361 | - * @param array $exclusions The sniff class names to exclude from the |
|
1362 | - * listeners list. |
|
1363 | - * |
|
1364 | - * @return void |
|
1365 | - * @throws PHP_CodeSniffer_Exception If a sniff file path is invalid. |
|
1366 | - */ |
|
1367 | - public function registerSniffs($files, $restrictions, $exclusions) |
|
1368 | - { |
|
1369 | - $listeners = array(); |
|
1370 | - |
|
1371 | - foreach ($files as $file) { |
|
1372 | - // Work out where the position of /StandardName/Sniffs/... is |
|
1373 | - // so we can determine what the class will be called. |
|
1374 | - $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR); |
|
1375 | - if ($sniffPos === false) { |
|
1376 | - continue; |
|
1377 | - } |
|
1378 | - |
|
1379 | - $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR); |
|
1380 | - if ($slashPos === false) { |
|
1381 | - continue; |
|
1382 | - } |
|
1383 | - |
|
1384 | - $className = substr($file, ($slashPos + 1)); |
|
1385 | - |
|
1386 | - if (substr_count($className, DIRECTORY_SEPARATOR) !== 3) { |
|
1387 | - throw new PHP_CodeSniffer_Exception("Sniff file $className is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory"); |
|
1388 | - } |
|
1389 | - |
|
1390 | - $className = substr($className, 0, -4); |
|
1391 | - $className = str_replace(DIRECTORY_SEPARATOR, '_', $className); |
|
1392 | - |
|
1393 | - // If they have specified a list of sniffs to restrict to, check |
|
1394 | - // to see if this sniff is allowed. |
|
1395 | - if (empty($restrictions) === false |
|
1396 | - && in_array(strtolower($className), $restrictions) === false |
|
1397 | - ) { |
|
1398 | - continue; |
|
1399 | - } |
|
1400 | - |
|
1401 | - // If they have specified a list of sniffs to exclude, check |
|
1402 | - // to see if this sniff is allowed. |
|
1403 | - if (empty($exclusions) === false |
|
1404 | - && in_array(strtolower($className), $exclusions) === true |
|
1405 | - ) { |
|
1406 | - continue; |
|
1407 | - } |
|
1408 | - |
|
1409 | - include_once $file; |
|
1410 | - |
|
1411 | - // Support the use of PHP namespaces. If the class name we included |
|
1412 | - // contains namespace separators instead of underscores, use this as the |
|
1413 | - // class name from now on. |
|
1414 | - $classNameNS = str_replace('_', '\\', $className); |
|
1415 | - if (class_exists($classNameNS, false) === true) { |
|
1416 | - $className = $classNameNS; |
|
1417 | - } |
|
1418 | - |
|
1419 | - // Skip abstract classes. |
|
1420 | - $reflection = new ReflectionClass($className); |
|
1421 | - if ($reflection->isAbstract() === true) { |
|
1422 | - continue; |
|
1423 | - } |
|
1424 | - |
|
1425 | - $listeners[$className] = $className; |
|
1426 | - |
|
1427 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
1428 | - echo "Registered $className".PHP_EOL; |
|
1429 | - } |
|
1430 | - }//end foreach |
|
1431 | - |
|
1432 | - $this->sniffs = $listeners; |
|
1433 | - |
|
1434 | - }//end registerSniffs() |
|
1435 | - |
|
1436 | - |
|
1437 | - /** |
|
1438 | - * Populates the array of PHP_CodeSniffer_Sniff's for this file. |
|
1439 | - * |
|
1440 | - * @return void |
|
1441 | - * @throws PHP_CodeSniffer_Exception If sniff registration fails. |
|
1442 | - */ |
|
1443 | - public function populateTokenListeners() |
|
1444 | - { |
|
1445 | - // Construct a list of listeners indexed by token being listened for. |
|
1446 | - $this->_tokenListeners = array(); |
|
1447 | - |
|
1448 | - foreach ($this->sniffs as $listenerClass) { |
|
1449 | - // Work out the internal code for this sniff. Detect usage of namespace |
|
1450 | - // separators instead of underscores to support PHP namespaces. |
|
1451 | - if (strstr($listenerClass, '\\') === false) { |
|
1452 | - $parts = explode('_', $listenerClass); |
|
1453 | - } else { |
|
1454 | - $parts = explode('\\', $listenerClass); |
|
1455 | - } |
|
1456 | - |
|
1457 | - $code = $parts[0].'.'.$parts[2].'.'.$parts[3]; |
|
1458 | - $code = substr($code, 0, -5); |
|
1459 | - |
|
1460 | - $this->listeners[$listenerClass] = new $listenerClass(); |
|
1461 | - $this->sniffCodes[$code] = $listenerClass; |
|
1462 | - |
|
1463 | - // Set custom properties. |
|
1464 | - if (isset($this->ruleset[$code]['properties']) === true) { |
|
1465 | - foreach ($this->ruleset[$code]['properties'] as $name => $value) { |
|
1466 | - $this->setSniffProperty($listenerClass, $name, $value); |
|
1467 | - } |
|
1468 | - } |
|
1469 | - |
|
1470 | - $tokenizers = array(); |
|
1471 | - $vars = get_class_vars($listenerClass); |
|
1472 | - if (isset($vars['supportedTokenizers']) === true) { |
|
1473 | - foreach ($vars['supportedTokenizers'] as $tokenizer) { |
|
1474 | - $tokenizers[$tokenizer] = $tokenizer; |
|
1475 | - } |
|
1476 | - } else { |
|
1477 | - $tokenizers = array('PHP' => 'PHP'); |
|
1478 | - } |
|
1479 | - |
|
1480 | - $tokens = $this->listeners[$listenerClass]->register(); |
|
1481 | - if (is_array($tokens) === false) { |
|
1482 | - $msg = "Sniff $listenerClass register() method must return an array"; |
|
1483 | - throw new PHP_CodeSniffer_Exception($msg); |
|
1484 | - } |
|
1485 | - |
|
1486 | - $parts = explode('_', str_replace('\\', '_', $listenerClass)); |
|
1487 | - $listenerSource = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5); |
|
1488 | - $ignorePatterns = array(); |
|
1489 | - $patterns = $this->getIgnorePatterns($listenerSource); |
|
1490 | - foreach ($patterns as $pattern => $type) { |
|
1491 | - // While there is support for a type of each pattern |
|
1492 | - // (absolute or relative) we don't actually support it here. |
|
1493 | - $replacements = array( |
|
1494 | - '\\,' => ',', |
|
1495 | - '*' => '.*', |
|
1496 | - ); |
|
1497 | - |
|
1498 | - $ignorePatterns[] = strtr($pattern, $replacements); |
|
1499 | - } |
|
1500 | - |
|
1501 | - foreach ($tokens as $token) { |
|
1502 | - if (isset($this->_tokenListeners[$token]) === false) { |
|
1503 | - $this->_tokenListeners[$token] = array(); |
|
1504 | - } |
|
1505 | - |
|
1506 | - if (isset($this->_tokenListeners[$token][$listenerClass]) === false) { |
|
1507 | - $this->_tokenListeners[$token][$listenerClass] = array( |
|
1508 | - 'class' => $listenerClass, |
|
1509 | - 'source' => $listenerSource, |
|
1510 | - 'tokenizers' => $tokenizers, |
|
1511 | - 'ignore' => $ignorePatterns, |
|
1512 | - ); |
|
1513 | - } |
|
1514 | - } |
|
1515 | - }//end foreach |
|
1516 | - |
|
1517 | - }//end populateTokenListeners() |
|
1518 | - |
|
1519 | - |
|
1520 | - /** |
|
1521 | - * Set a single property for a sniff. |
|
1522 | - * |
|
1523 | - * @param string $listenerClass The class name of the sniff. |
|
1524 | - * @param string $name The name of the property to change. |
|
1525 | - * @param string $value The new value of the property. |
|
1526 | - * |
|
1527 | - * @return void |
|
1528 | - */ |
|
1529 | - public function setSniffProperty($listenerClass, $name, $value) |
|
1530 | - { |
|
1531 | - // Setting a property for a sniff we are not using. |
|
1532 | - if (isset($this->listeners[$listenerClass]) === false) { |
|
1533 | - return; |
|
1534 | - } |
|
1535 | - |
|
1536 | - $name = trim($name); |
|
1537 | - if (is_string($value) === true) { |
|
1538 | - $value = trim($value); |
|
1539 | - } |
|
1540 | - |
|
1541 | - // Special case for booleans. |
|
1542 | - if ($value === 'true') { |
|
1543 | - $value = true; |
|
1544 | - } else if ($value === 'false') { |
|
1545 | - $value = false; |
|
1546 | - } |
|
1547 | - |
|
1548 | - $this->listeners[$listenerClass]->$name = $value; |
|
1549 | - |
|
1550 | - }//end setSniffProperty() |
|
1551 | - |
|
1552 | - |
|
1553 | - /** |
|
1554 | - * Get a list of files that will be processed. |
|
1555 | - * |
|
1556 | - * If passed directories, this method will find all files within them. |
|
1557 | - * The method will also perform file extension and ignore pattern filtering. |
|
1558 | - * |
|
1559 | - * @param string $paths A list of file or directory paths to process. |
|
1560 | - * @param boolean $local If true, only process 1 level of files in directories |
|
1561 | - * |
|
1562 | - * @return array |
|
1563 | - * @throws Exception If there was an error opening a directory. |
|
1564 | - * @see shouldProcessFile() |
|
1565 | - */ |
|
1566 | - public function getFilesToProcess($paths, $local=false) |
|
1567 | - { |
|
1568 | - $files = array(); |
|
1569 | - |
|
1570 | - foreach ($paths as $path) { |
|
1571 | - if (is_dir($path) === true || self::isPharFile($path) === true) { |
|
1572 | - if (self::isPharFile($path) === true) { |
|
1573 | - $path = 'phar://'.$path; |
|
1574 | - } |
|
1575 | - |
|
1576 | - if ($local === true) { |
|
1577 | - $di = new DirectoryIterator($path); |
|
1578 | - } else { |
|
1579 | - $di = new RecursiveIteratorIterator( |
|
1580 | - new RecursiveDirectoryIterator($path), |
|
1581 | - 0, |
|
1582 | - RecursiveIteratorIterator::CATCH_GET_CHILD |
|
1583 | - ); |
|
1584 | - } |
|
1585 | - |
|
1586 | - foreach ($di as $file) { |
|
1587 | - // Check if the file exists after all symlinks are resolved. |
|
1588 | - $filePath = self::realpath($file->getPathname()); |
|
1589 | - if ($filePath === false) { |
|
1590 | - continue; |
|
1591 | - } |
|
1592 | - |
|
1593 | - if (is_dir($filePath) === true) { |
|
1594 | - continue; |
|
1595 | - } |
|
1596 | - |
|
1597 | - if ($this->shouldProcessFile($file->getPathname(), $path) === false) { |
|
1598 | - continue; |
|
1599 | - } |
|
1600 | - |
|
1601 | - $files[] = $file->getPathname(); |
|
1602 | - }//end foreach |
|
1603 | - } else { |
|
1604 | - if ($this->shouldIgnoreFile($path, dirname($path)) === true) { |
|
1605 | - continue; |
|
1606 | - } |
|
1607 | - |
|
1608 | - $files[] = $path; |
|
1609 | - }//end if |
|
1610 | - }//end foreach |
|
1611 | - |
|
1612 | - return $files; |
|
1613 | - |
|
1614 | - }//end getFilesToProcess() |
|
1615 | - |
|
1616 | - |
|
1617 | - /** |
|
1618 | - * Checks filtering rules to see if a file should be checked. |
|
1619 | - * |
|
1620 | - * Checks both file extension filters and path ignore filters. |
|
1621 | - * |
|
1622 | - * @param string $path The path to the file being checked. |
|
1623 | - * @param string $basedir The directory to use for relative path checks. |
|
1624 | - * |
|
1625 | - * @return bool |
|
1626 | - */ |
|
1627 | - public function shouldProcessFile($path, $basedir) |
|
1628 | - { |
|
1629 | - // Check that the file's extension is one we are checking. |
|
1630 | - // We are strict about checking the extension and we don't |
|
1631 | - // let files through with no extension or that start with a dot. |
|
1632 | - $fileName = basename($path); |
|
1633 | - $fileParts = explode('.', $fileName); |
|
1634 | - if ($fileParts[0] === $fileName || $fileParts[0] === '') { |
|
1635 | - return false; |
|
1636 | - } |
|
1637 | - |
|
1638 | - // Checking multi-part file extensions, so need to create a |
|
1639 | - // complete extension list and make sure one is allowed. |
|
1640 | - $extensions = array(); |
|
1641 | - array_shift($fileParts); |
|
1642 | - foreach ($fileParts as $part) { |
|
1643 | - $extensions[implode('.', $fileParts)] = 1; |
|
1644 | - array_shift($fileParts); |
|
1645 | - } |
|
1646 | - |
|
1647 | - $matches = array_intersect_key($extensions, $this->allowedFileExtensions); |
|
1648 | - if (empty($matches) === true) { |
|
1649 | - return false; |
|
1650 | - } |
|
1651 | - |
|
1652 | - // If the file's path matches one of our ignore patterns, skip it. |
|
1653 | - if ($this->shouldIgnoreFile($path, $basedir) === true) { |
|
1654 | - return false; |
|
1655 | - } |
|
1656 | - |
|
1657 | - return true; |
|
1658 | - |
|
1659 | - }//end shouldProcessFile() |
|
1660 | - |
|
1661 | - |
|
1662 | - /** |
|
1663 | - * Checks filtering rules to see if a file should be ignored. |
|
1664 | - * |
|
1665 | - * @param string $path The path to the file being checked. |
|
1666 | - * @param string $basedir The directory to use for relative path checks. |
|
1667 | - * |
|
1668 | - * @return bool |
|
1669 | - */ |
|
1670 | - public function shouldIgnoreFile($path, $basedir) |
|
1671 | - { |
|
1672 | - $relativePath = $path; |
|
1673 | - if (strpos($path, $basedir) === 0) { |
|
1674 | - // The +1 cuts off the directory separator as well. |
|
1675 | - $relativePath = substr($path, (strlen($basedir) + 1)); |
|
1676 | - } |
|
1677 | - |
|
1678 | - foreach ($this->ignorePatterns as $pattern => $type) { |
|
1679 | - if (is_array($type) === true) { |
|
1680 | - // A sniff specific ignore pattern. |
|
1681 | - continue; |
|
1682 | - } |
|
1683 | - |
|
1684 | - // Maintains backwards compatibility in case the ignore pattern does |
|
1685 | - // not have a relative/absolute value. |
|
1686 | - if (is_int($pattern) === true) { |
|
1687 | - $pattern = $type; |
|
1688 | - $type = 'absolute'; |
|
1689 | - } |
|
1690 | - |
|
1691 | - $replacements = array( |
|
1692 | - '\\,' => ',', |
|
1693 | - '*' => '.*', |
|
1694 | - ); |
|
1695 | - |
|
1696 | - // We assume a / directory separator, as do the exclude rules |
|
1697 | - // most developers write, so we need a special case for any system |
|
1698 | - // that is different. |
|
1699 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
1700 | - $replacements['/'] = '\\\\'; |
|
1701 | - } |
|
1702 | - |
|
1703 | - $pattern = strtr($pattern, $replacements); |
|
1704 | - |
|
1705 | - if ($type === 'relative') { |
|
1706 | - $testPath = $relativePath; |
|
1707 | - } else { |
|
1708 | - $testPath = $path; |
|
1709 | - } |
|
1710 | - |
|
1711 | - $pattern = '`'.$pattern.'`i'; |
|
1712 | - if (preg_match($pattern, $testPath) === 1) { |
|
1713 | - return true; |
|
1714 | - } |
|
1715 | - }//end foreach |
|
1716 | - |
|
1717 | - return false; |
|
1718 | - |
|
1719 | - }//end shouldIgnoreFile() |
|
1720 | - |
|
1721 | - |
|
1722 | - /** |
|
1723 | - * Run the code sniffs over a single given file. |
|
1724 | - * |
|
1725 | - * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it |
|
1726 | - * conforms with the standard. Returns the processed file object, or NULL |
|
1727 | - * if no file was processed due to error. |
|
1728 | - * |
|
1729 | - * @param string $file The file to process. |
|
1730 | - * @param string $contents The contents to parse. If NULL, the content |
|
1731 | - * is taken from the file system. |
|
1732 | - * |
|
1733 | - * @return PHP_CodeSniffer_File |
|
1734 | - * @throws PHP_CodeSniffer_Exception If the file could not be processed. |
|
1735 | - * @see _processFile() |
|
1736 | - */ |
|
1737 | - public function processFile($file, $contents=null) |
|
1738 | - { |
|
1739 | - if ($contents === null && file_exists($file) === false) { |
|
1740 | - throw new PHP_CodeSniffer_Exception("Source file $file does not exist"); |
|
1741 | - } |
|
1742 | - |
|
1743 | - $filePath = self::realpath($file); |
|
1744 | - if ($filePath === false) { |
|
1745 | - $filePath = $file; |
|
1746 | - } |
|
1747 | - |
|
1748 | - // Before we go and spend time tokenizing this file, just check |
|
1749 | - // to see if there is a tag up top to indicate that the whole |
|
1750 | - // file should be ignored. It must be on one of the first two lines. |
|
1751 | - $firstContent = $contents; |
|
1752 | - if ($contents === null && is_readable($filePath) === true) { |
|
1753 | - $handle = fopen($filePath, 'r'); |
|
1754 | - stream_set_blocking($handle, true); |
|
1755 | - if ($handle !== false) { |
|
1756 | - $firstContent = fgets($handle); |
|
1757 | - $firstContent .= fgets($handle); |
|
1758 | - fclose($handle); |
|
1759 | - |
|
1760 | - if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) { |
|
1761 | - // We are ignoring the whole file. |
|
1762 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
1763 | - echo 'Ignoring '.basename($filePath).PHP_EOL; |
|
1764 | - } |
|
1765 | - |
|
1766 | - return null; |
|
1767 | - } |
|
1768 | - } |
|
1769 | - }//end if |
|
1770 | - |
|
1771 | - try { |
|
1772 | - $phpcsFile = $this->_processFile($file, $contents); |
|
1773 | - } catch (Exception $e) { |
|
1774 | - $trace = $e->getTrace(); |
|
1775 | - |
|
1776 | - $filename = $trace[0]['args'][0]; |
|
1777 | - if (is_object($filename) === true |
|
1778 | - && get_class($filename) === 'PHP_CodeSniffer_File' |
|
1779 | - ) { |
|
1780 | - $filename = $filename->getFilename(); |
|
1781 | - } else if (is_numeric($filename) === true) { |
|
1782 | - // See if we can find the PHP_CodeSniffer_File object. |
|
1783 | - foreach ($trace as $data) { |
|
1784 | - if (isset($data['args'][0]) === true |
|
1785 | - && ($data['args'][0] instanceof PHP_CodeSniffer_File) === true |
|
1786 | - ) { |
|
1787 | - $filename = $data['args'][0]->getFilename(); |
|
1788 | - } |
|
1789 | - } |
|
1790 | - } else if (is_string($filename) === false) { |
|
1791 | - $filename = (string) $filename; |
|
1792 | - } |
|
1793 | - |
|
1794 | - $errorMessage = '"'.$e->getMessage().'" at '.$e->getFile().':'.$e->getLine(); |
|
1795 | - $error = "An error occurred during processing; checking has been aborted. The error message was: $errorMessage"; |
|
1796 | - |
|
1797 | - $phpcsFile = new PHP_CodeSniffer_File( |
|
1798 | - $filename, |
|
1799 | - $this->_tokenListeners, |
|
1800 | - $this->ruleset, |
|
1801 | - $this |
|
1802 | - ); |
|
1803 | - |
|
1804 | - $phpcsFile->addError($error, null); |
|
1805 | - }//end try |
|
1806 | - |
|
1807 | - $cliValues = $this->cli->getCommandLineValues(); |
|
1808 | - |
|
1809 | - if (PHP_CODESNIFFER_INTERACTIVE === false) { |
|
1810 | - // Cache the report data for this file so we can unset it to save memory. |
|
1811 | - $this->reporting->cacheFileReport($phpcsFile, $cliValues); |
|
1812 | - $phpcsFile->cleanUp(); |
|
1813 | - return $phpcsFile; |
|
1814 | - } |
|
1815 | - |
|
1816 | - /* |
|
71 | + /** |
|
72 | + * The current version. |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + const VERSION = '2.7.1'; |
|
77 | + |
|
78 | + /** |
|
79 | + * Package stability; either stable, beta or alpha. |
|
80 | + * |
|
81 | + * @var string |
|
82 | + */ |
|
83 | + const STABILITY = 'stable'; |
|
84 | + |
|
85 | + /** |
|
86 | + * The file or directory that is currently being processed. |
|
87 | + * |
|
88 | + * @var string |
|
89 | + */ |
|
90 | + protected $file = ''; |
|
91 | + |
|
92 | + /** |
|
93 | + * The directories that the processed rulesets are in. |
|
94 | + * |
|
95 | + * This is declared static because it is also used in the |
|
96 | + * autoloader to look for sniffs outside the PHPCS install. |
|
97 | + * This way, standards designed to be installed inside PHPCS can |
|
98 | + * also be used from outside the PHPCS Standards directory. |
|
99 | + * |
|
100 | + * @var string |
|
101 | + */ |
|
102 | + protected static $rulesetDirs = array(); |
|
103 | + |
|
104 | + /** |
|
105 | + * The CLI object controlling the run. |
|
106 | + * |
|
107 | + * @var PHP_CodeSniffer_CLI |
|
108 | + */ |
|
109 | + public $cli = null; |
|
110 | + |
|
111 | + /** |
|
112 | + * The Reporting object controlling report generation. |
|
113 | + * |
|
114 | + * @var PHP_CodeSniffer_Reporting |
|
115 | + */ |
|
116 | + public $reporting = null; |
|
117 | + |
|
118 | + /** |
|
119 | + * An array of sniff objects that are being used to check files. |
|
120 | + * |
|
121 | + * @var array(PHP_CodeSniffer_Sniff) |
|
122 | + */ |
|
123 | + protected $listeners = array(); |
|
124 | + |
|
125 | + /** |
|
126 | + * An array of sniffs that are being used to check files. |
|
127 | + * |
|
128 | + * @var array(string) |
|
129 | + */ |
|
130 | + protected $sniffs = array(); |
|
131 | + |
|
132 | + /** |
|
133 | + * A mapping of sniff codes to fully qualified class names. |
|
134 | + * |
|
135 | + * The key is the sniff code and the value |
|
136 | + * is the fully qualified name of the sniff class. |
|
137 | + * |
|
138 | + * @var array<string, string> |
|
139 | + */ |
|
140 | + public $sniffCodes = array(); |
|
141 | + |
|
142 | + /** |
|
143 | + * The listeners array, indexed by token type. |
|
144 | + * |
|
145 | + * @var array |
|
146 | + */ |
|
147 | + private $_tokenListeners = array(); |
|
148 | + |
|
149 | + /** |
|
150 | + * An array of rules from the ruleset.xml file. |
|
151 | + * |
|
152 | + * It may be empty, indicating that the ruleset does not override |
|
153 | + * any of the default sniff settings. |
|
154 | + * |
|
155 | + * @var array |
|
156 | + */ |
|
157 | + protected $ruleset = array(); |
|
158 | + |
|
159 | + /** |
|
160 | + * An array of patterns to use for skipping files. |
|
161 | + * |
|
162 | + * @var array |
|
163 | + */ |
|
164 | + protected $ignorePatterns = array(); |
|
165 | + |
|
166 | + /** |
|
167 | + * An array of extensions for files we will check. |
|
168 | + * |
|
169 | + * @var array |
|
170 | + */ |
|
171 | + public $allowedFileExtensions = array(); |
|
172 | + |
|
173 | + /** |
|
174 | + * An array of default extensions and associated tokenizers. |
|
175 | + * |
|
176 | + * If no extensions are set, these will be used as the defaults. |
|
177 | + * If extensions are set, these will be used when the correct tokenizer |
|
178 | + * can not be determined, such as when checking a passed filename instead |
|
179 | + * of files in a directory. |
|
180 | + * |
|
181 | + * @var array |
|
182 | + */ |
|
183 | + public $defaultFileExtensions = array( |
|
184 | + 'php' => 'PHP', |
|
185 | + 'inc' => 'PHP', |
|
186 | + 'js' => 'JS', |
|
187 | + 'css' => 'CSS', |
|
188 | + ); |
|
189 | + |
|
190 | + /** |
|
191 | + * An array of variable types for param/var we will check. |
|
192 | + * |
|
193 | + * @var array(string) |
|
194 | + */ |
|
195 | + public static $allowedTypes = array( |
|
196 | + 'array', |
|
197 | + 'boolean', |
|
198 | + 'float', |
|
199 | + 'integer', |
|
200 | + 'mixed', |
|
201 | + 'object', |
|
202 | + 'string', |
|
203 | + 'resource', |
|
204 | + 'callable', |
|
205 | + ); |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * Constructs a PHP_CodeSniffer object. |
|
210 | + * |
|
211 | + * @param int $verbosity The verbosity level. |
|
212 | + * 1: Print progress information. |
|
213 | + * 2: Print tokenizer debug information. |
|
214 | + * 3: Print sniff debug information. |
|
215 | + * @param int $tabWidth The number of spaces each tab represents. |
|
216 | + * If greater than zero, tabs will be replaced |
|
217 | + * by spaces before testing each file. |
|
218 | + * @param string $encoding The charset of the sniffed files. |
|
219 | + * This is important for some reports that output |
|
220 | + * with utf-8 encoding as you don't want it double |
|
221 | + * encoding messages. |
|
222 | + * @param bool $interactive If TRUE, will stop after each file with errors |
|
223 | + * and wait for user input. |
|
224 | + * |
|
225 | + * @see process() |
|
226 | + */ |
|
227 | + public function __construct( |
|
228 | + $verbosity=0, |
|
229 | + $tabWidth=0, |
|
230 | + $encoding='iso-8859-1', |
|
231 | + $interactive=false |
|
232 | + ) { |
|
233 | + if ($verbosity !== null) { |
|
234 | + $this->setVerbosity($verbosity); |
|
235 | + } |
|
236 | + |
|
237 | + if ($tabWidth !== null) { |
|
238 | + $this->setTabWidth($tabWidth); |
|
239 | + } |
|
240 | + |
|
241 | + if ($encoding !== null) { |
|
242 | + $this->setEncoding($encoding); |
|
243 | + } |
|
244 | + |
|
245 | + if ($interactive !== null) { |
|
246 | + $this->setInteractive($interactive); |
|
247 | + } |
|
248 | + |
|
249 | + if (defined('PHPCS_DEFAULT_ERROR_SEV') === false) { |
|
250 | + define('PHPCS_DEFAULT_ERROR_SEV', 5); |
|
251 | + } |
|
252 | + |
|
253 | + if (defined('PHPCS_DEFAULT_WARN_SEV') === false) { |
|
254 | + define('PHPCS_DEFAULT_WARN_SEV', 5); |
|
255 | + } |
|
256 | + |
|
257 | + if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
258 | + define('PHP_CODESNIFFER_CBF', false); |
|
259 | + } |
|
260 | + |
|
261 | + // Set default CLI object in case someone is running us |
|
262 | + // without using the command line script. |
|
263 | + $this->cli = new PHP_CodeSniffer_CLI(); |
|
264 | + $this->cli->errorSeverity = PHPCS_DEFAULT_ERROR_SEV; |
|
265 | + $this->cli->warningSeverity = PHPCS_DEFAULT_WARN_SEV; |
|
266 | + $this->cli->dieOnUnknownArg = false; |
|
267 | + |
|
268 | + $this->reporting = new PHP_CodeSniffer_Reporting(); |
|
269 | + |
|
270 | + }//end __construct() |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * Autoload static method for loading classes and interfaces. |
|
275 | + * |
|
276 | + * @param string $className The name of the class or interface. |
|
277 | + * |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public static function autoload($className) |
|
281 | + { |
|
282 | + if (substr($className, 0, 4) === 'PHP_') { |
|
283 | + $newClassName = substr($className, 4); |
|
284 | + } else { |
|
285 | + $newClassName = $className; |
|
286 | + } |
|
287 | + |
|
288 | + $path = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $newClassName).'.php'; |
|
289 | + |
|
290 | + if (is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.$path) === true) { |
|
291 | + // Check standard file locations based on class name. |
|
292 | + include dirname(__FILE__).DIRECTORY_SEPARATOR.$path; |
|
293 | + return; |
|
294 | + } else { |
|
295 | + // Check for included sniffs. |
|
296 | + $installedPaths = PHP_CodeSniffer::getInstalledStandardPaths(); |
|
297 | + foreach ($installedPaths as $installedPath) { |
|
298 | + if (is_file($installedPath.DIRECTORY_SEPARATOR.$path) === true) { |
|
299 | + include $installedPath.DIRECTORY_SEPARATOR.$path; |
|
300 | + return; |
|
301 | + } |
|
302 | + } |
|
303 | + |
|
304 | + // Check standard file locations based on the loaded rulesets. |
|
305 | + foreach (self::$rulesetDirs as $rulesetDir) { |
|
306 | + if (is_file(dirname($rulesetDir).DIRECTORY_SEPARATOR.$path) === true) { |
|
307 | + include_once dirname($rulesetDir).DIRECTORY_SEPARATOR.$path; |
|
308 | + return; |
|
309 | + } |
|
310 | + } |
|
311 | + }//end if |
|
312 | + |
|
313 | + // Everything else. |
|
314 | + @include $path; |
|
315 | + |
|
316 | + }//end autoload() |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Sets the verbosity level. |
|
321 | + * |
|
322 | + * @param int $verbosity The verbosity level. |
|
323 | + * 1: Print progress information. |
|
324 | + * 2: Print tokenizer debug information. |
|
325 | + * 3: Print sniff debug information. |
|
326 | + * |
|
327 | + * @return void |
|
328 | + */ |
|
329 | + public function setVerbosity($verbosity) |
|
330 | + { |
|
331 | + if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
332 | + define('PHP_CODESNIFFER_VERBOSITY', $verbosity); |
|
333 | + } |
|
334 | + |
|
335 | + }//end setVerbosity() |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Sets the tab width. |
|
340 | + * |
|
341 | + * @param int $tabWidth The number of spaces each tab represents. |
|
342 | + * If greater than zero, tabs will be replaced |
|
343 | + * by spaces before testing each file. |
|
344 | + * |
|
345 | + * @return void |
|
346 | + */ |
|
347 | + public function setTabWidth($tabWidth) |
|
348 | + { |
|
349 | + if (defined('PHP_CODESNIFFER_TAB_WIDTH') === false) { |
|
350 | + define('PHP_CODESNIFFER_TAB_WIDTH', $tabWidth); |
|
351 | + } |
|
352 | + |
|
353 | + }//end setTabWidth() |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Sets the encoding. |
|
358 | + * |
|
359 | + * @param string $encoding The charset of the sniffed files. |
|
360 | + * This is important for some reports that output |
|
361 | + * with utf-8 encoding as you don't want it double |
|
362 | + * encoding messages. |
|
363 | + * |
|
364 | + * @return void |
|
365 | + */ |
|
366 | + public function setEncoding($encoding) |
|
367 | + { |
|
368 | + if (defined('PHP_CODESNIFFER_ENCODING') === false) { |
|
369 | + define('PHP_CODESNIFFER_ENCODING', $encoding); |
|
370 | + } |
|
371 | + |
|
372 | + }//end setEncoding() |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Sets the interactive flag. |
|
377 | + * |
|
378 | + * @param bool $interactive If TRUE, will stop after each file with errors |
|
379 | + * and wait for user input. |
|
380 | + * |
|
381 | + * @return void |
|
382 | + */ |
|
383 | + public function setInteractive($interactive) |
|
384 | + { |
|
385 | + if (defined('PHP_CODESNIFFER_INTERACTIVE') === false) { |
|
386 | + define('PHP_CODESNIFFER_INTERACTIVE', $interactive); |
|
387 | + } |
|
388 | + |
|
389 | + }//end setInteractive() |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Sets an array of file extensions that we will allow checking of. |
|
394 | + * |
|
395 | + * If the extension is one of the defaults, a specific tokenizer |
|
396 | + * will be used. Otherwise, the PHP tokenizer will be used for |
|
397 | + * all extensions passed. |
|
398 | + * |
|
399 | + * @param array $extensions An array of file extensions. |
|
400 | + * |
|
401 | + * @return void |
|
402 | + */ |
|
403 | + public function setAllowedFileExtensions(array $extensions) |
|
404 | + { |
|
405 | + $newExtensions = array(); |
|
406 | + foreach ($extensions as $ext) { |
|
407 | + $slash = strpos($ext, '/'); |
|
408 | + if ($slash !== false) { |
|
409 | + // They specified the tokenizer too. |
|
410 | + list($ext, $tokenizer) = explode('/', $ext); |
|
411 | + $newExtensions[$ext] = strtoupper($tokenizer); |
|
412 | + continue; |
|
413 | + } |
|
414 | + |
|
415 | + if (isset($this->allowedFileExtensions[$ext]) === true) { |
|
416 | + $newExtensions[$ext] = $this->allowedFileExtensions[$ext]; |
|
417 | + } else if (isset($this->defaultFileExtensions[$ext]) === true) { |
|
418 | + $newExtensions[$ext] = $this->defaultFileExtensions[$ext]; |
|
419 | + } else { |
|
420 | + $newExtensions[$ext] = 'PHP'; |
|
421 | + } |
|
422 | + } |
|
423 | + |
|
424 | + $this->allowedFileExtensions = $newExtensions; |
|
425 | + |
|
426 | + }//end setAllowedFileExtensions() |
|
427 | + |
|
428 | + |
|
429 | + /** |
|
430 | + * Sets an array of ignore patterns that we use to skip files and folders. |
|
431 | + * |
|
432 | + * Patterns are not case sensitive. |
|
433 | + * |
|
434 | + * @param array $patterns An array of ignore patterns. The pattern is the key |
|
435 | + * and the value is either "absolute" or "relative", |
|
436 | + * depending on how the pattern should be applied to a |
|
437 | + * file path. |
|
438 | + * |
|
439 | + * @return void |
|
440 | + */ |
|
441 | + public function setIgnorePatterns(array $patterns) |
|
442 | + { |
|
443 | + $this->ignorePatterns = $patterns; |
|
444 | + |
|
445 | + }//end setIgnorePatterns() |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * Gets the array of ignore patterns. |
|
450 | + * |
|
451 | + * Optionally takes a listener to get ignore patterns specified |
|
452 | + * for that sniff only. |
|
453 | + * |
|
454 | + * @param string $listener The listener to get patterns for. If NULL, all |
|
455 | + * patterns are returned. |
|
456 | + * |
|
457 | + * @return array |
|
458 | + */ |
|
459 | + public function getIgnorePatterns($listener=null) |
|
460 | + { |
|
461 | + if ($listener === null) { |
|
462 | + return $this->ignorePatterns; |
|
463 | + } |
|
464 | + |
|
465 | + if (isset($this->ignorePatterns[$listener]) === true) { |
|
466 | + return $this->ignorePatterns[$listener]; |
|
467 | + } |
|
468 | + |
|
469 | + return array(); |
|
470 | + |
|
471 | + }//end getIgnorePatterns() |
|
472 | + |
|
473 | + |
|
474 | + /** |
|
475 | + * Sets the internal CLI object. |
|
476 | + * |
|
477 | + * @param object $cli The CLI object controlling the run. |
|
478 | + * |
|
479 | + * @return void |
|
480 | + */ |
|
481 | + public function setCli($cli) |
|
482 | + { |
|
483 | + $this->cli = $cli; |
|
484 | + |
|
485 | + }//end setCli() |
|
486 | + |
|
487 | + |
|
488 | + /** |
|
489 | + * Start a PHP_CodeSniffer run. |
|
490 | + * |
|
491 | + * @param string|array $files The files and directories to process. For |
|
492 | + * directories, each sub directory will also |
|
493 | + * be traversed for source files. |
|
494 | + * @param string|array $standards The set of code sniffs we are testing |
|
495 | + * against. |
|
496 | + * @param array $restrictions The sniff codes to restrict the |
|
497 | + * violations to. |
|
498 | + * @param boolean $local If true, don't recurse into directories. |
|
499 | + * |
|
500 | + * @return void |
|
501 | + */ |
|
502 | + public function process($files, $standards, array $restrictions=array(), $local=false) |
|
503 | + { |
|
504 | + $files = (array) $files; |
|
505 | + $this->initStandard($standards, $restrictions); |
|
506 | + $this->processFiles($files, $local); |
|
507 | + |
|
508 | + }//end process() |
|
509 | + |
|
510 | + |
|
511 | + /** |
|
512 | + * Initialise the standard that the run will use. |
|
513 | + * |
|
514 | + * @param string|array $standards The set of code sniffs we are testing |
|
515 | + * against. |
|
516 | + * @param array $restrictions The sniff codes to restrict the testing to. |
|
517 | + * @param array $exclusions The sniff codes to exclude from testing. |
|
518 | + * |
|
519 | + * @return void |
|
520 | + */ |
|
521 | + public function initStandard($standards, array $restrictions=array(), array $exclusions=array()) |
|
522 | + { |
|
523 | + $standards = (array) $standards; |
|
524 | + |
|
525 | + // Reset the members. |
|
526 | + $this->listeners = array(); |
|
527 | + $this->sniffs = array(); |
|
528 | + $this->ruleset = array(); |
|
529 | + $this->_tokenListeners = array(); |
|
530 | + self::$rulesetDirs = array(); |
|
531 | + |
|
532 | + // Ensure this option is enabled or else line endings will not always |
|
533 | + // be detected properly for files created on a Mac with the /r line ending. |
|
534 | + ini_set('auto_detect_line_endings', true); |
|
535 | + |
|
536 | + if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) { |
|
537 | + // Should be one standard and one sniff being tested at a time. |
|
538 | + $installed = $this->getInstalledStandardPath($standards[0]); |
|
539 | + if ($installed !== null) { |
|
540 | + $standard = $installed; |
|
541 | + } else { |
|
542 | + $standard = self::realpath($standards[0]); |
|
543 | + if (is_dir($standard) === true |
|
544 | + && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
545 | + ) { |
|
546 | + $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
547 | + } |
|
548 | + } |
|
549 | + |
|
550 | + $sniffs = $this->_expandRulesetReference($restrictions[0], dirname($standard)); |
|
551 | + } else { |
|
552 | + $sniffs = array(); |
|
553 | + foreach ($standards as $standard) { |
|
554 | + $installed = $this->getInstalledStandardPath($standard); |
|
555 | + if ($installed !== null) { |
|
556 | + $standard = $installed; |
|
557 | + } else { |
|
558 | + $standard = self::realpath($standard); |
|
559 | + if (is_dir($standard) === true |
|
560 | + && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
561 | + ) { |
|
562 | + $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
563 | + } |
|
564 | + } |
|
565 | + |
|
566 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
567 | + $ruleset = simplexml_load_string(file_get_contents($standard)); |
|
568 | + if ($ruleset !== false) { |
|
569 | + $standardName = (string) $ruleset['name']; |
|
570 | + } |
|
571 | + |
|
572 | + echo "Registering sniffs in the $standardName standard... "; |
|
573 | + if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) { |
|
574 | + echo PHP_EOL; |
|
575 | + } |
|
576 | + } |
|
577 | + |
|
578 | + $sniffs = array_merge($sniffs, $this->processRuleset($standard)); |
|
579 | + }//end foreach |
|
580 | + }//end if |
|
581 | + |
|
582 | + $sniffRestrictions = array(); |
|
583 | + foreach ($restrictions as $sniffCode) { |
|
584 | + $parts = explode('.', strtolower($sniffCode)); |
|
585 | + $sniffRestrictions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
586 | + } |
|
587 | + |
|
588 | + $sniffExclusions = array(); |
|
589 | + foreach ($exclusions as $sniffCode) { |
|
590 | + $parts = explode('.', strtolower($sniffCode)); |
|
591 | + $sniffExclusions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
592 | + } |
|
593 | + |
|
594 | + $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions); |
|
595 | + $this->populateTokenListeners(); |
|
596 | + |
|
597 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
598 | + $numSniffs = count($this->sniffs); |
|
599 | + echo "DONE ($numSniffs sniffs registered)".PHP_EOL; |
|
600 | + } |
|
601 | + |
|
602 | + }//end initStandard() |
|
603 | + |
|
604 | + |
|
605 | + /** |
|
606 | + * Processes the files/directories that PHP_CodeSniffer was constructed with. |
|
607 | + * |
|
608 | + * @param string|array $files The files and directories to process. For |
|
609 | + * directories, each sub directory will also |
|
610 | + * be traversed for source files. |
|
611 | + * @param boolean $local If true, don't recurse into directories. |
|
612 | + * |
|
613 | + * @return void |
|
614 | + * @throws PHP_CodeSniffer_Exception If files are invalid. |
|
615 | + */ |
|
616 | + public function processFiles($files, $local=false) |
|
617 | + { |
|
618 | + $files = (array) $files; |
|
619 | + $cliValues = $this->cli->getCommandLineValues(); |
|
620 | + $showProgress = $cliValues['showProgress']; |
|
621 | + $useColors = $cliValues['colors']; |
|
622 | + |
|
623 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
624 | + echo 'Creating file list... '; |
|
625 | + } |
|
626 | + |
|
627 | + if (empty($this->allowedFileExtensions) === true) { |
|
628 | + $this->allowedFileExtensions = $this->defaultFileExtensions; |
|
629 | + } |
|
630 | + |
|
631 | + $todo = $this->getFilesToProcess($files, $local); |
|
632 | + $numFiles = count($todo); |
|
633 | + |
|
634 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
635 | + echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
636 | + } |
|
637 | + |
|
638 | + $numProcessed = 0; |
|
639 | + $dots = 0; |
|
640 | + $maxLength = strlen($numFiles); |
|
641 | + $lastDir = ''; |
|
642 | + foreach ($todo as $file) { |
|
643 | + $this->file = $file; |
|
644 | + $currDir = dirname($file); |
|
645 | + if ($lastDir !== $currDir) { |
|
646 | + if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true) { |
|
647 | + echo 'Changing into directory '.$currDir.PHP_EOL; |
|
648 | + } |
|
649 | + |
|
650 | + $lastDir = $currDir; |
|
651 | + } |
|
652 | + |
|
653 | + $phpcsFile = $this->processFile($file, null); |
|
654 | + $numProcessed++; |
|
655 | + |
|
656 | + if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
657 | + || PHP_CODESNIFFER_INTERACTIVE === true |
|
658 | + || $showProgress === false |
|
659 | + ) { |
|
660 | + continue; |
|
661 | + } |
|
662 | + |
|
663 | + // Show progress information. |
|
664 | + if ($phpcsFile === null) { |
|
665 | + echo 'S'; |
|
666 | + } else { |
|
667 | + $errors = $phpcsFile->getErrorCount(); |
|
668 | + $warnings = $phpcsFile->getWarningCount(); |
|
669 | + if ($errors > 0) { |
|
670 | + if ($useColors === true) { |
|
671 | + echo "\033[31m"; |
|
672 | + } |
|
673 | + |
|
674 | + echo 'E'; |
|
675 | + } else if ($warnings > 0) { |
|
676 | + if ($useColors === true) { |
|
677 | + echo "\033[33m"; |
|
678 | + } |
|
679 | + |
|
680 | + echo 'W'; |
|
681 | + } else { |
|
682 | + echo '.'; |
|
683 | + } |
|
684 | + |
|
685 | + if ($useColors === true) { |
|
686 | + echo "\033[0m"; |
|
687 | + } |
|
688 | + }//end if |
|
689 | + |
|
690 | + $dots++; |
|
691 | + if ($dots === 60) { |
|
692 | + $padding = ($maxLength - strlen($numProcessed)); |
|
693 | + echo str_repeat(' ', $padding); |
|
694 | + $percent = round(($numProcessed / $numFiles) * 100); |
|
695 | + echo " $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
696 | + $dots = 0; |
|
697 | + } |
|
698 | + }//end foreach |
|
699 | + |
|
700 | + if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
701 | + && PHP_CODESNIFFER_INTERACTIVE === false |
|
702 | + && $showProgress === true |
|
703 | + ) { |
|
704 | + echo PHP_EOL.PHP_EOL; |
|
705 | + } |
|
706 | + |
|
707 | + }//end processFiles() |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * Processes a single ruleset and returns a list of the sniffs it represents. |
|
712 | + * |
|
713 | + * Rules founds within the ruleset are processed immediately, but sniff classes |
|
714 | + * are not registered by this method. |
|
715 | + * |
|
716 | + * @param string $rulesetPath The path to a ruleset XML file. |
|
717 | + * @param int $depth How many nested processing steps we are in. This |
|
718 | + * is only used for debug output. |
|
719 | + * |
|
720 | + * @return array |
|
721 | + * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid. |
|
722 | + */ |
|
723 | + public function processRuleset($rulesetPath, $depth=0) |
|
724 | + { |
|
725 | + $rulesetPath = self::realpath($rulesetPath); |
|
726 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
727 | + echo str_repeat("\t", $depth); |
|
728 | + echo "Processing ruleset $rulesetPath".PHP_EOL; |
|
729 | + } |
|
730 | + |
|
731 | + $ruleset = simplexml_load_string(file_get_contents($rulesetPath)); |
|
732 | + if ($ruleset === false) { |
|
733 | + throw new PHP_CodeSniffer_Exception("Ruleset $rulesetPath is not valid"); |
|
734 | + } |
|
735 | + |
|
736 | + $ownSniffs = array(); |
|
737 | + $includedSniffs = array(); |
|
738 | + $excludedSniffs = array(); |
|
739 | + $cliValues = $this->cli->getCommandLineValues(); |
|
740 | + |
|
741 | + $rulesetDir = dirname($rulesetPath); |
|
742 | + self::$rulesetDirs[] = $rulesetDir; |
|
743 | + |
|
744 | + if (is_dir($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs') === true) { |
|
745 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
746 | + echo str_repeat("\t", $depth); |
|
747 | + echo "\tAdding sniff files from \"/.../".basename($rulesetDir)."/Sniffs/\" directory".PHP_EOL; |
|
748 | + } |
|
749 | + |
|
750 | + $ownSniffs = $this->_expandSniffDirectory($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs', $depth); |
|
751 | + } |
|
752 | + |
|
753 | + // Process custom sniff config settings. |
|
754 | + foreach ($ruleset->{'config'} as $config) { |
|
755 | + if ($this->_shouldProcessElement($config) === false) { |
|
756 | + continue; |
|
757 | + } |
|
758 | + |
|
759 | + $this->setConfigData((string) $config['name'], (string) $config['value'], true); |
|
760 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
761 | + echo str_repeat("\t", $depth); |
|
762 | + echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL; |
|
763 | + } |
|
764 | + } |
|
765 | + |
|
766 | + foreach ($ruleset->rule as $rule) { |
|
767 | + if (isset($rule['ref']) === false |
|
768 | + || $this->_shouldProcessElement($rule) === false |
|
769 | + ) { |
|
770 | + continue; |
|
771 | + } |
|
772 | + |
|
773 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
774 | + echo str_repeat("\t", $depth); |
|
775 | + echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL; |
|
776 | + } |
|
777 | + |
|
778 | + $includedSniffs = array_merge( |
|
779 | + $includedSniffs, |
|
780 | + $this->_expandRulesetReference($rule['ref'], $rulesetDir, $depth) |
|
781 | + ); |
|
782 | + |
|
783 | + if (isset($rule->exclude) === true) { |
|
784 | + foreach ($rule->exclude as $exclude) { |
|
785 | + if ($this->_shouldProcessElement($exclude) === false) { |
|
786 | + continue; |
|
787 | + } |
|
788 | + |
|
789 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
790 | + echo str_repeat("\t", $depth); |
|
791 | + echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL; |
|
792 | + } |
|
793 | + |
|
794 | + // Check if a single code is being excluded, which is a shortcut |
|
795 | + // for setting the severity of the message to 0. |
|
796 | + $parts = explode('.', $exclude['name']); |
|
797 | + if (count($parts) === 4) { |
|
798 | + $this->ruleset[(string) $exclude['name']]['severity'] = 0; |
|
799 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
800 | + echo str_repeat("\t", $depth); |
|
801 | + echo "\t\t=> severity set to 0".PHP_EOL; |
|
802 | + } |
|
803 | + } else { |
|
804 | + $excludedSniffs = array_merge( |
|
805 | + $excludedSniffs, |
|
806 | + $this->_expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1)) |
|
807 | + ); |
|
808 | + } |
|
809 | + }//end foreach |
|
810 | + }//end if |
|
811 | + |
|
812 | + $this->_processRule($rule, $depth); |
|
813 | + }//end foreach |
|
814 | + |
|
815 | + // Process custom command line arguments. |
|
816 | + $cliArgs = array(); |
|
817 | + foreach ($ruleset->{'arg'} as $arg) { |
|
818 | + if ($this->_shouldProcessElement($arg) === false) { |
|
819 | + continue; |
|
820 | + } |
|
821 | + |
|
822 | + if (isset($arg['name']) === true) { |
|
823 | + $argString = '--'.(string) $arg['name']; |
|
824 | + if (isset($arg['value']) === true) { |
|
825 | + $argString .= '='.(string) $arg['value']; |
|
826 | + } |
|
827 | + } else { |
|
828 | + $argString = '-'.(string) $arg['value']; |
|
829 | + } |
|
830 | + |
|
831 | + $cliArgs[] = $argString; |
|
832 | + |
|
833 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
834 | + echo str_repeat("\t", $depth); |
|
835 | + echo "\t=> set command line value $argString".PHP_EOL; |
|
836 | + } |
|
837 | + }//end foreach |
|
838 | + |
|
839 | + // Set custom php ini values as CLI args. |
|
840 | + foreach ($ruleset->{'ini'} as $arg) { |
|
841 | + if ($this->_shouldProcessElement($arg) === false) { |
|
842 | + continue; |
|
843 | + } |
|
844 | + |
|
845 | + if (isset($arg['name']) === false) { |
|
846 | + continue; |
|
847 | + } |
|
848 | + |
|
849 | + $name = (string) $arg['name']; |
|
850 | + $argString = $name; |
|
851 | + if (isset($arg['value']) === true) { |
|
852 | + $value = (string) $arg['value']; |
|
853 | + $argString .= "=$value"; |
|
854 | + } else { |
|
855 | + $value = 'true'; |
|
856 | + } |
|
857 | + |
|
858 | + $cliArgs[] = '-d'; |
|
859 | + $cliArgs[] = $argString; |
|
860 | + |
|
861 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
862 | + echo str_repeat("\t", $depth); |
|
863 | + echo "\t=> set PHP ini value $name to $value".PHP_EOL; |
|
864 | + } |
|
865 | + }//end foreach |
|
866 | + |
|
867 | + if (empty($cliValues['files']) === true && $cliValues['stdin'] === null) { |
|
868 | + // Process hard-coded file paths. |
|
869 | + foreach ($ruleset->{'file'} as $file) { |
|
870 | + $file = (string) $file; |
|
871 | + $cliArgs[] = $file; |
|
872 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
873 | + echo str_repeat("\t", $depth); |
|
874 | + echo "\t=> added \"$file\" to the file list".PHP_EOL; |
|
875 | + } |
|
876 | + } |
|
877 | + } |
|
878 | + |
|
879 | + if (empty($cliArgs) === false) { |
|
880 | + // Change the directory so all relative paths are worked |
|
881 | + // out based on the location of the ruleset instead of |
|
882 | + // the location of the user. |
|
883 | + $inPhar = self::isPharFile($rulesetDir); |
|
884 | + if ($inPhar === false) { |
|
885 | + $currentDir = getcwd(); |
|
886 | + chdir($rulesetDir); |
|
887 | + } |
|
888 | + |
|
889 | + $this->cli->setCommandLineValues($cliArgs); |
|
890 | + |
|
891 | + if ($inPhar === false) { |
|
892 | + chdir($currentDir); |
|
893 | + } |
|
894 | + } |
|
895 | + |
|
896 | + // Process custom ignore pattern rules. |
|
897 | + foreach ($ruleset->{'exclude-pattern'} as $pattern) { |
|
898 | + if ($this->_shouldProcessElement($pattern) === false) { |
|
899 | + continue; |
|
900 | + } |
|
901 | + |
|
902 | + if (isset($pattern['type']) === false) { |
|
903 | + $pattern['type'] = 'absolute'; |
|
904 | + } |
|
905 | + |
|
906 | + $this->ignorePatterns[(string) $pattern] = (string) $pattern['type']; |
|
907 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
908 | + echo str_repeat("\t", $depth); |
|
909 | + echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
910 | + } |
|
911 | + } |
|
912 | + |
|
913 | + $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs)); |
|
914 | + $excludedSniffs = array_unique($excludedSniffs); |
|
915 | + |
|
916 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
917 | + $included = count($includedSniffs); |
|
918 | + $excluded = count($excludedSniffs); |
|
919 | + echo str_repeat("\t", $depth); |
|
920 | + echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL; |
|
921 | + } |
|
922 | + |
|
923 | + // Merge our own sniff list with our externally included |
|
924 | + // sniff list, but filter out any excluded sniffs. |
|
925 | + $files = array(); |
|
926 | + foreach ($includedSniffs as $sniff) { |
|
927 | + if (in_array($sniff, $excludedSniffs) === true) { |
|
928 | + continue; |
|
929 | + } else { |
|
930 | + $files[] = self::realpath($sniff); |
|
931 | + } |
|
932 | + } |
|
933 | + |
|
934 | + return $files; |
|
935 | + |
|
936 | + }//end processRuleset() |
|
937 | + |
|
938 | + |
|
939 | + /** |
|
940 | + * Expands a directory into a list of sniff files within. |
|
941 | + * |
|
942 | + * @param string $directory The path to a directory. |
|
943 | + * @param int $depth How many nested processing steps we are in. This |
|
944 | + * is only used for debug output. |
|
945 | + * |
|
946 | + * @return array |
|
947 | + */ |
|
948 | + private function _expandSniffDirectory($directory, $depth=0) |
|
949 | + { |
|
950 | + $sniffs = array(); |
|
951 | + |
|
952 | + if (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') === true) { |
|
953 | + $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS); |
|
954 | + } else { |
|
955 | + $rdi = new RecursiveDirectoryIterator($directory); |
|
956 | + } |
|
957 | + |
|
958 | + $di = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD); |
|
959 | + |
|
960 | + $dirLen = strlen($directory); |
|
961 | + |
|
962 | + foreach ($di as $file) { |
|
963 | + $filename = $file->getFilename(); |
|
964 | + |
|
965 | + // Skip hidden files. |
|
966 | + if (substr($filename, 0, 1) === '.') { |
|
967 | + continue; |
|
968 | + } |
|
969 | + |
|
970 | + // We are only interested in PHP and sniff files. |
|
971 | + $fileParts = explode('.', $filename); |
|
972 | + if (array_pop($fileParts) !== 'php') { |
|
973 | + continue; |
|
974 | + } |
|
975 | + |
|
976 | + $basename = basename($filename, '.php'); |
|
977 | + if (substr($basename, -5) !== 'Sniff') { |
|
978 | + continue; |
|
979 | + } |
|
980 | + |
|
981 | + $path = $file->getPathname(); |
|
982 | + |
|
983 | + // Skip files in hidden directories within the Sniffs directory of this |
|
984 | + // standard. We use the offset with strpos() to allow hidden directories |
|
985 | + // before, valid example: |
|
986 | + // /home/foo/.composer/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/... |
|
987 | + if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) { |
|
988 | + continue; |
|
989 | + } |
|
990 | + |
|
991 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
992 | + echo str_repeat("\t", $depth); |
|
993 | + echo "\t\t=> $path".PHP_EOL; |
|
994 | + } |
|
995 | + |
|
996 | + $sniffs[] = $path; |
|
997 | + }//end foreach |
|
998 | + |
|
999 | + return $sniffs; |
|
1000 | + |
|
1001 | + }//end _expandSniffDirectory() |
|
1002 | + |
|
1003 | + |
|
1004 | + /** |
|
1005 | + * Expands a ruleset reference into a list of sniff files. |
|
1006 | + * |
|
1007 | + * @param string $ref The reference from the ruleset XML file. |
|
1008 | + * @param string $rulesetDir The directory of the ruleset XML file, used to |
|
1009 | + * evaluate relative paths. |
|
1010 | + * @param int $depth How many nested processing steps we are in. This |
|
1011 | + * is only used for debug output. |
|
1012 | + * |
|
1013 | + * @return array |
|
1014 | + * @throws PHP_CodeSniffer_Exception If the reference is invalid. |
|
1015 | + */ |
|
1016 | + private function _expandRulesetReference($ref, $rulesetDir, $depth=0) |
|
1017 | + { |
|
1018 | + // Ignore internal sniffs codes as they are used to only |
|
1019 | + // hide and change internal messages. |
|
1020 | + if (substr($ref, 0, 9) === 'Internal.') { |
|
1021 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1022 | + echo str_repeat("\t", $depth); |
|
1023 | + echo "\t\t* ignoring internal sniff code *".PHP_EOL; |
|
1024 | + } |
|
1025 | + |
|
1026 | + return array(); |
|
1027 | + } |
|
1028 | + |
|
1029 | + // As sniffs can't begin with a full stop, assume references in |
|
1030 | + // this format are relative paths and attempt to convert them |
|
1031 | + // to absolute paths. If this fails, let the reference run through |
|
1032 | + // the normal checks and have it fail as normal. |
|
1033 | + if (substr($ref, 0, 1) === '.') { |
|
1034 | + $realpath = self::realpath($rulesetDir.'/'.$ref); |
|
1035 | + if ($realpath !== false) { |
|
1036 | + $ref = $realpath; |
|
1037 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1038 | + echo str_repeat("\t", $depth); |
|
1039 | + echo "\t\t=> $ref".PHP_EOL; |
|
1040 | + } |
|
1041 | + } |
|
1042 | + } |
|
1043 | + |
|
1044 | + // As sniffs can't begin with a tilde, assume references in |
|
1045 | + // this format at relative to the user's home directory. |
|
1046 | + if (substr($ref, 0, 2) === '~/') { |
|
1047 | + $realpath = self::realpath($ref); |
|
1048 | + if ($realpath !== false) { |
|
1049 | + $ref = $realpath; |
|
1050 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1051 | + echo str_repeat("\t", $depth); |
|
1052 | + echo "\t\t=> $ref".PHP_EOL; |
|
1053 | + } |
|
1054 | + } |
|
1055 | + } |
|
1056 | + |
|
1057 | + if (is_file($ref) === true) { |
|
1058 | + if (substr($ref, -9) === 'Sniff.php') { |
|
1059 | + // A single external sniff. |
|
1060 | + self::$rulesetDirs[] = dirname(dirname(dirname($ref))); |
|
1061 | + return array($ref); |
|
1062 | + } |
|
1063 | + } else { |
|
1064 | + // See if this is a whole standard being referenced. |
|
1065 | + $path = $this->getInstalledStandardPath($ref); |
|
1066 | + if (self::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { |
|
1067 | + // If the ruleset exists inside the phar file, use it. |
|
1068 | + if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1069 | + $path = $path.DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
1070 | + } else { |
|
1071 | + $path = null; |
|
1072 | + } |
|
1073 | + } |
|
1074 | + |
|
1075 | + if ($path !== null) { |
|
1076 | + $ref = $path; |
|
1077 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1078 | + echo str_repeat("\t", $depth); |
|
1079 | + echo "\t\t=> $ref".PHP_EOL; |
|
1080 | + } |
|
1081 | + } else if (is_dir($ref) === false) { |
|
1082 | + // Work out the sniff path. |
|
1083 | + $sepPos = strpos($ref, DIRECTORY_SEPARATOR); |
|
1084 | + if ($sepPos !== false) { |
|
1085 | + $stdName = substr($ref, 0, $sepPos); |
|
1086 | + $path = substr($ref, $sepPos); |
|
1087 | + } else { |
|
1088 | + $parts = explode('.', $ref); |
|
1089 | + $stdName = $parts[0]; |
|
1090 | + if (count($parts) === 1) { |
|
1091 | + // A whole standard? |
|
1092 | + $path = ''; |
|
1093 | + } else if (count($parts) === 2) { |
|
1094 | + // A directory of sniffs? |
|
1095 | + $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1]; |
|
1096 | + } else { |
|
1097 | + // A single sniff? |
|
1098 | + $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php'; |
|
1099 | + } |
|
1100 | + } |
|
1101 | + |
|
1102 | + $newRef = false; |
|
1103 | + $stdPath = $this->getInstalledStandardPath($stdName); |
|
1104 | + if ($stdPath !== null && $path !== '') { |
|
1105 | + if (self::isPharFile($stdPath) === true |
|
1106 | + && strpos($stdPath, 'ruleset.xml') === false |
|
1107 | + ) { |
|
1108 | + // Phar files can only return the directory, |
|
1109 | + // since ruleset can be omitted if building one standard. |
|
1110 | + $newRef = self::realpath($stdPath.$path); |
|
1111 | + } else { |
|
1112 | + $newRef = self::realpath(dirname($stdPath).$path); |
|
1113 | + } |
|
1114 | + } |
|
1115 | + |
|
1116 | + if ($newRef === false) { |
|
1117 | + // The sniff is not locally installed, so check if it is being |
|
1118 | + // referenced as a remote sniff outside the install. We do this |
|
1119 | + // by looking through all directories where we have found ruleset |
|
1120 | + // files before, looking for ones for this particular standard, |
|
1121 | + // and seeing if it is in there. |
|
1122 | + foreach (self::$rulesetDirs as $dir) { |
|
1123 | + if (strtolower(basename($dir)) !== strtolower($stdName)) { |
|
1124 | + continue; |
|
1125 | + } |
|
1126 | + |
|
1127 | + $newRef = self::realpath($dir.$path); |
|
1128 | + |
|
1129 | + if ($newRef !== false) { |
|
1130 | + $ref = $newRef; |
|
1131 | + } |
|
1132 | + } |
|
1133 | + } else { |
|
1134 | + $ref = $newRef; |
|
1135 | + } |
|
1136 | + |
|
1137 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1138 | + echo str_repeat("\t", $depth); |
|
1139 | + echo "\t\t=> $ref".PHP_EOL; |
|
1140 | + } |
|
1141 | + }//end if |
|
1142 | + }//end if |
|
1143 | + |
|
1144 | + if (is_dir($ref) === true) { |
|
1145 | + if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1146 | + // We are referencing an external coding standard. |
|
1147 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1148 | + echo str_repeat("\t", $depth); |
|
1149 | + echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL; |
|
1150 | + } |
|
1151 | + |
|
1152 | + return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2)); |
|
1153 | + } else { |
|
1154 | + // We are referencing a whole directory of sniffs. |
|
1155 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1156 | + echo str_repeat("\t", $depth); |
|
1157 | + echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL; |
|
1158 | + echo str_repeat("\t", $depth); |
|
1159 | + echo "\t\tAdding sniff files from directory".PHP_EOL; |
|
1160 | + } |
|
1161 | + |
|
1162 | + return $this->_expandSniffDirectory($ref, ($depth + 1)); |
|
1163 | + } |
|
1164 | + } else { |
|
1165 | + if (is_file($ref) === false) { |
|
1166 | + $error = "Referenced sniff \"$ref\" does not exist"; |
|
1167 | + throw new PHP_CodeSniffer_Exception($error); |
|
1168 | + } |
|
1169 | + |
|
1170 | + if (substr($ref, -9) === 'Sniff.php') { |
|
1171 | + // A single sniff. |
|
1172 | + return array($ref); |
|
1173 | + } else { |
|
1174 | + // Assume an external ruleset.xml file. |
|
1175 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1176 | + echo str_repeat("\t", $depth); |
|
1177 | + echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL; |
|
1178 | + } |
|
1179 | + |
|
1180 | + return $this->processRuleset($ref, ($depth + 2)); |
|
1181 | + } |
|
1182 | + }//end if |
|
1183 | + |
|
1184 | + }//end _expandRulesetReference() |
|
1185 | + |
|
1186 | + |
|
1187 | + /** |
|
1188 | + * Processes a rule from a ruleset XML file, overriding built-in defaults. |
|
1189 | + * |
|
1190 | + * @param SimpleXMLElement $rule The rule object from a ruleset XML file. |
|
1191 | + * @param int $depth How many nested processing steps we are in. |
|
1192 | + * This is only used for debug output. |
|
1193 | + * |
|
1194 | + * @return void |
|
1195 | + */ |
|
1196 | + private function _processRule($rule, $depth=0) |
|
1197 | + { |
|
1198 | + $code = (string) $rule['ref']; |
|
1199 | + |
|
1200 | + // Custom severity. |
|
1201 | + if (isset($rule->severity) === true |
|
1202 | + && $this->_shouldProcessElement($rule->severity) === true |
|
1203 | + ) { |
|
1204 | + if (isset($this->ruleset[$code]) === false) { |
|
1205 | + $this->ruleset[$code] = array(); |
|
1206 | + } |
|
1207 | + |
|
1208 | + $this->ruleset[$code]['severity'] = (int) $rule->severity; |
|
1209 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1210 | + echo str_repeat("\t", $depth); |
|
1211 | + echo "\t\t=> severity set to ".(int) $rule->severity.PHP_EOL; |
|
1212 | + } |
|
1213 | + } |
|
1214 | + |
|
1215 | + // Custom message type. |
|
1216 | + if (isset($rule->type) === true |
|
1217 | + && $this->_shouldProcessElement($rule->type) === true |
|
1218 | + ) { |
|
1219 | + if (isset($this->ruleset[$code]) === false) { |
|
1220 | + $this->ruleset[$code] = array(); |
|
1221 | + } |
|
1222 | + |
|
1223 | + $this->ruleset[$code]['type'] = (string) $rule->type; |
|
1224 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1225 | + echo str_repeat("\t", $depth); |
|
1226 | + echo "\t\t=> message type set to ".(string) $rule->type.PHP_EOL; |
|
1227 | + } |
|
1228 | + } |
|
1229 | + |
|
1230 | + // Custom message. |
|
1231 | + if (isset($rule->message) === true |
|
1232 | + && $this->_shouldProcessElement($rule->message) === true |
|
1233 | + ) { |
|
1234 | + if (isset($this->ruleset[$code]) === false) { |
|
1235 | + $this->ruleset[$code] = array(); |
|
1236 | + } |
|
1237 | + |
|
1238 | + $this->ruleset[$code]['message'] = (string) $rule->message; |
|
1239 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1240 | + echo str_repeat("\t", $depth); |
|
1241 | + echo "\t\t=> message set to ".(string) $rule->message.PHP_EOL; |
|
1242 | + } |
|
1243 | + } |
|
1244 | + |
|
1245 | + // Custom properties. |
|
1246 | + if (isset($rule->properties) === true |
|
1247 | + && $this->_shouldProcessElement($rule->properties) === true |
|
1248 | + ) { |
|
1249 | + foreach ($rule->properties->property as $prop) { |
|
1250 | + if ($this->_shouldProcessElement($prop) === false) { |
|
1251 | + continue; |
|
1252 | + } |
|
1253 | + |
|
1254 | + if (isset($this->ruleset[$code]) === false) { |
|
1255 | + $this->ruleset[$code] = array( |
|
1256 | + 'properties' => array(), |
|
1257 | + ); |
|
1258 | + } else if (isset($this->ruleset[$code]['properties']) === false) { |
|
1259 | + $this->ruleset[$code]['properties'] = array(); |
|
1260 | + } |
|
1261 | + |
|
1262 | + $name = (string) $prop['name']; |
|
1263 | + if (isset($prop['type']) === true |
|
1264 | + && (string) $prop['type'] === 'array' |
|
1265 | + ) { |
|
1266 | + $value = (string) $prop['value']; |
|
1267 | + $values = array(); |
|
1268 | + foreach (explode(',', $value) as $val) { |
|
1269 | + $v = ''; |
|
1270 | + |
|
1271 | + list($k,$v) = explode('=>', $val.'=>'); |
|
1272 | + if ($v !== '') { |
|
1273 | + $values[$k] = $v; |
|
1274 | + } else { |
|
1275 | + $values[] = $k; |
|
1276 | + } |
|
1277 | + } |
|
1278 | + |
|
1279 | + $this->ruleset[$code]['properties'][$name] = $values; |
|
1280 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1281 | + echo str_repeat("\t", $depth); |
|
1282 | + echo "\t\t=> array property \"$name\" set to \"$value\"".PHP_EOL; |
|
1283 | + } |
|
1284 | + } else { |
|
1285 | + $this->ruleset[$code]['properties'][$name] = (string) $prop['value']; |
|
1286 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1287 | + echo str_repeat("\t", $depth); |
|
1288 | + echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'.PHP_EOL; |
|
1289 | + } |
|
1290 | + }//end if |
|
1291 | + }//end foreach |
|
1292 | + }//end if |
|
1293 | + |
|
1294 | + // Ignore patterns. |
|
1295 | + foreach ($rule->{'exclude-pattern'} as $pattern) { |
|
1296 | + if ($this->_shouldProcessElement($pattern) === false) { |
|
1297 | + continue; |
|
1298 | + } |
|
1299 | + |
|
1300 | + if (isset($this->ignorePatterns[$code]) === false) { |
|
1301 | + $this->ignorePatterns[$code] = array(); |
|
1302 | + } |
|
1303 | + |
|
1304 | + if (isset($pattern['type']) === false) { |
|
1305 | + $pattern['type'] = 'absolute'; |
|
1306 | + } |
|
1307 | + |
|
1308 | + $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type']; |
|
1309 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1310 | + echo str_repeat("\t", $depth); |
|
1311 | + echo "\t\t=> added sniff-specific ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
1312 | + } |
|
1313 | + } |
|
1314 | + |
|
1315 | + }//end _processRule() |
|
1316 | + |
|
1317 | + |
|
1318 | + /** |
|
1319 | + * Determine if an element should be processed or ignored. |
|
1320 | + * |
|
1321 | + * @param SimpleXMLElement $element An object from a ruleset XML file. |
|
1322 | + * @param int $depth How many nested processing steps we are in. |
|
1323 | + * This is only used for debug output. |
|
1324 | + * |
|
1325 | + * @return bool |
|
1326 | + */ |
|
1327 | + private function _shouldProcessElement($element, $depth=0) |
|
1328 | + { |
|
1329 | + if (isset($element['phpcbf-only']) === false |
|
1330 | + && isset($element['phpcs-only']) === false |
|
1331 | + ) { |
|
1332 | + // No exceptions are being made. |
|
1333 | + return true; |
|
1334 | + } |
|
1335 | + |
|
1336 | + if (PHP_CODESNIFFER_CBF === true |
|
1337 | + && isset($element['phpcbf-only']) === true |
|
1338 | + && (string) $element['phpcbf-only'] === 'true' |
|
1339 | + ) { |
|
1340 | + return true; |
|
1341 | + } |
|
1342 | + |
|
1343 | + if (PHP_CODESNIFFER_CBF === false |
|
1344 | + && isset($element['phpcs-only']) === true |
|
1345 | + && (string) $element['phpcs-only'] === 'true' |
|
1346 | + ) { |
|
1347 | + return true; |
|
1348 | + } |
|
1349 | + |
|
1350 | + return false; |
|
1351 | + |
|
1352 | + }//end _shouldProcessElement() |
|
1353 | + |
|
1354 | + |
|
1355 | + /** |
|
1356 | + * Loads and stores sniffs objects used for sniffing files. |
|
1357 | + * |
|
1358 | + * @param array $files Paths to the sniff files to register. |
|
1359 | + * @param array $restrictions The sniff class names to restrict the allowed |
|
1360 | + * listeners to. |
|
1361 | + * @param array $exclusions The sniff class names to exclude from the |
|
1362 | + * listeners list. |
|
1363 | + * |
|
1364 | + * @return void |
|
1365 | + * @throws PHP_CodeSniffer_Exception If a sniff file path is invalid. |
|
1366 | + */ |
|
1367 | + public function registerSniffs($files, $restrictions, $exclusions) |
|
1368 | + { |
|
1369 | + $listeners = array(); |
|
1370 | + |
|
1371 | + foreach ($files as $file) { |
|
1372 | + // Work out where the position of /StandardName/Sniffs/... is |
|
1373 | + // so we can determine what the class will be called. |
|
1374 | + $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR); |
|
1375 | + if ($sniffPos === false) { |
|
1376 | + continue; |
|
1377 | + } |
|
1378 | + |
|
1379 | + $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR); |
|
1380 | + if ($slashPos === false) { |
|
1381 | + continue; |
|
1382 | + } |
|
1383 | + |
|
1384 | + $className = substr($file, ($slashPos + 1)); |
|
1385 | + |
|
1386 | + if (substr_count($className, DIRECTORY_SEPARATOR) !== 3) { |
|
1387 | + throw new PHP_CodeSniffer_Exception("Sniff file $className is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory"); |
|
1388 | + } |
|
1389 | + |
|
1390 | + $className = substr($className, 0, -4); |
|
1391 | + $className = str_replace(DIRECTORY_SEPARATOR, '_', $className); |
|
1392 | + |
|
1393 | + // If they have specified a list of sniffs to restrict to, check |
|
1394 | + // to see if this sniff is allowed. |
|
1395 | + if (empty($restrictions) === false |
|
1396 | + && in_array(strtolower($className), $restrictions) === false |
|
1397 | + ) { |
|
1398 | + continue; |
|
1399 | + } |
|
1400 | + |
|
1401 | + // If they have specified a list of sniffs to exclude, check |
|
1402 | + // to see if this sniff is allowed. |
|
1403 | + if (empty($exclusions) === false |
|
1404 | + && in_array(strtolower($className), $exclusions) === true |
|
1405 | + ) { |
|
1406 | + continue; |
|
1407 | + } |
|
1408 | + |
|
1409 | + include_once $file; |
|
1410 | + |
|
1411 | + // Support the use of PHP namespaces. If the class name we included |
|
1412 | + // contains namespace separators instead of underscores, use this as the |
|
1413 | + // class name from now on. |
|
1414 | + $classNameNS = str_replace('_', '\\', $className); |
|
1415 | + if (class_exists($classNameNS, false) === true) { |
|
1416 | + $className = $classNameNS; |
|
1417 | + } |
|
1418 | + |
|
1419 | + // Skip abstract classes. |
|
1420 | + $reflection = new ReflectionClass($className); |
|
1421 | + if ($reflection->isAbstract() === true) { |
|
1422 | + continue; |
|
1423 | + } |
|
1424 | + |
|
1425 | + $listeners[$className] = $className; |
|
1426 | + |
|
1427 | + if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
1428 | + echo "Registered $className".PHP_EOL; |
|
1429 | + } |
|
1430 | + }//end foreach |
|
1431 | + |
|
1432 | + $this->sniffs = $listeners; |
|
1433 | + |
|
1434 | + }//end registerSniffs() |
|
1435 | + |
|
1436 | + |
|
1437 | + /** |
|
1438 | + * Populates the array of PHP_CodeSniffer_Sniff's for this file. |
|
1439 | + * |
|
1440 | + * @return void |
|
1441 | + * @throws PHP_CodeSniffer_Exception If sniff registration fails. |
|
1442 | + */ |
|
1443 | + public function populateTokenListeners() |
|
1444 | + { |
|
1445 | + // Construct a list of listeners indexed by token being listened for. |
|
1446 | + $this->_tokenListeners = array(); |
|
1447 | + |
|
1448 | + foreach ($this->sniffs as $listenerClass) { |
|
1449 | + // Work out the internal code for this sniff. Detect usage of namespace |
|
1450 | + // separators instead of underscores to support PHP namespaces. |
|
1451 | + if (strstr($listenerClass, '\\') === false) { |
|
1452 | + $parts = explode('_', $listenerClass); |
|
1453 | + } else { |
|
1454 | + $parts = explode('\\', $listenerClass); |
|
1455 | + } |
|
1456 | + |
|
1457 | + $code = $parts[0].'.'.$parts[2].'.'.$parts[3]; |
|
1458 | + $code = substr($code, 0, -5); |
|
1459 | + |
|
1460 | + $this->listeners[$listenerClass] = new $listenerClass(); |
|
1461 | + $this->sniffCodes[$code] = $listenerClass; |
|
1462 | + |
|
1463 | + // Set custom properties. |
|
1464 | + if (isset($this->ruleset[$code]['properties']) === true) { |
|
1465 | + foreach ($this->ruleset[$code]['properties'] as $name => $value) { |
|
1466 | + $this->setSniffProperty($listenerClass, $name, $value); |
|
1467 | + } |
|
1468 | + } |
|
1469 | + |
|
1470 | + $tokenizers = array(); |
|
1471 | + $vars = get_class_vars($listenerClass); |
|
1472 | + if (isset($vars['supportedTokenizers']) === true) { |
|
1473 | + foreach ($vars['supportedTokenizers'] as $tokenizer) { |
|
1474 | + $tokenizers[$tokenizer] = $tokenizer; |
|
1475 | + } |
|
1476 | + } else { |
|
1477 | + $tokenizers = array('PHP' => 'PHP'); |
|
1478 | + } |
|
1479 | + |
|
1480 | + $tokens = $this->listeners[$listenerClass]->register(); |
|
1481 | + if (is_array($tokens) === false) { |
|
1482 | + $msg = "Sniff $listenerClass register() method must return an array"; |
|
1483 | + throw new PHP_CodeSniffer_Exception($msg); |
|
1484 | + } |
|
1485 | + |
|
1486 | + $parts = explode('_', str_replace('\\', '_', $listenerClass)); |
|
1487 | + $listenerSource = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5); |
|
1488 | + $ignorePatterns = array(); |
|
1489 | + $patterns = $this->getIgnorePatterns($listenerSource); |
|
1490 | + foreach ($patterns as $pattern => $type) { |
|
1491 | + // While there is support for a type of each pattern |
|
1492 | + // (absolute or relative) we don't actually support it here. |
|
1493 | + $replacements = array( |
|
1494 | + '\\,' => ',', |
|
1495 | + '*' => '.*', |
|
1496 | + ); |
|
1497 | + |
|
1498 | + $ignorePatterns[] = strtr($pattern, $replacements); |
|
1499 | + } |
|
1500 | + |
|
1501 | + foreach ($tokens as $token) { |
|
1502 | + if (isset($this->_tokenListeners[$token]) === false) { |
|
1503 | + $this->_tokenListeners[$token] = array(); |
|
1504 | + } |
|
1505 | + |
|
1506 | + if (isset($this->_tokenListeners[$token][$listenerClass]) === false) { |
|
1507 | + $this->_tokenListeners[$token][$listenerClass] = array( |
|
1508 | + 'class' => $listenerClass, |
|
1509 | + 'source' => $listenerSource, |
|
1510 | + 'tokenizers' => $tokenizers, |
|
1511 | + 'ignore' => $ignorePatterns, |
|
1512 | + ); |
|
1513 | + } |
|
1514 | + } |
|
1515 | + }//end foreach |
|
1516 | + |
|
1517 | + }//end populateTokenListeners() |
|
1518 | + |
|
1519 | + |
|
1520 | + /** |
|
1521 | + * Set a single property for a sniff. |
|
1522 | + * |
|
1523 | + * @param string $listenerClass The class name of the sniff. |
|
1524 | + * @param string $name The name of the property to change. |
|
1525 | + * @param string $value The new value of the property. |
|
1526 | + * |
|
1527 | + * @return void |
|
1528 | + */ |
|
1529 | + public function setSniffProperty($listenerClass, $name, $value) |
|
1530 | + { |
|
1531 | + // Setting a property for a sniff we are not using. |
|
1532 | + if (isset($this->listeners[$listenerClass]) === false) { |
|
1533 | + return; |
|
1534 | + } |
|
1535 | + |
|
1536 | + $name = trim($name); |
|
1537 | + if (is_string($value) === true) { |
|
1538 | + $value = trim($value); |
|
1539 | + } |
|
1540 | + |
|
1541 | + // Special case for booleans. |
|
1542 | + if ($value === 'true') { |
|
1543 | + $value = true; |
|
1544 | + } else if ($value === 'false') { |
|
1545 | + $value = false; |
|
1546 | + } |
|
1547 | + |
|
1548 | + $this->listeners[$listenerClass]->$name = $value; |
|
1549 | + |
|
1550 | + }//end setSniffProperty() |
|
1551 | + |
|
1552 | + |
|
1553 | + /** |
|
1554 | + * Get a list of files that will be processed. |
|
1555 | + * |
|
1556 | + * If passed directories, this method will find all files within them. |
|
1557 | + * The method will also perform file extension and ignore pattern filtering. |
|
1558 | + * |
|
1559 | + * @param string $paths A list of file or directory paths to process. |
|
1560 | + * @param boolean $local If true, only process 1 level of files in directories |
|
1561 | + * |
|
1562 | + * @return array |
|
1563 | + * @throws Exception If there was an error opening a directory. |
|
1564 | + * @see shouldProcessFile() |
|
1565 | + */ |
|
1566 | + public function getFilesToProcess($paths, $local=false) |
|
1567 | + { |
|
1568 | + $files = array(); |
|
1569 | + |
|
1570 | + foreach ($paths as $path) { |
|
1571 | + if (is_dir($path) === true || self::isPharFile($path) === true) { |
|
1572 | + if (self::isPharFile($path) === true) { |
|
1573 | + $path = 'phar://'.$path; |
|
1574 | + } |
|
1575 | + |
|
1576 | + if ($local === true) { |
|
1577 | + $di = new DirectoryIterator($path); |
|
1578 | + } else { |
|
1579 | + $di = new RecursiveIteratorIterator( |
|
1580 | + new RecursiveDirectoryIterator($path), |
|
1581 | + 0, |
|
1582 | + RecursiveIteratorIterator::CATCH_GET_CHILD |
|
1583 | + ); |
|
1584 | + } |
|
1585 | + |
|
1586 | + foreach ($di as $file) { |
|
1587 | + // Check if the file exists after all symlinks are resolved. |
|
1588 | + $filePath = self::realpath($file->getPathname()); |
|
1589 | + if ($filePath === false) { |
|
1590 | + continue; |
|
1591 | + } |
|
1592 | + |
|
1593 | + if (is_dir($filePath) === true) { |
|
1594 | + continue; |
|
1595 | + } |
|
1596 | + |
|
1597 | + if ($this->shouldProcessFile($file->getPathname(), $path) === false) { |
|
1598 | + continue; |
|
1599 | + } |
|
1600 | + |
|
1601 | + $files[] = $file->getPathname(); |
|
1602 | + }//end foreach |
|
1603 | + } else { |
|
1604 | + if ($this->shouldIgnoreFile($path, dirname($path)) === true) { |
|
1605 | + continue; |
|
1606 | + } |
|
1607 | + |
|
1608 | + $files[] = $path; |
|
1609 | + }//end if |
|
1610 | + }//end foreach |
|
1611 | + |
|
1612 | + return $files; |
|
1613 | + |
|
1614 | + }//end getFilesToProcess() |
|
1615 | + |
|
1616 | + |
|
1617 | + /** |
|
1618 | + * Checks filtering rules to see if a file should be checked. |
|
1619 | + * |
|
1620 | + * Checks both file extension filters and path ignore filters. |
|
1621 | + * |
|
1622 | + * @param string $path The path to the file being checked. |
|
1623 | + * @param string $basedir The directory to use for relative path checks. |
|
1624 | + * |
|
1625 | + * @return bool |
|
1626 | + */ |
|
1627 | + public function shouldProcessFile($path, $basedir) |
|
1628 | + { |
|
1629 | + // Check that the file's extension is one we are checking. |
|
1630 | + // We are strict about checking the extension and we don't |
|
1631 | + // let files through with no extension or that start with a dot. |
|
1632 | + $fileName = basename($path); |
|
1633 | + $fileParts = explode('.', $fileName); |
|
1634 | + if ($fileParts[0] === $fileName || $fileParts[0] === '') { |
|
1635 | + return false; |
|
1636 | + } |
|
1637 | + |
|
1638 | + // Checking multi-part file extensions, so need to create a |
|
1639 | + // complete extension list and make sure one is allowed. |
|
1640 | + $extensions = array(); |
|
1641 | + array_shift($fileParts); |
|
1642 | + foreach ($fileParts as $part) { |
|
1643 | + $extensions[implode('.', $fileParts)] = 1; |
|
1644 | + array_shift($fileParts); |
|
1645 | + } |
|
1646 | + |
|
1647 | + $matches = array_intersect_key($extensions, $this->allowedFileExtensions); |
|
1648 | + if (empty($matches) === true) { |
|
1649 | + return false; |
|
1650 | + } |
|
1651 | + |
|
1652 | + // If the file's path matches one of our ignore patterns, skip it. |
|
1653 | + if ($this->shouldIgnoreFile($path, $basedir) === true) { |
|
1654 | + return false; |
|
1655 | + } |
|
1656 | + |
|
1657 | + return true; |
|
1658 | + |
|
1659 | + }//end shouldProcessFile() |
|
1660 | + |
|
1661 | + |
|
1662 | + /** |
|
1663 | + * Checks filtering rules to see if a file should be ignored. |
|
1664 | + * |
|
1665 | + * @param string $path The path to the file being checked. |
|
1666 | + * @param string $basedir The directory to use for relative path checks. |
|
1667 | + * |
|
1668 | + * @return bool |
|
1669 | + */ |
|
1670 | + public function shouldIgnoreFile($path, $basedir) |
|
1671 | + { |
|
1672 | + $relativePath = $path; |
|
1673 | + if (strpos($path, $basedir) === 0) { |
|
1674 | + // The +1 cuts off the directory separator as well. |
|
1675 | + $relativePath = substr($path, (strlen($basedir) + 1)); |
|
1676 | + } |
|
1677 | + |
|
1678 | + foreach ($this->ignorePatterns as $pattern => $type) { |
|
1679 | + if (is_array($type) === true) { |
|
1680 | + // A sniff specific ignore pattern. |
|
1681 | + continue; |
|
1682 | + } |
|
1683 | + |
|
1684 | + // Maintains backwards compatibility in case the ignore pattern does |
|
1685 | + // not have a relative/absolute value. |
|
1686 | + if (is_int($pattern) === true) { |
|
1687 | + $pattern = $type; |
|
1688 | + $type = 'absolute'; |
|
1689 | + } |
|
1690 | + |
|
1691 | + $replacements = array( |
|
1692 | + '\\,' => ',', |
|
1693 | + '*' => '.*', |
|
1694 | + ); |
|
1695 | + |
|
1696 | + // We assume a / directory separator, as do the exclude rules |
|
1697 | + // most developers write, so we need a special case for any system |
|
1698 | + // that is different. |
|
1699 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
1700 | + $replacements['/'] = '\\\\'; |
|
1701 | + } |
|
1702 | + |
|
1703 | + $pattern = strtr($pattern, $replacements); |
|
1704 | + |
|
1705 | + if ($type === 'relative') { |
|
1706 | + $testPath = $relativePath; |
|
1707 | + } else { |
|
1708 | + $testPath = $path; |
|
1709 | + } |
|
1710 | + |
|
1711 | + $pattern = '`'.$pattern.'`i'; |
|
1712 | + if (preg_match($pattern, $testPath) === 1) { |
|
1713 | + return true; |
|
1714 | + } |
|
1715 | + }//end foreach |
|
1716 | + |
|
1717 | + return false; |
|
1718 | + |
|
1719 | + }//end shouldIgnoreFile() |
|
1720 | + |
|
1721 | + |
|
1722 | + /** |
|
1723 | + * Run the code sniffs over a single given file. |
|
1724 | + * |
|
1725 | + * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it |
|
1726 | + * conforms with the standard. Returns the processed file object, or NULL |
|
1727 | + * if no file was processed due to error. |
|
1728 | + * |
|
1729 | + * @param string $file The file to process. |
|
1730 | + * @param string $contents The contents to parse. If NULL, the content |
|
1731 | + * is taken from the file system. |
|
1732 | + * |
|
1733 | + * @return PHP_CodeSniffer_File |
|
1734 | + * @throws PHP_CodeSniffer_Exception If the file could not be processed. |
|
1735 | + * @see _processFile() |
|
1736 | + */ |
|
1737 | + public function processFile($file, $contents=null) |
|
1738 | + { |
|
1739 | + if ($contents === null && file_exists($file) === false) { |
|
1740 | + throw new PHP_CodeSniffer_Exception("Source file $file does not exist"); |
|
1741 | + } |
|
1742 | + |
|
1743 | + $filePath = self::realpath($file); |
|
1744 | + if ($filePath === false) { |
|
1745 | + $filePath = $file; |
|
1746 | + } |
|
1747 | + |
|
1748 | + // Before we go and spend time tokenizing this file, just check |
|
1749 | + // to see if there is a tag up top to indicate that the whole |
|
1750 | + // file should be ignored. It must be on one of the first two lines. |
|
1751 | + $firstContent = $contents; |
|
1752 | + if ($contents === null && is_readable($filePath) === true) { |
|
1753 | + $handle = fopen($filePath, 'r'); |
|
1754 | + stream_set_blocking($handle, true); |
|
1755 | + if ($handle !== false) { |
|
1756 | + $firstContent = fgets($handle); |
|
1757 | + $firstContent .= fgets($handle); |
|
1758 | + fclose($handle); |
|
1759 | + |
|
1760 | + if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) { |
|
1761 | + // We are ignoring the whole file. |
|
1762 | + if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
1763 | + echo 'Ignoring '.basename($filePath).PHP_EOL; |
|
1764 | + } |
|
1765 | + |
|
1766 | + return null; |
|
1767 | + } |
|
1768 | + } |
|
1769 | + }//end if |
|
1770 | + |
|
1771 | + try { |
|
1772 | + $phpcsFile = $this->_processFile($file, $contents); |
|
1773 | + } catch (Exception $e) { |
|
1774 | + $trace = $e->getTrace(); |
|
1775 | + |
|
1776 | + $filename = $trace[0]['args'][0]; |
|
1777 | + if (is_object($filename) === true |
|
1778 | + && get_class($filename) === 'PHP_CodeSniffer_File' |
|
1779 | + ) { |
|
1780 | + $filename = $filename->getFilename(); |
|
1781 | + } else if (is_numeric($filename) === true) { |
|
1782 | + // See if we can find the PHP_CodeSniffer_File object. |
|
1783 | + foreach ($trace as $data) { |
|
1784 | + if (isset($data['args'][0]) === true |
|
1785 | + && ($data['args'][0] instanceof PHP_CodeSniffer_File) === true |
|
1786 | + ) { |
|
1787 | + $filename = $data['args'][0]->getFilename(); |
|
1788 | + } |
|
1789 | + } |
|
1790 | + } else if (is_string($filename) === false) { |
|
1791 | + $filename = (string) $filename; |
|
1792 | + } |
|
1793 | + |
|
1794 | + $errorMessage = '"'.$e->getMessage().'" at '.$e->getFile().':'.$e->getLine(); |
|
1795 | + $error = "An error occurred during processing; checking has been aborted. The error message was: $errorMessage"; |
|
1796 | + |
|
1797 | + $phpcsFile = new PHP_CodeSniffer_File( |
|
1798 | + $filename, |
|
1799 | + $this->_tokenListeners, |
|
1800 | + $this->ruleset, |
|
1801 | + $this |
|
1802 | + ); |
|
1803 | + |
|
1804 | + $phpcsFile->addError($error, null); |
|
1805 | + }//end try |
|
1806 | + |
|
1807 | + $cliValues = $this->cli->getCommandLineValues(); |
|
1808 | + |
|
1809 | + if (PHP_CODESNIFFER_INTERACTIVE === false) { |
|
1810 | + // Cache the report data for this file so we can unset it to save memory. |
|
1811 | + $this->reporting->cacheFileReport($phpcsFile, $cliValues); |
|
1812 | + $phpcsFile->cleanUp(); |
|
1813 | + return $phpcsFile; |
|
1814 | + } |
|
1815 | + |
|
1816 | + /* |
|
1817 | 1817 | Running interactively. |
1818 | 1818 | Print the error report for the current file and then wait for user input. |
1819 | 1819 | */ |
1820 | 1820 | |
1821 | - // Get current violations and then clear the list to make sure |
|
1822 | - // we only print violations for a single file each time. |
|
1823 | - $numErrors = null; |
|
1824 | - while ($numErrors !== 0) { |
|
1825 | - $numErrors = ($phpcsFile->getErrorCount() + $phpcsFile->getWarningCount()); |
|
1826 | - if ($numErrors === 0) { |
|
1827 | - continue; |
|
1828 | - } |
|
1829 | - |
|
1830 | - $reportClass = $this->reporting->factory('full'); |
|
1831 | - $reportData = $this->reporting->prepareFileReport($phpcsFile); |
|
1832 | - $reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']); |
|
1833 | - |
|
1834 | - echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
1835 | - $input = fgets(STDIN); |
|
1836 | - $input = trim($input); |
|
1837 | - |
|
1838 | - switch ($input) { |
|
1839 | - case 's': |
|
1840 | - break(2); |
|
1841 | - case 'q': |
|
1842 | - exit(0); |
|
1843 | - break; |
|
1844 | - default: |
|
1845 | - // Repopulate the sniffs because some of them save their state |
|
1846 | - // and only clear it when the file changes, but we are rechecking |
|
1847 | - // the same file. |
|
1848 | - $this->populateTokenListeners(); |
|
1849 | - $phpcsFile = $this->_processFile($file, $contents); |
|
1850 | - break; |
|
1851 | - } |
|
1852 | - }//end while |
|
1853 | - |
|
1854 | - return $phpcsFile; |
|
1855 | - |
|
1856 | - }//end processFile() |
|
1857 | - |
|
1858 | - |
|
1859 | - /** |
|
1860 | - * Process the sniffs for a single file. |
|
1861 | - * |
|
1862 | - * Does raw processing only. No interactive support or error checking. |
|
1863 | - * |
|
1864 | - * @param string $file The file to process. |
|
1865 | - * @param string $contents The contents to parse. If NULL, the content |
|
1866 | - * is taken from the file system. |
|
1867 | - * |
|
1868 | - * @return PHP_CodeSniffer_File |
|
1869 | - * @see processFile() |
|
1870 | - */ |
|
1871 | - private function _processFile($file, $contents) |
|
1872 | - { |
|
1873 | - $stdin = false; |
|
1874 | - $cliValues = $this->cli->getCommandLineValues(); |
|
1875 | - if (empty($cliValues['files']) === true) { |
|
1876 | - $stdin = true; |
|
1877 | - } |
|
1878 | - |
|
1879 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1880 | - $startTime = microtime(true); |
|
1881 | - echo 'Processing '.basename($file).' '; |
|
1882 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1883 | - echo PHP_EOL; |
|
1884 | - } |
|
1885 | - } |
|
1886 | - |
|
1887 | - $phpcsFile = new PHP_CodeSniffer_File( |
|
1888 | - $file, |
|
1889 | - $this->_tokenListeners, |
|
1890 | - $this->ruleset, |
|
1891 | - $this |
|
1892 | - ); |
|
1893 | - |
|
1894 | - $phpcsFile->start($contents); |
|
1895 | - |
|
1896 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1897 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
1898 | - if ($timeTaken < 1000) { |
|
1899 | - $timeTaken = round($timeTaken); |
|
1900 | - echo "DONE in {$timeTaken}ms"; |
|
1901 | - } else { |
|
1902 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
1903 | - echo "DONE in $timeTaken secs"; |
|
1904 | - } |
|
1905 | - |
|
1906 | - if (PHP_CODESNIFFER_CBF === true) { |
|
1907 | - $errors = $phpcsFile->getFixableCount(); |
|
1908 | - echo " ($errors fixable violations)".PHP_EOL; |
|
1909 | - } else { |
|
1910 | - $errors = $phpcsFile->getErrorCount(); |
|
1911 | - $warnings = $phpcsFile->getWarningCount(); |
|
1912 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
1913 | - } |
|
1914 | - } |
|
1915 | - |
|
1916 | - return $phpcsFile; |
|
1917 | - |
|
1918 | - }//end _processFile() |
|
1919 | - |
|
1920 | - |
|
1921 | - /** |
|
1922 | - * Generates documentation for a coding standard. |
|
1923 | - * |
|
1924 | - * @param string $standard The standard to generate docs for |
|
1925 | - * @param array $sniffs A list of sniffs to limit the docs to. |
|
1926 | - * @param string $generator The name of the generator class to use. |
|
1927 | - * |
|
1928 | - * @return void |
|
1929 | - */ |
|
1930 | - public function generateDocs($standard, array $sniffs=array(), $generator='Text') |
|
1931 | - { |
|
1932 | - if (class_exists('PHP_CodeSniffer_DocGenerators_'.$generator, true) === false) { |
|
1933 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_'.$generator.' not found'); |
|
1934 | - } |
|
1935 | - |
|
1936 | - $class = "PHP_CodeSniffer_DocGenerators_$generator"; |
|
1937 | - $generator = new $class($standard, $sniffs); |
|
1938 | - |
|
1939 | - $generator->generate(); |
|
1940 | - |
|
1941 | - }//end generateDocs() |
|
1942 | - |
|
1943 | - |
|
1944 | - /** |
|
1945 | - * Gets the array of PHP_CodeSniffer_Sniff's. |
|
1946 | - * |
|
1947 | - * @return PHP_CodeSniffer_Sniff[] |
|
1948 | - */ |
|
1949 | - public function getSniffs() |
|
1950 | - { |
|
1951 | - return $this->listeners; |
|
1952 | - |
|
1953 | - }//end getSniffs() |
|
1954 | - |
|
1955 | - |
|
1956 | - /** |
|
1957 | - * Gets the array of PHP_CodeSniffer_Sniff's indexed by token type. |
|
1958 | - * |
|
1959 | - * @return array |
|
1960 | - */ |
|
1961 | - public function getTokenSniffs() |
|
1962 | - { |
|
1963 | - return $this->_tokenListeners; |
|
1964 | - |
|
1965 | - }//end getTokenSniffs() |
|
1966 | - |
|
1967 | - |
|
1968 | - /** |
|
1969 | - * Returns true if the specified string is in the camel caps format. |
|
1970 | - * |
|
1971 | - * @param string $string The string the verify. |
|
1972 | - * @param boolean $classFormat If true, check to see if the string is in the |
|
1973 | - * class format. Class format strings must start |
|
1974 | - * with a capital letter and contain no |
|
1975 | - * underscores. |
|
1976 | - * @param boolean $public If true, the first character in the string |
|
1977 | - * must be an a-z character. If false, the |
|
1978 | - * character must be an underscore. This |
|
1979 | - * argument is only applicable if $classFormat |
|
1980 | - * is false. |
|
1981 | - * @param boolean $strict If true, the string must not have two capital |
|
1982 | - * letters next to each other. If false, a |
|
1983 | - * relaxed camel caps policy is used to allow |
|
1984 | - * for acronyms. |
|
1985 | - * |
|
1986 | - * @return boolean |
|
1987 | - */ |
|
1988 | - public static function isCamelCaps( |
|
1989 | - $string, |
|
1990 | - $classFormat=false, |
|
1991 | - $public=true, |
|
1992 | - $strict=true |
|
1993 | - ) { |
|
1994 | - // Check the first character first. |
|
1995 | - if ($classFormat === false) { |
|
1996 | - $legalFirstChar = ''; |
|
1997 | - if ($public === false) { |
|
1998 | - $legalFirstChar = '[_]'; |
|
1999 | - } |
|
2000 | - |
|
2001 | - if ($strict === false) { |
|
2002 | - // Can either start with a lowercase letter, or multiple uppercase |
|
2003 | - // in a row, representing an acronym. |
|
2004 | - $legalFirstChar .= '([A-Z]{2,}|[a-z])'; |
|
2005 | - } else { |
|
2006 | - $legalFirstChar .= '[a-z]'; |
|
2007 | - } |
|
2008 | - } else { |
|
2009 | - $legalFirstChar = '[A-Z]'; |
|
2010 | - } |
|
2011 | - |
|
2012 | - if (preg_match("/^$legalFirstChar/", $string) === 0) { |
|
2013 | - return false; |
|
2014 | - } |
|
2015 | - |
|
2016 | - // Check that the name only contains legal characters. |
|
2017 | - $legalChars = 'a-zA-Z0-9'; |
|
2018 | - if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) { |
|
2019 | - return false; |
|
2020 | - } |
|
2021 | - |
|
2022 | - if ($strict === true) { |
|
2023 | - // Check that there are not two capital letters next to each other. |
|
2024 | - $length = strlen($string); |
|
2025 | - $lastCharWasCaps = $classFormat; |
|
2026 | - |
|
2027 | - for ($i = 1; $i < $length; $i++) { |
|
2028 | - $ascii = ord($string{$i}); |
|
2029 | - if ($ascii >= 48 && $ascii <= 57) { |
|
2030 | - // The character is a number, so it cant be a capital. |
|
2031 | - $isCaps = false; |
|
2032 | - } else { |
|
2033 | - if (strtoupper($string{$i}) === $string{$i}) { |
|
2034 | - $isCaps = true; |
|
2035 | - } else { |
|
2036 | - $isCaps = false; |
|
2037 | - } |
|
2038 | - } |
|
2039 | - |
|
2040 | - if ($isCaps === true && $lastCharWasCaps === true) { |
|
2041 | - return false; |
|
2042 | - } |
|
2043 | - |
|
2044 | - $lastCharWasCaps = $isCaps; |
|
2045 | - } |
|
2046 | - }//end if |
|
2047 | - |
|
2048 | - return true; |
|
2049 | - |
|
2050 | - }//end isCamelCaps() |
|
2051 | - |
|
2052 | - |
|
2053 | - /** |
|
2054 | - * Returns true if the specified string is in the underscore caps format. |
|
2055 | - * |
|
2056 | - * @param string $string The string to verify. |
|
2057 | - * |
|
2058 | - * @return boolean |
|
2059 | - */ |
|
2060 | - public static function isUnderscoreName($string) |
|
2061 | - { |
|
2062 | - // If there are space in the name, it can't be valid. |
|
2063 | - if (strpos($string, ' ') !== false) { |
|
2064 | - return false; |
|
2065 | - } |
|
2066 | - |
|
2067 | - $validName = true; |
|
2068 | - $nameBits = explode('_', $string); |
|
2069 | - |
|
2070 | - if (preg_match('|^[A-Z]|', $string) === 0) { |
|
2071 | - // Name does not begin with a capital letter. |
|
2072 | - $validName = false; |
|
2073 | - } else { |
|
2074 | - foreach ($nameBits as $bit) { |
|
2075 | - if ($bit === '') { |
|
2076 | - continue; |
|
2077 | - } |
|
2078 | - |
|
2079 | - if ($bit{0} !== strtoupper($bit{0})) { |
|
2080 | - $validName = false; |
|
2081 | - break; |
|
2082 | - } |
|
2083 | - } |
|
2084 | - } |
|
2085 | - |
|
2086 | - return $validName; |
|
2087 | - |
|
2088 | - }//end isUnderscoreName() |
|
2089 | - |
|
2090 | - |
|
2091 | - /** |
|
2092 | - * Returns a valid variable type for param/var tag. |
|
2093 | - * |
|
2094 | - * If type is not one of the standard type, it must be a custom type. |
|
2095 | - * Returns the correct type name suggestion if type name is invalid. |
|
2096 | - * |
|
2097 | - * @param string $varType The variable type to process. |
|
2098 | - * |
|
2099 | - * @return string |
|
2100 | - */ |
|
2101 | - public static function suggestType($varType) |
|
2102 | - { |
|
2103 | - if ($varType === '') { |
|
2104 | - return ''; |
|
2105 | - } |
|
2106 | - |
|
2107 | - if (in_array($varType, self::$allowedTypes) === true) { |
|
2108 | - return $varType; |
|
2109 | - } else { |
|
2110 | - $lowerVarType = strtolower($varType); |
|
2111 | - switch ($lowerVarType) { |
|
2112 | - case 'bool': |
|
2113 | - case 'boolean': |
|
2114 | - return 'boolean'; |
|
2115 | - case 'double': |
|
2116 | - case 'real': |
|
2117 | - case 'float': |
|
2118 | - return 'float'; |
|
2119 | - case 'int': |
|
2120 | - case 'integer': |
|
2121 | - return 'integer'; |
|
2122 | - case 'array()': |
|
2123 | - case 'array': |
|
2124 | - return 'array'; |
|
2125 | - }//end switch |
|
2126 | - |
|
2127 | - if (strpos($lowerVarType, 'array(') !== false) { |
|
2128 | - // Valid array declaration: |
|
2129 | - // array, array(type), array(type1 => type2). |
|
2130 | - $matches = array(); |
|
2131 | - $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; |
|
2132 | - if (preg_match($pattern, $varType, $matches) !== 0) { |
|
2133 | - $type1 = ''; |
|
2134 | - if (isset($matches[1]) === true) { |
|
2135 | - $type1 = $matches[1]; |
|
2136 | - } |
|
2137 | - |
|
2138 | - $type2 = ''; |
|
2139 | - if (isset($matches[3]) === true) { |
|
2140 | - $type2 = $matches[3]; |
|
2141 | - } |
|
2142 | - |
|
2143 | - $type1 = self::suggestType($type1); |
|
2144 | - $type2 = self::suggestType($type2); |
|
2145 | - if ($type2 !== '') { |
|
2146 | - $type2 = ' => '.$type2; |
|
2147 | - } |
|
2148 | - |
|
2149 | - return "array($type1$type2)"; |
|
2150 | - } else { |
|
2151 | - return 'array'; |
|
2152 | - }//end if |
|
2153 | - } else if (in_array($lowerVarType, self::$allowedTypes) === true) { |
|
2154 | - // A valid type, but not lower cased. |
|
2155 | - return $lowerVarType; |
|
2156 | - } else { |
|
2157 | - // Must be a custom type name. |
|
2158 | - return $varType; |
|
2159 | - }//end if |
|
2160 | - }//end if |
|
2161 | - |
|
2162 | - }//end suggestType() |
|
2163 | - |
|
2164 | - |
|
2165 | - /** |
|
2166 | - * Prepares token content for output to screen. |
|
2167 | - * |
|
2168 | - * Replaces invisible characters so they are visible. On non-Windows |
|
2169 | - * OSes it will also colour the invisible characters. |
|
2170 | - * |
|
2171 | - * @param string $content The content to prepare. |
|
2172 | - * |
|
2173 | - * @return string |
|
2174 | - */ |
|
2175 | - public static function prepareForOutput($content) |
|
2176 | - { |
|
2177 | - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
2178 | - $content = str_replace("\r", '\r', $content); |
|
2179 | - $content = str_replace("\n", '\n', $content); |
|
2180 | - $content = str_replace("\t", '\t', $content); |
|
2181 | - } else { |
|
2182 | - $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content); |
|
2183 | - $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content); |
|
2184 | - $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content); |
|
2185 | - $content = str_replace(' ', "\033[30;1m·\033[0m", $content); |
|
2186 | - } |
|
2187 | - |
|
2188 | - return $content; |
|
2189 | - |
|
2190 | - }//end prepareForOutput() |
|
2191 | - |
|
2192 | - |
|
2193 | - /** |
|
2194 | - * Get a list paths where standards are installed. |
|
2195 | - * |
|
2196 | - * @return array |
|
2197 | - */ |
|
2198 | - public static function getInstalledStandardPaths() |
|
2199 | - { |
|
2200 | - $installedPaths = array(dirname(__FILE__).DIRECTORY_SEPARATOR.'CodeSniffer'.DIRECTORY_SEPARATOR.'Standards'); |
|
2201 | - $configPaths = PHP_CodeSniffer::getConfigData('installed_paths'); |
|
2202 | - if ($configPaths !== null) { |
|
2203 | - $installedPaths = array_merge($installedPaths, explode(',', $configPaths)); |
|
2204 | - } |
|
2205 | - |
|
2206 | - $resolvedInstalledPaths = array(); |
|
2207 | - foreach ($installedPaths as $installedPath) { |
|
2208 | - if (substr($installedPath, 0, 1) === '.') { |
|
2209 | - $installedPath = dirname(__FILE__).DIRECTORY_SEPARATOR.$installedPath; |
|
2210 | - } |
|
2211 | - |
|
2212 | - $resolvedInstalledPaths[] = $installedPath; |
|
2213 | - } |
|
2214 | - |
|
2215 | - return $resolvedInstalledPaths; |
|
2216 | - |
|
2217 | - }//end getInstalledStandardPaths() |
|
2218 | - |
|
2219 | - |
|
2220 | - /** |
|
2221 | - * Get a list of all coding standards installed. |
|
2222 | - * |
|
2223 | - * Coding standards are directories located in the |
|
2224 | - * CodeSniffer/Standards directory. Valid coding standards |
|
2225 | - * include a Sniffs subdirectory. |
|
2226 | - * |
|
2227 | - * @param boolean $includeGeneric If true, the special "Generic" |
|
2228 | - * coding standard will be included |
|
2229 | - * if installed. |
|
2230 | - * @param string $standardsDir A specific directory to look for standards |
|
2231 | - * in. If not specified, PHP_CodeSniffer will |
|
2232 | - * look in its default locations. |
|
2233 | - * |
|
2234 | - * @return array |
|
2235 | - * @see isInstalledStandard() |
|
2236 | - */ |
|
2237 | - public static function getInstalledStandards( |
|
2238 | - $includeGeneric=false, |
|
2239 | - $standardsDir='' |
|
2240 | - ) { |
|
2241 | - $installedStandards = array(); |
|
2242 | - |
|
2243 | - if ($standardsDir === '') { |
|
2244 | - $installedPaths = self::getInstalledStandardPaths(); |
|
2245 | - } else { |
|
2246 | - $installedPaths = array($standardsDir); |
|
2247 | - } |
|
2248 | - |
|
2249 | - foreach ($installedPaths as $standardsDir) { |
|
2250 | - $di = new DirectoryIterator($standardsDir); |
|
2251 | - foreach ($di as $file) { |
|
2252 | - if ($file->isDir() === true && $file->isDot() === false) { |
|
2253 | - $filename = $file->getFilename(); |
|
2254 | - |
|
2255 | - // Ignore the special "Generic" standard. |
|
2256 | - if ($includeGeneric === false && $filename === 'Generic') { |
|
2257 | - continue; |
|
2258 | - } |
|
2259 | - |
|
2260 | - // Valid coding standard dirs include a ruleset. |
|
2261 | - $csFile = $file->getPathname().'/ruleset.xml'; |
|
2262 | - if (is_file($csFile) === true) { |
|
2263 | - $installedStandards[] = $filename; |
|
2264 | - } |
|
2265 | - } |
|
2266 | - } |
|
2267 | - }//end foreach |
|
2268 | - |
|
2269 | - return $installedStandards; |
|
2270 | - |
|
2271 | - }//end getInstalledStandards() |
|
2272 | - |
|
2273 | - |
|
2274 | - /** |
|
2275 | - * Determine if a standard is installed. |
|
2276 | - * |
|
2277 | - * Coding standards are directories located in the |
|
2278 | - * CodeSniffer/Standards directory. Valid coding standards |
|
2279 | - * include a ruleset.xml file. |
|
2280 | - * |
|
2281 | - * @param string $standard The name of the coding standard. |
|
2282 | - * |
|
2283 | - * @return boolean |
|
2284 | - * @see getInstalledStandards() |
|
2285 | - */ |
|
2286 | - public static function isInstalledStandard($standard) |
|
2287 | - { |
|
2288 | - $path = self::getInstalledStandardPath($standard); |
|
2289 | - if ($path !== null && strpos($path, 'ruleset.xml') !== false) { |
|
2290 | - return true; |
|
2291 | - } else { |
|
2292 | - // This could be a custom standard, installed outside our |
|
2293 | - // standards directory. |
|
2294 | - $standard = self::realPath($standard); |
|
2295 | - |
|
2296 | - // Might be an actual ruleset file itself. |
|
2297 | - // If it has an XML extension, let's at least try it. |
|
2298 | - if (is_file($standard) === true |
|
2299 | - && (substr(strtolower($standard), -4) === '.xml' |
|
2300 | - || substr(strtolower($standard), -9) === '.xml.dist') |
|
2301 | - ) { |
|
2302 | - return true; |
|
2303 | - } |
|
2304 | - |
|
2305 | - // If it is a directory with a ruleset.xml file in it, |
|
2306 | - // it is a standard. |
|
2307 | - $ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
2308 | - if (is_file($ruleset) === true) { |
|
2309 | - return true; |
|
2310 | - } |
|
2311 | - }//end if |
|
2312 | - |
|
2313 | - return false; |
|
2314 | - |
|
2315 | - }//end isInstalledStandard() |
|
2316 | - |
|
2317 | - |
|
2318 | - /** |
|
2319 | - * Return the path of an installed coding standard. |
|
2320 | - * |
|
2321 | - * Coding standards are directories located in the |
|
2322 | - * CodeSniffer/Standards directory. Valid coding standards |
|
2323 | - * include a ruleset.xml file. |
|
2324 | - * |
|
2325 | - * @param string $standard The name of the coding standard. |
|
2326 | - * |
|
2327 | - * @return string|null |
|
2328 | - */ |
|
2329 | - public static function getInstalledStandardPath($standard) |
|
2330 | - { |
|
2331 | - $installedPaths = self::getInstalledStandardPaths(); |
|
2332 | - foreach ($installedPaths as $installedPath) { |
|
2333 | - $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard; |
|
2334 | - $path = self::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
2335 | - if (is_file($path) === true) { |
|
2336 | - return $path; |
|
2337 | - } else if (self::isPharFile($standardPath) === true) { |
|
2338 | - $path = self::realpath($standardPath); |
|
2339 | - if ($path !== false) { |
|
2340 | - return $path; |
|
2341 | - } |
|
2342 | - } |
|
2343 | - } |
|
2344 | - |
|
2345 | - return null; |
|
2346 | - |
|
2347 | - }//end getInstalledStandardPath() |
|
2348 | - |
|
2349 | - |
|
2350 | - /** |
|
2351 | - * Get a single config value. |
|
2352 | - * |
|
2353 | - * Config data is stored in the data dir, in a file called |
|
2354 | - * CodeSniffer.conf. It is a simple PHP array. |
|
2355 | - * |
|
2356 | - * @param string $key The name of the config value. |
|
2357 | - * |
|
2358 | - * @return string|null |
|
2359 | - * @see setConfigData() |
|
2360 | - * @see getAllConfigData() |
|
2361 | - */ |
|
2362 | - public static function getConfigData($key) |
|
2363 | - { |
|
2364 | - $phpCodeSnifferConfig = self::getAllConfigData(); |
|
2365 | - |
|
2366 | - if ($phpCodeSnifferConfig === null) { |
|
2367 | - return null; |
|
2368 | - } |
|
2369 | - |
|
2370 | - if (isset($phpCodeSnifferConfig[$key]) === false) { |
|
2371 | - return null; |
|
2372 | - } |
|
2373 | - |
|
2374 | - return $phpCodeSnifferConfig[$key]; |
|
2375 | - |
|
2376 | - }//end getConfigData() |
|
2377 | - |
|
2378 | - |
|
2379 | - /** |
|
2380 | - * Set a single config value. |
|
2381 | - * |
|
2382 | - * Config data is stored in the data dir, in a file called |
|
2383 | - * CodeSniffer.conf. It is a simple PHP array. |
|
2384 | - * |
|
2385 | - * @param string $key The name of the config value. |
|
2386 | - * @param string|null $value The value to set. If null, the config |
|
2387 | - * entry is deleted, reverting it to the |
|
2388 | - * default value. |
|
2389 | - * @param boolean $temp Set this config data temporarily for this |
|
2390 | - * script run. This will not write the config |
|
2391 | - * data to the config file. |
|
2392 | - * |
|
2393 | - * @return boolean |
|
2394 | - * @see getConfigData() |
|
2395 | - * @throws PHP_CodeSniffer_Exception If the config file can not be written. |
|
2396 | - */ |
|
2397 | - public static function setConfigData($key, $value, $temp=false) |
|
2398 | - { |
|
2399 | - if ($temp === false) { |
|
2400 | - $path = ''; |
|
2401 | - if (is_callable('Phar::running') === true) { |
|
2402 | - $path = Phar::running(false); |
|
2403 | - } |
|
2404 | - |
|
2405 | - if ($path !== '') { |
|
2406 | - $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2407 | - } else { |
|
2408 | - $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2409 | - if (is_file($configFile) === false |
|
2410 | - && strpos('@data_dir@', '@data_dir') === false |
|
2411 | - ) { |
|
2412 | - // If data_dir was replaced, this is a PEAR install and we can |
|
2413 | - // use the PEAR data dir to store the conf file. |
|
2414 | - $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf'; |
|
2415 | - } |
|
2416 | - } |
|
2417 | - |
|
2418 | - if (is_file($configFile) === true |
|
2419 | - && is_writable($configFile) === false |
|
2420 | - ) { |
|
2421 | - $error = 'Config file '.$configFile.' is not writable'; |
|
2422 | - throw new PHP_CodeSniffer_Exception($error); |
|
2423 | - } |
|
2424 | - }//end if |
|
2425 | - |
|
2426 | - $phpCodeSnifferConfig = self::getAllConfigData(); |
|
2427 | - |
|
2428 | - if ($value === null) { |
|
2429 | - if (isset($phpCodeSnifferConfig[$key]) === true) { |
|
2430 | - unset($phpCodeSnifferConfig[$key]); |
|
2431 | - } |
|
2432 | - } else { |
|
2433 | - $phpCodeSnifferConfig[$key] = $value; |
|
2434 | - } |
|
2435 | - |
|
2436 | - if ($temp === false) { |
|
2437 | - $output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
|
2438 | - $output .= var_export($phpCodeSnifferConfig, true); |
|
2439 | - $output .= "\n?".'>'; |
|
2440 | - |
|
2441 | - if (file_put_contents($configFile, $output) === false) { |
|
2442 | - return false; |
|
2443 | - } |
|
2444 | - } |
|
2445 | - |
|
2446 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2447 | - |
|
2448 | - return true; |
|
2449 | - |
|
2450 | - }//end setConfigData() |
|
2451 | - |
|
2452 | - |
|
2453 | - /** |
|
2454 | - * Get all config data in an array. |
|
2455 | - * |
|
2456 | - * @return array<string, string> |
|
2457 | - * @see getConfigData() |
|
2458 | - */ |
|
2459 | - public static function getAllConfigData() |
|
2460 | - { |
|
2461 | - if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) { |
|
2462 | - return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2463 | - } |
|
2464 | - |
|
2465 | - $path = ''; |
|
2466 | - if (is_callable('Phar::running') === true) { |
|
2467 | - $path = Phar::running(false); |
|
2468 | - } |
|
2469 | - |
|
2470 | - if ($path !== '') { |
|
2471 | - $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2472 | - } else { |
|
2473 | - $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2474 | - if (is_file($configFile) === false) { |
|
2475 | - $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf'; |
|
2476 | - } |
|
2477 | - } |
|
2478 | - |
|
2479 | - if (is_file($configFile) === false) { |
|
2480 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = array(); |
|
2481 | - return array(); |
|
2482 | - } |
|
2483 | - |
|
2484 | - include $configFile; |
|
2485 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2486 | - return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2487 | - |
|
2488 | - }//end getAllConfigData() |
|
2489 | - |
|
2490 | - |
|
2491 | - /** |
|
2492 | - * Return TRUE, if the path is a phar file. |
|
2493 | - * |
|
2494 | - * @param string $path The path to use. |
|
2495 | - * |
|
2496 | - * @return mixed |
|
2497 | - */ |
|
2498 | - public static function isPharFile($path) |
|
2499 | - { |
|
2500 | - if (strpos($path, 'phar://') === 0) { |
|
2501 | - return true; |
|
2502 | - } |
|
2503 | - |
|
2504 | - return false; |
|
2505 | - |
|
2506 | - }//end isPharFile() |
|
2507 | - |
|
2508 | - |
|
2509 | - /** |
|
2510 | - * CodeSniffer alternative for realpath. |
|
2511 | - * |
|
2512 | - * Allows for phar support. |
|
2513 | - * |
|
2514 | - * @param string $path The path to use. |
|
2515 | - * |
|
2516 | - * @return mixed |
|
2517 | - */ |
|
2518 | - public static function realpath($path) |
|
2519 | - { |
|
2520 | - // Support the path replacement of ~ with the user's home directory. |
|
2521 | - if (substr($path, 0, 2) === '~/') { |
|
2522 | - $homeDir = getenv('HOME'); |
|
2523 | - if ($homeDir !== false) { |
|
2524 | - $path = $homeDir.substr($path, 1); |
|
2525 | - } |
|
2526 | - } |
|
2527 | - |
|
2528 | - // No extra work needed if this is not a phar file. |
|
2529 | - if (self::isPharFile($path) === false) { |
|
2530 | - return realpath($path); |
|
2531 | - } |
|
2532 | - |
|
2533 | - // Before trying to break down the file path, |
|
2534 | - // check if it exists first because it will mostly not |
|
2535 | - // change after running the below code. |
|
2536 | - if (file_exists($path) === true) { |
|
2537 | - return $path; |
|
2538 | - } |
|
2539 | - |
|
2540 | - $phar = Phar::running(false); |
|
2541 | - $extra = str_replace('phar://'.$phar, '', $path); |
|
2542 | - $path = realpath($phar); |
|
2543 | - if ($path === false) { |
|
2544 | - return false; |
|
2545 | - } |
|
2546 | - |
|
2547 | - $path = 'phar://'.$path.$extra; |
|
2548 | - if (file_exists($path) === true) { |
|
2549 | - return $path; |
|
2550 | - } |
|
2551 | - |
|
2552 | - return false; |
|
2553 | - |
|
2554 | - }//end realpath() |
|
2555 | - |
|
2556 | - |
|
2557 | - /** |
|
2558 | - * CodeSniffer alternative for chdir(). |
|
2559 | - * |
|
2560 | - * Allows for phar support. |
|
2561 | - * |
|
2562 | - * @param string $path The path to use. |
|
2563 | - * |
|
2564 | - * @return void |
|
2565 | - */ |
|
2566 | - public static function chdir($path) |
|
2567 | - { |
|
2568 | - if (self::isPharFile($path) === true) { |
|
2569 | - $phar = Phar::running(false); |
|
2570 | - chdir(dirname($phar)); |
|
2571 | - } else { |
|
2572 | - chdir($path); |
|
2573 | - } |
|
2574 | - |
|
2575 | - }//end chdir() |
|
1821 | + // Get current violations and then clear the list to make sure |
|
1822 | + // we only print violations for a single file each time. |
|
1823 | + $numErrors = null; |
|
1824 | + while ($numErrors !== 0) { |
|
1825 | + $numErrors = ($phpcsFile->getErrorCount() + $phpcsFile->getWarningCount()); |
|
1826 | + if ($numErrors === 0) { |
|
1827 | + continue; |
|
1828 | + } |
|
1829 | + |
|
1830 | + $reportClass = $this->reporting->factory('full'); |
|
1831 | + $reportData = $this->reporting->prepareFileReport($phpcsFile); |
|
1832 | + $reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']); |
|
1833 | + |
|
1834 | + echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
|
1835 | + $input = fgets(STDIN); |
|
1836 | + $input = trim($input); |
|
1837 | + |
|
1838 | + switch ($input) { |
|
1839 | + case 's': |
|
1840 | + break(2); |
|
1841 | + case 'q': |
|
1842 | + exit(0); |
|
1843 | + break; |
|
1844 | + default: |
|
1845 | + // Repopulate the sniffs because some of them save their state |
|
1846 | + // and only clear it when the file changes, but we are rechecking |
|
1847 | + // the same file. |
|
1848 | + $this->populateTokenListeners(); |
|
1849 | + $phpcsFile = $this->_processFile($file, $contents); |
|
1850 | + break; |
|
1851 | + } |
|
1852 | + }//end while |
|
1853 | + |
|
1854 | + return $phpcsFile; |
|
1855 | + |
|
1856 | + }//end processFile() |
|
1857 | + |
|
1858 | + |
|
1859 | + /** |
|
1860 | + * Process the sniffs for a single file. |
|
1861 | + * |
|
1862 | + * Does raw processing only. No interactive support or error checking. |
|
1863 | + * |
|
1864 | + * @param string $file The file to process. |
|
1865 | + * @param string $contents The contents to parse. If NULL, the content |
|
1866 | + * is taken from the file system. |
|
1867 | + * |
|
1868 | + * @return PHP_CodeSniffer_File |
|
1869 | + * @see processFile() |
|
1870 | + */ |
|
1871 | + private function _processFile($file, $contents) |
|
1872 | + { |
|
1873 | + $stdin = false; |
|
1874 | + $cliValues = $this->cli->getCommandLineValues(); |
|
1875 | + if (empty($cliValues['files']) === true) { |
|
1876 | + $stdin = true; |
|
1877 | + } |
|
1878 | + |
|
1879 | + if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1880 | + $startTime = microtime(true); |
|
1881 | + echo 'Processing '.basename($file).' '; |
|
1882 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1883 | + echo PHP_EOL; |
|
1884 | + } |
|
1885 | + } |
|
1886 | + |
|
1887 | + $phpcsFile = new PHP_CodeSniffer_File( |
|
1888 | + $file, |
|
1889 | + $this->_tokenListeners, |
|
1890 | + $this->ruleset, |
|
1891 | + $this |
|
1892 | + ); |
|
1893 | + |
|
1894 | + $phpcsFile->start($contents); |
|
1895 | + |
|
1896 | + if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1897 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
1898 | + if ($timeTaken < 1000) { |
|
1899 | + $timeTaken = round($timeTaken); |
|
1900 | + echo "DONE in {$timeTaken}ms"; |
|
1901 | + } else { |
|
1902 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
1903 | + echo "DONE in $timeTaken secs"; |
|
1904 | + } |
|
1905 | + |
|
1906 | + if (PHP_CODESNIFFER_CBF === true) { |
|
1907 | + $errors = $phpcsFile->getFixableCount(); |
|
1908 | + echo " ($errors fixable violations)".PHP_EOL; |
|
1909 | + } else { |
|
1910 | + $errors = $phpcsFile->getErrorCount(); |
|
1911 | + $warnings = $phpcsFile->getWarningCount(); |
|
1912 | + echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
1913 | + } |
|
1914 | + } |
|
1915 | + |
|
1916 | + return $phpcsFile; |
|
1917 | + |
|
1918 | + }//end _processFile() |
|
1919 | + |
|
1920 | + |
|
1921 | + /** |
|
1922 | + * Generates documentation for a coding standard. |
|
1923 | + * |
|
1924 | + * @param string $standard The standard to generate docs for |
|
1925 | + * @param array $sniffs A list of sniffs to limit the docs to. |
|
1926 | + * @param string $generator The name of the generator class to use. |
|
1927 | + * |
|
1928 | + * @return void |
|
1929 | + */ |
|
1930 | + public function generateDocs($standard, array $sniffs=array(), $generator='Text') |
|
1931 | + { |
|
1932 | + if (class_exists('PHP_CodeSniffer_DocGenerators_'.$generator, true) === false) { |
|
1933 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_'.$generator.' not found'); |
|
1934 | + } |
|
1935 | + |
|
1936 | + $class = "PHP_CodeSniffer_DocGenerators_$generator"; |
|
1937 | + $generator = new $class($standard, $sniffs); |
|
1938 | + |
|
1939 | + $generator->generate(); |
|
1940 | + |
|
1941 | + }//end generateDocs() |
|
1942 | + |
|
1943 | + |
|
1944 | + /** |
|
1945 | + * Gets the array of PHP_CodeSniffer_Sniff's. |
|
1946 | + * |
|
1947 | + * @return PHP_CodeSniffer_Sniff[] |
|
1948 | + */ |
|
1949 | + public function getSniffs() |
|
1950 | + { |
|
1951 | + return $this->listeners; |
|
1952 | + |
|
1953 | + }//end getSniffs() |
|
1954 | + |
|
1955 | + |
|
1956 | + /** |
|
1957 | + * Gets the array of PHP_CodeSniffer_Sniff's indexed by token type. |
|
1958 | + * |
|
1959 | + * @return array |
|
1960 | + */ |
|
1961 | + public function getTokenSniffs() |
|
1962 | + { |
|
1963 | + return $this->_tokenListeners; |
|
1964 | + |
|
1965 | + }//end getTokenSniffs() |
|
1966 | + |
|
1967 | + |
|
1968 | + /** |
|
1969 | + * Returns true if the specified string is in the camel caps format. |
|
1970 | + * |
|
1971 | + * @param string $string The string the verify. |
|
1972 | + * @param boolean $classFormat If true, check to see if the string is in the |
|
1973 | + * class format. Class format strings must start |
|
1974 | + * with a capital letter and contain no |
|
1975 | + * underscores. |
|
1976 | + * @param boolean $public If true, the first character in the string |
|
1977 | + * must be an a-z character. If false, the |
|
1978 | + * character must be an underscore. This |
|
1979 | + * argument is only applicable if $classFormat |
|
1980 | + * is false. |
|
1981 | + * @param boolean $strict If true, the string must not have two capital |
|
1982 | + * letters next to each other. If false, a |
|
1983 | + * relaxed camel caps policy is used to allow |
|
1984 | + * for acronyms. |
|
1985 | + * |
|
1986 | + * @return boolean |
|
1987 | + */ |
|
1988 | + public static function isCamelCaps( |
|
1989 | + $string, |
|
1990 | + $classFormat=false, |
|
1991 | + $public=true, |
|
1992 | + $strict=true |
|
1993 | + ) { |
|
1994 | + // Check the first character first. |
|
1995 | + if ($classFormat === false) { |
|
1996 | + $legalFirstChar = ''; |
|
1997 | + if ($public === false) { |
|
1998 | + $legalFirstChar = '[_]'; |
|
1999 | + } |
|
2000 | + |
|
2001 | + if ($strict === false) { |
|
2002 | + // Can either start with a lowercase letter, or multiple uppercase |
|
2003 | + // in a row, representing an acronym. |
|
2004 | + $legalFirstChar .= '([A-Z]{2,}|[a-z])'; |
|
2005 | + } else { |
|
2006 | + $legalFirstChar .= '[a-z]'; |
|
2007 | + } |
|
2008 | + } else { |
|
2009 | + $legalFirstChar = '[A-Z]'; |
|
2010 | + } |
|
2011 | + |
|
2012 | + if (preg_match("/^$legalFirstChar/", $string) === 0) { |
|
2013 | + return false; |
|
2014 | + } |
|
2015 | + |
|
2016 | + // Check that the name only contains legal characters. |
|
2017 | + $legalChars = 'a-zA-Z0-9'; |
|
2018 | + if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) { |
|
2019 | + return false; |
|
2020 | + } |
|
2021 | + |
|
2022 | + if ($strict === true) { |
|
2023 | + // Check that there are not two capital letters next to each other. |
|
2024 | + $length = strlen($string); |
|
2025 | + $lastCharWasCaps = $classFormat; |
|
2026 | + |
|
2027 | + for ($i = 1; $i < $length; $i++) { |
|
2028 | + $ascii = ord($string{$i}); |
|
2029 | + if ($ascii >= 48 && $ascii <= 57) { |
|
2030 | + // The character is a number, so it cant be a capital. |
|
2031 | + $isCaps = false; |
|
2032 | + } else { |
|
2033 | + if (strtoupper($string{$i}) === $string{$i}) { |
|
2034 | + $isCaps = true; |
|
2035 | + } else { |
|
2036 | + $isCaps = false; |
|
2037 | + } |
|
2038 | + } |
|
2039 | + |
|
2040 | + if ($isCaps === true && $lastCharWasCaps === true) { |
|
2041 | + return false; |
|
2042 | + } |
|
2043 | + |
|
2044 | + $lastCharWasCaps = $isCaps; |
|
2045 | + } |
|
2046 | + }//end if |
|
2047 | + |
|
2048 | + return true; |
|
2049 | + |
|
2050 | + }//end isCamelCaps() |
|
2051 | + |
|
2052 | + |
|
2053 | + /** |
|
2054 | + * Returns true if the specified string is in the underscore caps format. |
|
2055 | + * |
|
2056 | + * @param string $string The string to verify. |
|
2057 | + * |
|
2058 | + * @return boolean |
|
2059 | + */ |
|
2060 | + public static function isUnderscoreName($string) |
|
2061 | + { |
|
2062 | + // If there are space in the name, it can't be valid. |
|
2063 | + if (strpos($string, ' ') !== false) { |
|
2064 | + return false; |
|
2065 | + } |
|
2066 | + |
|
2067 | + $validName = true; |
|
2068 | + $nameBits = explode('_', $string); |
|
2069 | + |
|
2070 | + if (preg_match('|^[A-Z]|', $string) === 0) { |
|
2071 | + // Name does not begin with a capital letter. |
|
2072 | + $validName = false; |
|
2073 | + } else { |
|
2074 | + foreach ($nameBits as $bit) { |
|
2075 | + if ($bit === '') { |
|
2076 | + continue; |
|
2077 | + } |
|
2078 | + |
|
2079 | + if ($bit{0} !== strtoupper($bit{0})) { |
|
2080 | + $validName = false; |
|
2081 | + break; |
|
2082 | + } |
|
2083 | + } |
|
2084 | + } |
|
2085 | + |
|
2086 | + return $validName; |
|
2087 | + |
|
2088 | + }//end isUnderscoreName() |
|
2089 | + |
|
2090 | + |
|
2091 | + /** |
|
2092 | + * Returns a valid variable type for param/var tag. |
|
2093 | + * |
|
2094 | + * If type is not one of the standard type, it must be a custom type. |
|
2095 | + * Returns the correct type name suggestion if type name is invalid. |
|
2096 | + * |
|
2097 | + * @param string $varType The variable type to process. |
|
2098 | + * |
|
2099 | + * @return string |
|
2100 | + */ |
|
2101 | + public static function suggestType($varType) |
|
2102 | + { |
|
2103 | + if ($varType === '') { |
|
2104 | + return ''; |
|
2105 | + } |
|
2106 | + |
|
2107 | + if (in_array($varType, self::$allowedTypes) === true) { |
|
2108 | + return $varType; |
|
2109 | + } else { |
|
2110 | + $lowerVarType = strtolower($varType); |
|
2111 | + switch ($lowerVarType) { |
|
2112 | + case 'bool': |
|
2113 | + case 'boolean': |
|
2114 | + return 'boolean'; |
|
2115 | + case 'double': |
|
2116 | + case 'real': |
|
2117 | + case 'float': |
|
2118 | + return 'float'; |
|
2119 | + case 'int': |
|
2120 | + case 'integer': |
|
2121 | + return 'integer'; |
|
2122 | + case 'array()': |
|
2123 | + case 'array': |
|
2124 | + return 'array'; |
|
2125 | + }//end switch |
|
2126 | + |
|
2127 | + if (strpos($lowerVarType, 'array(') !== false) { |
|
2128 | + // Valid array declaration: |
|
2129 | + // array, array(type), array(type1 => type2). |
|
2130 | + $matches = array(); |
|
2131 | + $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; |
|
2132 | + if (preg_match($pattern, $varType, $matches) !== 0) { |
|
2133 | + $type1 = ''; |
|
2134 | + if (isset($matches[1]) === true) { |
|
2135 | + $type1 = $matches[1]; |
|
2136 | + } |
|
2137 | + |
|
2138 | + $type2 = ''; |
|
2139 | + if (isset($matches[3]) === true) { |
|
2140 | + $type2 = $matches[3]; |
|
2141 | + } |
|
2142 | + |
|
2143 | + $type1 = self::suggestType($type1); |
|
2144 | + $type2 = self::suggestType($type2); |
|
2145 | + if ($type2 !== '') { |
|
2146 | + $type2 = ' => '.$type2; |
|
2147 | + } |
|
2148 | + |
|
2149 | + return "array($type1$type2)"; |
|
2150 | + } else { |
|
2151 | + return 'array'; |
|
2152 | + }//end if |
|
2153 | + } else if (in_array($lowerVarType, self::$allowedTypes) === true) { |
|
2154 | + // A valid type, but not lower cased. |
|
2155 | + return $lowerVarType; |
|
2156 | + } else { |
|
2157 | + // Must be a custom type name. |
|
2158 | + return $varType; |
|
2159 | + }//end if |
|
2160 | + }//end if |
|
2161 | + |
|
2162 | + }//end suggestType() |
|
2163 | + |
|
2164 | + |
|
2165 | + /** |
|
2166 | + * Prepares token content for output to screen. |
|
2167 | + * |
|
2168 | + * Replaces invisible characters so they are visible. On non-Windows |
|
2169 | + * OSes it will also colour the invisible characters. |
|
2170 | + * |
|
2171 | + * @param string $content The content to prepare. |
|
2172 | + * |
|
2173 | + * @return string |
|
2174 | + */ |
|
2175 | + public static function prepareForOutput($content) |
|
2176 | + { |
|
2177 | + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
2178 | + $content = str_replace("\r", '\r', $content); |
|
2179 | + $content = str_replace("\n", '\n', $content); |
|
2180 | + $content = str_replace("\t", '\t', $content); |
|
2181 | + } else { |
|
2182 | + $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content); |
|
2183 | + $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content); |
|
2184 | + $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content); |
|
2185 | + $content = str_replace(' ', "\033[30;1m·\033[0m", $content); |
|
2186 | + } |
|
2187 | + |
|
2188 | + return $content; |
|
2189 | + |
|
2190 | + }//end prepareForOutput() |
|
2191 | + |
|
2192 | + |
|
2193 | + /** |
|
2194 | + * Get a list paths where standards are installed. |
|
2195 | + * |
|
2196 | + * @return array |
|
2197 | + */ |
|
2198 | + public static function getInstalledStandardPaths() |
|
2199 | + { |
|
2200 | + $installedPaths = array(dirname(__FILE__).DIRECTORY_SEPARATOR.'CodeSniffer'.DIRECTORY_SEPARATOR.'Standards'); |
|
2201 | + $configPaths = PHP_CodeSniffer::getConfigData('installed_paths'); |
|
2202 | + if ($configPaths !== null) { |
|
2203 | + $installedPaths = array_merge($installedPaths, explode(',', $configPaths)); |
|
2204 | + } |
|
2205 | + |
|
2206 | + $resolvedInstalledPaths = array(); |
|
2207 | + foreach ($installedPaths as $installedPath) { |
|
2208 | + if (substr($installedPath, 0, 1) === '.') { |
|
2209 | + $installedPath = dirname(__FILE__).DIRECTORY_SEPARATOR.$installedPath; |
|
2210 | + } |
|
2211 | + |
|
2212 | + $resolvedInstalledPaths[] = $installedPath; |
|
2213 | + } |
|
2214 | + |
|
2215 | + return $resolvedInstalledPaths; |
|
2216 | + |
|
2217 | + }//end getInstalledStandardPaths() |
|
2218 | + |
|
2219 | + |
|
2220 | + /** |
|
2221 | + * Get a list of all coding standards installed. |
|
2222 | + * |
|
2223 | + * Coding standards are directories located in the |
|
2224 | + * CodeSniffer/Standards directory. Valid coding standards |
|
2225 | + * include a Sniffs subdirectory. |
|
2226 | + * |
|
2227 | + * @param boolean $includeGeneric If true, the special "Generic" |
|
2228 | + * coding standard will be included |
|
2229 | + * if installed. |
|
2230 | + * @param string $standardsDir A specific directory to look for standards |
|
2231 | + * in. If not specified, PHP_CodeSniffer will |
|
2232 | + * look in its default locations. |
|
2233 | + * |
|
2234 | + * @return array |
|
2235 | + * @see isInstalledStandard() |
|
2236 | + */ |
|
2237 | + public static function getInstalledStandards( |
|
2238 | + $includeGeneric=false, |
|
2239 | + $standardsDir='' |
|
2240 | + ) { |
|
2241 | + $installedStandards = array(); |
|
2242 | + |
|
2243 | + if ($standardsDir === '') { |
|
2244 | + $installedPaths = self::getInstalledStandardPaths(); |
|
2245 | + } else { |
|
2246 | + $installedPaths = array($standardsDir); |
|
2247 | + } |
|
2248 | + |
|
2249 | + foreach ($installedPaths as $standardsDir) { |
|
2250 | + $di = new DirectoryIterator($standardsDir); |
|
2251 | + foreach ($di as $file) { |
|
2252 | + if ($file->isDir() === true && $file->isDot() === false) { |
|
2253 | + $filename = $file->getFilename(); |
|
2254 | + |
|
2255 | + // Ignore the special "Generic" standard. |
|
2256 | + if ($includeGeneric === false && $filename === 'Generic') { |
|
2257 | + continue; |
|
2258 | + } |
|
2259 | + |
|
2260 | + // Valid coding standard dirs include a ruleset. |
|
2261 | + $csFile = $file->getPathname().'/ruleset.xml'; |
|
2262 | + if (is_file($csFile) === true) { |
|
2263 | + $installedStandards[] = $filename; |
|
2264 | + } |
|
2265 | + } |
|
2266 | + } |
|
2267 | + }//end foreach |
|
2268 | + |
|
2269 | + return $installedStandards; |
|
2270 | + |
|
2271 | + }//end getInstalledStandards() |
|
2272 | + |
|
2273 | + |
|
2274 | + /** |
|
2275 | + * Determine if a standard is installed. |
|
2276 | + * |
|
2277 | + * Coding standards are directories located in the |
|
2278 | + * CodeSniffer/Standards directory. Valid coding standards |
|
2279 | + * include a ruleset.xml file. |
|
2280 | + * |
|
2281 | + * @param string $standard The name of the coding standard. |
|
2282 | + * |
|
2283 | + * @return boolean |
|
2284 | + * @see getInstalledStandards() |
|
2285 | + */ |
|
2286 | + public static function isInstalledStandard($standard) |
|
2287 | + { |
|
2288 | + $path = self::getInstalledStandardPath($standard); |
|
2289 | + if ($path !== null && strpos($path, 'ruleset.xml') !== false) { |
|
2290 | + return true; |
|
2291 | + } else { |
|
2292 | + // This could be a custom standard, installed outside our |
|
2293 | + // standards directory. |
|
2294 | + $standard = self::realPath($standard); |
|
2295 | + |
|
2296 | + // Might be an actual ruleset file itself. |
|
2297 | + // If it has an XML extension, let's at least try it. |
|
2298 | + if (is_file($standard) === true |
|
2299 | + && (substr(strtolower($standard), -4) === '.xml' |
|
2300 | + || substr(strtolower($standard), -9) === '.xml.dist') |
|
2301 | + ) { |
|
2302 | + return true; |
|
2303 | + } |
|
2304 | + |
|
2305 | + // If it is a directory with a ruleset.xml file in it, |
|
2306 | + // it is a standard. |
|
2307 | + $ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
2308 | + if (is_file($ruleset) === true) { |
|
2309 | + return true; |
|
2310 | + } |
|
2311 | + }//end if |
|
2312 | + |
|
2313 | + return false; |
|
2314 | + |
|
2315 | + }//end isInstalledStandard() |
|
2316 | + |
|
2317 | + |
|
2318 | + /** |
|
2319 | + * Return the path of an installed coding standard. |
|
2320 | + * |
|
2321 | + * Coding standards are directories located in the |
|
2322 | + * CodeSniffer/Standards directory. Valid coding standards |
|
2323 | + * include a ruleset.xml file. |
|
2324 | + * |
|
2325 | + * @param string $standard The name of the coding standard. |
|
2326 | + * |
|
2327 | + * @return string|null |
|
2328 | + */ |
|
2329 | + public static function getInstalledStandardPath($standard) |
|
2330 | + { |
|
2331 | + $installedPaths = self::getInstalledStandardPaths(); |
|
2332 | + foreach ($installedPaths as $installedPath) { |
|
2333 | + $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard; |
|
2334 | + $path = self::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
2335 | + if (is_file($path) === true) { |
|
2336 | + return $path; |
|
2337 | + } else if (self::isPharFile($standardPath) === true) { |
|
2338 | + $path = self::realpath($standardPath); |
|
2339 | + if ($path !== false) { |
|
2340 | + return $path; |
|
2341 | + } |
|
2342 | + } |
|
2343 | + } |
|
2344 | + |
|
2345 | + return null; |
|
2346 | + |
|
2347 | + }//end getInstalledStandardPath() |
|
2348 | + |
|
2349 | + |
|
2350 | + /** |
|
2351 | + * Get a single config value. |
|
2352 | + * |
|
2353 | + * Config data is stored in the data dir, in a file called |
|
2354 | + * CodeSniffer.conf. It is a simple PHP array. |
|
2355 | + * |
|
2356 | + * @param string $key The name of the config value. |
|
2357 | + * |
|
2358 | + * @return string|null |
|
2359 | + * @see setConfigData() |
|
2360 | + * @see getAllConfigData() |
|
2361 | + */ |
|
2362 | + public static function getConfigData($key) |
|
2363 | + { |
|
2364 | + $phpCodeSnifferConfig = self::getAllConfigData(); |
|
2365 | + |
|
2366 | + if ($phpCodeSnifferConfig === null) { |
|
2367 | + return null; |
|
2368 | + } |
|
2369 | + |
|
2370 | + if (isset($phpCodeSnifferConfig[$key]) === false) { |
|
2371 | + return null; |
|
2372 | + } |
|
2373 | + |
|
2374 | + return $phpCodeSnifferConfig[$key]; |
|
2375 | + |
|
2376 | + }//end getConfigData() |
|
2377 | + |
|
2378 | + |
|
2379 | + /** |
|
2380 | + * Set a single config value. |
|
2381 | + * |
|
2382 | + * Config data is stored in the data dir, in a file called |
|
2383 | + * CodeSniffer.conf. It is a simple PHP array. |
|
2384 | + * |
|
2385 | + * @param string $key The name of the config value. |
|
2386 | + * @param string|null $value The value to set. If null, the config |
|
2387 | + * entry is deleted, reverting it to the |
|
2388 | + * default value. |
|
2389 | + * @param boolean $temp Set this config data temporarily for this |
|
2390 | + * script run. This will not write the config |
|
2391 | + * data to the config file. |
|
2392 | + * |
|
2393 | + * @return boolean |
|
2394 | + * @see getConfigData() |
|
2395 | + * @throws PHP_CodeSniffer_Exception If the config file can not be written. |
|
2396 | + */ |
|
2397 | + public static function setConfigData($key, $value, $temp=false) |
|
2398 | + { |
|
2399 | + if ($temp === false) { |
|
2400 | + $path = ''; |
|
2401 | + if (is_callable('Phar::running') === true) { |
|
2402 | + $path = Phar::running(false); |
|
2403 | + } |
|
2404 | + |
|
2405 | + if ($path !== '') { |
|
2406 | + $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2407 | + } else { |
|
2408 | + $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2409 | + if (is_file($configFile) === false |
|
2410 | + && strpos('@data_dir@', '@data_dir') === false |
|
2411 | + ) { |
|
2412 | + // If data_dir was replaced, this is a PEAR install and we can |
|
2413 | + // use the PEAR data dir to store the conf file. |
|
2414 | + $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf'; |
|
2415 | + } |
|
2416 | + } |
|
2417 | + |
|
2418 | + if (is_file($configFile) === true |
|
2419 | + && is_writable($configFile) === false |
|
2420 | + ) { |
|
2421 | + $error = 'Config file '.$configFile.' is not writable'; |
|
2422 | + throw new PHP_CodeSniffer_Exception($error); |
|
2423 | + } |
|
2424 | + }//end if |
|
2425 | + |
|
2426 | + $phpCodeSnifferConfig = self::getAllConfigData(); |
|
2427 | + |
|
2428 | + if ($value === null) { |
|
2429 | + if (isset($phpCodeSnifferConfig[$key]) === true) { |
|
2430 | + unset($phpCodeSnifferConfig[$key]); |
|
2431 | + } |
|
2432 | + } else { |
|
2433 | + $phpCodeSnifferConfig[$key] = $value; |
|
2434 | + } |
|
2435 | + |
|
2436 | + if ($temp === false) { |
|
2437 | + $output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
|
2438 | + $output .= var_export($phpCodeSnifferConfig, true); |
|
2439 | + $output .= "\n?".'>'; |
|
2440 | + |
|
2441 | + if (file_put_contents($configFile, $output) === false) { |
|
2442 | + return false; |
|
2443 | + } |
|
2444 | + } |
|
2445 | + |
|
2446 | + $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2447 | + |
|
2448 | + return true; |
|
2449 | + |
|
2450 | + }//end setConfigData() |
|
2451 | + |
|
2452 | + |
|
2453 | + /** |
|
2454 | + * Get all config data in an array. |
|
2455 | + * |
|
2456 | + * @return array<string, string> |
|
2457 | + * @see getConfigData() |
|
2458 | + */ |
|
2459 | + public static function getAllConfigData() |
|
2460 | + { |
|
2461 | + if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) { |
|
2462 | + return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2463 | + } |
|
2464 | + |
|
2465 | + $path = ''; |
|
2466 | + if (is_callable('Phar::running') === true) { |
|
2467 | + $path = Phar::running(false); |
|
2468 | + } |
|
2469 | + |
|
2470 | + if ($path !== '') { |
|
2471 | + $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2472 | + } else { |
|
2473 | + $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2474 | + if (is_file($configFile) === false) { |
|
2475 | + $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf'; |
|
2476 | + } |
|
2477 | + } |
|
2478 | + |
|
2479 | + if (is_file($configFile) === false) { |
|
2480 | + $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = array(); |
|
2481 | + return array(); |
|
2482 | + } |
|
2483 | + |
|
2484 | + include $configFile; |
|
2485 | + $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2486 | + return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2487 | + |
|
2488 | + }//end getAllConfigData() |
|
2489 | + |
|
2490 | + |
|
2491 | + /** |
|
2492 | + * Return TRUE, if the path is a phar file. |
|
2493 | + * |
|
2494 | + * @param string $path The path to use. |
|
2495 | + * |
|
2496 | + * @return mixed |
|
2497 | + */ |
|
2498 | + public static function isPharFile($path) |
|
2499 | + { |
|
2500 | + if (strpos($path, 'phar://') === 0) { |
|
2501 | + return true; |
|
2502 | + } |
|
2503 | + |
|
2504 | + return false; |
|
2505 | + |
|
2506 | + }//end isPharFile() |
|
2507 | + |
|
2508 | + |
|
2509 | + /** |
|
2510 | + * CodeSniffer alternative for realpath. |
|
2511 | + * |
|
2512 | + * Allows for phar support. |
|
2513 | + * |
|
2514 | + * @param string $path The path to use. |
|
2515 | + * |
|
2516 | + * @return mixed |
|
2517 | + */ |
|
2518 | + public static function realpath($path) |
|
2519 | + { |
|
2520 | + // Support the path replacement of ~ with the user's home directory. |
|
2521 | + if (substr($path, 0, 2) === '~/') { |
|
2522 | + $homeDir = getenv('HOME'); |
|
2523 | + if ($homeDir !== false) { |
|
2524 | + $path = $homeDir.substr($path, 1); |
|
2525 | + } |
|
2526 | + } |
|
2527 | + |
|
2528 | + // No extra work needed if this is not a phar file. |
|
2529 | + if (self::isPharFile($path) === false) { |
|
2530 | + return realpath($path); |
|
2531 | + } |
|
2532 | + |
|
2533 | + // Before trying to break down the file path, |
|
2534 | + // check if it exists first because it will mostly not |
|
2535 | + // change after running the below code. |
|
2536 | + if (file_exists($path) === true) { |
|
2537 | + return $path; |
|
2538 | + } |
|
2539 | + |
|
2540 | + $phar = Phar::running(false); |
|
2541 | + $extra = str_replace('phar://'.$phar, '', $path); |
|
2542 | + $path = realpath($phar); |
|
2543 | + if ($path === false) { |
|
2544 | + return false; |
|
2545 | + } |
|
2546 | + |
|
2547 | + $path = 'phar://'.$path.$extra; |
|
2548 | + if (file_exists($path) === true) { |
|
2549 | + return $path; |
|
2550 | + } |
|
2551 | + |
|
2552 | + return false; |
|
2553 | + |
|
2554 | + }//end realpath() |
|
2555 | + |
|
2556 | + |
|
2557 | + /** |
|
2558 | + * CodeSniffer alternative for chdir(). |
|
2559 | + * |
|
2560 | + * Allows for phar support. |
|
2561 | + * |
|
2562 | + * @param string $path The path to use. |
|
2563 | + * |
|
2564 | + * @return void |
|
2565 | + */ |
|
2566 | + public static function chdir($path) |
|
2567 | + { |
|
2568 | + if (self::isPharFile($path) === true) { |
|
2569 | + $phar = Phar::running(false); |
|
2570 | + chdir(dirname($phar)); |
|
2571 | + } else { |
|
2572 | + chdir($path); |
|
2573 | + } |
|
2574 | + |
|
2575 | + }//end chdir() |
|
2576 | 2576 | |
2577 | 2577 | |
2578 | 2578 | }//end class |
@@ -1836,18 +1836,18 @@ discard block |
||
1836 | 1836 | $input = trim($input); |
1837 | 1837 | |
1838 | 1838 | switch ($input) { |
1839 | - case 's': |
|
1840 | - break(2); |
|
1841 | - case 'q': |
|
1842 | - exit(0); |
|
1843 | - break; |
|
1844 | - default: |
|
1845 | - // Repopulate the sniffs because some of them save their state |
|
1846 | - // and only clear it when the file changes, but we are rechecking |
|
1847 | - // the same file. |
|
1848 | - $this->populateTokenListeners(); |
|
1849 | - $phpcsFile = $this->_processFile($file, $contents); |
|
1850 | - break; |
|
1839 | + case 's': |
|
1840 | + break(2); |
|
1841 | + case 'q': |
|
1842 | + exit(0); |
|
1843 | + break; |
|
1844 | + default: |
|
1845 | + // Repopulate the sniffs because some of them save their state |
|
1846 | + // and only clear it when the file changes, but we are rechecking |
|
1847 | + // the same file. |
|
1848 | + $this->populateTokenListeners(); |
|
1849 | + $phpcsFile = $this->_processFile($file, $contents); |
|
1850 | + break; |
|
1851 | 1851 | } |
1852 | 1852 | }//end while |
1853 | 1853 | |
@@ -2109,19 +2109,19 @@ discard block |
||
2109 | 2109 | } else { |
2110 | 2110 | $lowerVarType = strtolower($varType); |
2111 | 2111 | switch ($lowerVarType) { |
2112 | - case 'bool': |
|
2113 | - case 'boolean': |
|
2114 | - return 'boolean'; |
|
2115 | - case 'double': |
|
2116 | - case 'real': |
|
2117 | - case 'float': |
|
2118 | - return 'float'; |
|
2119 | - case 'int': |
|
2120 | - case 'integer': |
|
2121 | - return 'integer'; |
|
2122 | - case 'array()': |
|
2123 | - case 'array': |
|
2124 | - return 'array'; |
|
2112 | + case 'bool': |
|
2113 | + case 'boolean': |
|
2114 | + return 'boolean'; |
|
2115 | + case 'double': |
|
2116 | + case 'real': |
|
2117 | + case 'float': |
|
2118 | + return 'float'; |
|
2119 | + case 'int': |
|
2120 | + case 'integer': |
|
2121 | + return 'integer'; |
|
2122 | + case 'array()': |
|
2123 | + case 'array': |
|
2124 | + return 'array'; |
|
2125 | 2125 | }//end switch |
2126 | 2126 | |
2127 | 2127 | if (strpos($lowerVarType, 'array(') !== false) { |
@@ -14,30 +14,30 @@ discard block |
||
14 | 14 | * @link http://pear.php.net/package/PHP_CodeSniffer |
15 | 15 | */ |
16 | 16 | |
17 | -spl_autoload_register(array('PHP_CodeSniffer', 'autoload')); |
|
17 | +spl_autoload_register( array( 'PHP_CodeSniffer', 'autoload' ) ); |
|
18 | 18 | |
19 | -if (class_exists('PHP_CodeSniffer_Exception', true) === false) { |
|
20 | - throw new Exception('Class PHP_CodeSniffer_Exception not found'); |
|
19 | +if ( class_exists( 'PHP_CodeSniffer_Exception', true ) === false ) { |
|
20 | + throw new Exception( 'Class PHP_CodeSniffer_Exception not found' ); |
|
21 | 21 | } |
22 | 22 | |
23 | -if (class_exists('PHP_CodeSniffer_File', true) === false) { |
|
24 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_File not found'); |
|
23 | +if ( class_exists( 'PHP_CodeSniffer_File', true ) === false ) { |
|
24 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_File not found' ); |
|
25 | 25 | } |
26 | 26 | |
27 | -if (class_exists('PHP_CodeSniffer_Fixer', true) === false) { |
|
28 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Fixer not found'); |
|
27 | +if ( class_exists( 'PHP_CodeSniffer_Fixer', true ) === false ) { |
|
28 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_Fixer not found' ); |
|
29 | 29 | } |
30 | 30 | |
31 | -if (class_exists('PHP_CodeSniffer_Tokens', true) === false) { |
|
32 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Tokens not found'); |
|
31 | +if ( class_exists( 'PHP_CodeSniffer_Tokens', true ) === false ) { |
|
32 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_Tokens not found' ); |
|
33 | 33 | } |
34 | 34 | |
35 | -if (class_exists('PHP_CodeSniffer_CLI', true) === false) { |
|
36 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CLI not found'); |
|
35 | +if ( class_exists( 'PHP_CodeSniffer_CLI', true ) === false ) { |
|
36 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_CLI not found' ); |
|
37 | 37 | } |
38 | 38 | |
39 | -if (interface_exists('PHP_CodeSniffer_Sniff', true) === false) { |
|
40 | - throw new PHP_CodeSniffer_Exception('Interface PHP_CodeSniffer_Sniff not found'); |
|
39 | +if ( interface_exists( 'PHP_CodeSniffer_Sniff', true ) === false ) { |
|
40 | + throw new PHP_CodeSniffer_Exception( 'Interface PHP_CodeSniffer_Sniff not found' ); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -225,37 +225,37 @@ discard block |
||
225 | 225 | * @see process() |
226 | 226 | */ |
227 | 227 | public function __construct( |
228 | - $verbosity=0, |
|
229 | - $tabWidth=0, |
|
230 | - $encoding='iso-8859-1', |
|
231 | - $interactive=false |
|
228 | + $verbosity = 0, |
|
229 | + $tabWidth = 0, |
|
230 | + $encoding = 'iso-8859-1', |
|
231 | + $interactive = false |
|
232 | 232 | ) { |
233 | - if ($verbosity !== null) { |
|
234 | - $this->setVerbosity($verbosity); |
|
233 | + if ( $verbosity !== null ) { |
|
234 | + $this->setVerbosity( $verbosity ); |
|
235 | 235 | } |
236 | 236 | |
237 | - if ($tabWidth !== null) { |
|
238 | - $this->setTabWidth($tabWidth); |
|
237 | + if ( $tabWidth !== null ) { |
|
238 | + $this->setTabWidth( $tabWidth ); |
|
239 | 239 | } |
240 | 240 | |
241 | - if ($encoding !== null) { |
|
242 | - $this->setEncoding($encoding); |
|
241 | + if ( $encoding !== null ) { |
|
242 | + $this->setEncoding( $encoding ); |
|
243 | 243 | } |
244 | 244 | |
245 | - if ($interactive !== null) { |
|
246 | - $this->setInteractive($interactive); |
|
245 | + if ( $interactive !== null ) { |
|
246 | + $this->setInteractive( $interactive ); |
|
247 | 247 | } |
248 | 248 | |
249 | - if (defined('PHPCS_DEFAULT_ERROR_SEV') === false) { |
|
250 | - define('PHPCS_DEFAULT_ERROR_SEV', 5); |
|
249 | + if ( defined( 'PHPCS_DEFAULT_ERROR_SEV' ) === false ) { |
|
250 | + define( 'PHPCS_DEFAULT_ERROR_SEV', 5 ); |
|
251 | 251 | } |
252 | 252 | |
253 | - if (defined('PHPCS_DEFAULT_WARN_SEV') === false) { |
|
254 | - define('PHPCS_DEFAULT_WARN_SEV', 5); |
|
253 | + if ( defined( 'PHPCS_DEFAULT_WARN_SEV' ) === false ) { |
|
254 | + define( 'PHPCS_DEFAULT_WARN_SEV', 5 ); |
|
255 | 255 | } |
256 | 256 | |
257 | - if (defined('PHP_CODESNIFFER_CBF') === false) { |
|
258 | - define('PHP_CODESNIFFER_CBF', false); |
|
257 | + if ( defined( 'PHP_CODESNIFFER_CBF' ) === false ) { |
|
258 | + define( 'PHP_CODESNIFFER_CBF', false ); |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | // Set default CLI object in case someone is running us |
@@ -277,34 +277,34 @@ discard block |
||
277 | 277 | * |
278 | 278 | * @return void |
279 | 279 | */ |
280 | - public static function autoload($className) |
|
280 | + public static function autoload( $className ) |
|
281 | 281 | { |
282 | - if (substr($className, 0, 4) === 'PHP_') { |
|
283 | - $newClassName = substr($className, 4); |
|
282 | + if ( substr( $className, 0, 4 ) === 'PHP_' ) { |
|
283 | + $newClassName = substr( $className, 4 ); |
|
284 | 284 | } else { |
285 | 285 | $newClassName = $className; |
286 | 286 | } |
287 | 287 | |
288 | - $path = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $newClassName).'.php'; |
|
288 | + $path = str_replace( array( '_', '\\' ), DIRECTORY_SEPARATOR, $newClassName ) . '.php'; |
|
289 | 289 | |
290 | - if (is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.$path) === true) { |
|
290 | + if ( is_file( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $path ) === true ) { |
|
291 | 291 | // Check standard file locations based on class name. |
292 | - include dirname(__FILE__).DIRECTORY_SEPARATOR.$path; |
|
292 | + include dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $path; |
|
293 | 293 | return; |
294 | 294 | } else { |
295 | 295 | // Check for included sniffs. |
296 | 296 | $installedPaths = PHP_CodeSniffer::getInstalledStandardPaths(); |
297 | - foreach ($installedPaths as $installedPath) { |
|
298 | - if (is_file($installedPath.DIRECTORY_SEPARATOR.$path) === true) { |
|
299 | - include $installedPath.DIRECTORY_SEPARATOR.$path; |
|
297 | + foreach ( $installedPaths as $installedPath ) { |
|
298 | + if ( is_file( $installedPath . DIRECTORY_SEPARATOR . $path ) === true ) { |
|
299 | + include $installedPath . DIRECTORY_SEPARATOR . $path; |
|
300 | 300 | return; |
301 | 301 | } |
302 | 302 | } |
303 | 303 | |
304 | 304 | // Check standard file locations based on the loaded rulesets. |
305 | - foreach (self::$rulesetDirs as $rulesetDir) { |
|
306 | - if (is_file(dirname($rulesetDir).DIRECTORY_SEPARATOR.$path) === true) { |
|
307 | - include_once dirname($rulesetDir).DIRECTORY_SEPARATOR.$path; |
|
305 | + foreach ( self::$rulesetDirs as $rulesetDir ) { |
|
306 | + if ( is_file( dirname( $rulesetDir ) . DIRECTORY_SEPARATOR . $path ) === true ) { |
|
307 | + include_once dirname( $rulesetDir ) . DIRECTORY_SEPARATOR . $path; |
|
308 | 308 | return; |
309 | 309 | } |
310 | 310 | } |
@@ -326,10 +326,10 @@ discard block |
||
326 | 326 | * |
327 | 327 | * @return void |
328 | 328 | */ |
329 | - public function setVerbosity($verbosity) |
|
329 | + public function setVerbosity( $verbosity ) |
|
330 | 330 | { |
331 | - if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
|
332 | - define('PHP_CODESNIFFER_VERBOSITY', $verbosity); |
|
331 | + if ( defined( 'PHP_CODESNIFFER_VERBOSITY' ) === false ) { |
|
332 | + define( 'PHP_CODESNIFFER_VERBOSITY', $verbosity ); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | }//end setVerbosity() |
@@ -344,10 +344,10 @@ discard block |
||
344 | 344 | * |
345 | 345 | * @return void |
346 | 346 | */ |
347 | - public function setTabWidth($tabWidth) |
|
347 | + public function setTabWidth( $tabWidth ) |
|
348 | 348 | { |
349 | - if (defined('PHP_CODESNIFFER_TAB_WIDTH') === false) { |
|
350 | - define('PHP_CODESNIFFER_TAB_WIDTH', $tabWidth); |
|
349 | + if ( defined( 'PHP_CODESNIFFER_TAB_WIDTH' ) === false ) { |
|
350 | + define( 'PHP_CODESNIFFER_TAB_WIDTH', $tabWidth ); |
|
351 | 351 | } |
352 | 352 | |
353 | 353 | }//end setTabWidth() |
@@ -363,10 +363,10 @@ discard block |
||
363 | 363 | * |
364 | 364 | * @return void |
365 | 365 | */ |
366 | - public function setEncoding($encoding) |
|
366 | + public function setEncoding( $encoding ) |
|
367 | 367 | { |
368 | - if (defined('PHP_CODESNIFFER_ENCODING') === false) { |
|
369 | - define('PHP_CODESNIFFER_ENCODING', $encoding); |
|
368 | + if ( defined( 'PHP_CODESNIFFER_ENCODING' ) === false ) { |
|
369 | + define( 'PHP_CODESNIFFER_ENCODING', $encoding ); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | }//end setEncoding() |
@@ -380,10 +380,10 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return void |
382 | 382 | */ |
383 | - public function setInteractive($interactive) |
|
383 | + public function setInteractive( $interactive ) |
|
384 | 384 | { |
385 | - if (defined('PHP_CODESNIFFER_INTERACTIVE') === false) { |
|
386 | - define('PHP_CODESNIFFER_INTERACTIVE', $interactive); |
|
385 | + if ( defined( 'PHP_CODESNIFFER_INTERACTIVE' ) === false ) { |
|
386 | + define( 'PHP_CODESNIFFER_INTERACTIVE', $interactive ); |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | }//end setInteractive() |
@@ -400,24 +400,24 @@ discard block |
||
400 | 400 | * |
401 | 401 | * @return void |
402 | 402 | */ |
403 | - public function setAllowedFileExtensions(array $extensions) |
|
403 | + public function setAllowedFileExtensions( array $extensions ) |
|
404 | 404 | { |
405 | 405 | $newExtensions = array(); |
406 | - foreach ($extensions as $ext) { |
|
407 | - $slash = strpos($ext, '/'); |
|
408 | - if ($slash !== false) { |
|
406 | + foreach ( $extensions as $ext ) { |
|
407 | + $slash = strpos( $ext, '/' ); |
|
408 | + if ( $slash !== false ) { |
|
409 | 409 | // They specified the tokenizer too. |
410 | - list($ext, $tokenizer) = explode('/', $ext); |
|
411 | - $newExtensions[$ext] = strtoupper($tokenizer); |
|
410 | + list( $ext, $tokenizer ) = explode( '/', $ext ); |
|
411 | + $newExtensions[ $ext ] = strtoupper( $tokenizer ); |
|
412 | 412 | continue; |
413 | 413 | } |
414 | 414 | |
415 | - if (isset($this->allowedFileExtensions[$ext]) === true) { |
|
416 | - $newExtensions[$ext] = $this->allowedFileExtensions[$ext]; |
|
417 | - } else if (isset($this->defaultFileExtensions[$ext]) === true) { |
|
418 | - $newExtensions[$ext] = $this->defaultFileExtensions[$ext]; |
|
415 | + if ( isset( $this->allowedFileExtensions[ $ext ] ) === true ) { |
|
416 | + $newExtensions[ $ext ] = $this->allowedFileExtensions[ $ext ]; |
|
417 | + } else if ( isset( $this->defaultFileExtensions[ $ext ] ) === true ) { |
|
418 | + $newExtensions[ $ext ] = $this->defaultFileExtensions[ $ext ]; |
|
419 | 419 | } else { |
420 | - $newExtensions[$ext] = 'PHP'; |
|
420 | + $newExtensions[ $ext ] = 'PHP'; |
|
421 | 421 | } |
422 | 422 | } |
423 | 423 | |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | * |
439 | 439 | * @return void |
440 | 440 | */ |
441 | - public function setIgnorePatterns(array $patterns) |
|
441 | + public function setIgnorePatterns( array $patterns ) |
|
442 | 442 | { |
443 | 443 | $this->ignorePatterns = $patterns; |
444 | 444 | |
@@ -456,14 +456,14 @@ discard block |
||
456 | 456 | * |
457 | 457 | * @return array |
458 | 458 | */ |
459 | - public function getIgnorePatterns($listener=null) |
|
459 | + public function getIgnorePatterns( $listener = null ) |
|
460 | 460 | { |
461 | - if ($listener === null) { |
|
461 | + if ( $listener === null ) { |
|
462 | 462 | return $this->ignorePatterns; |
463 | 463 | } |
464 | 464 | |
465 | - if (isset($this->ignorePatterns[$listener]) === true) { |
|
466 | - return $this->ignorePatterns[$listener]; |
|
465 | + if ( isset( $this->ignorePatterns[ $listener ] ) === true ) { |
|
466 | + return $this->ignorePatterns[ $listener ]; |
|
467 | 467 | } |
468 | 468 | |
469 | 469 | return array(); |
@@ -478,7 +478,7 @@ discard block |
||
478 | 478 | * |
479 | 479 | * @return void |
480 | 480 | */ |
481 | - public function setCli($cli) |
|
481 | + public function setCli( $cli ) |
|
482 | 482 | { |
483 | 483 | $this->cli = $cli; |
484 | 484 | |
@@ -499,11 +499,11 @@ discard block |
||
499 | 499 | * |
500 | 500 | * @return void |
501 | 501 | */ |
502 | - public function process($files, $standards, array $restrictions=array(), $local=false) |
|
502 | + public function process( $files, $standards, array $restrictions = array(), $local = false ) |
|
503 | 503 | { |
504 | 504 | $files = (array) $files; |
505 | - $this->initStandard($standards, $restrictions); |
|
506 | - $this->processFiles($files, $local); |
|
505 | + $this->initStandard( $standards, $restrictions ); |
|
506 | + $this->processFiles( $files, $local ); |
|
507 | 507 | |
508 | 508 | }//end process() |
509 | 509 | |
@@ -518,7 +518,7 @@ discard block |
||
518 | 518 | * |
519 | 519 | * @return void |
520 | 520 | */ |
521 | - public function initStandard($standards, array $restrictions=array(), array $exclusions=array()) |
|
521 | + public function initStandard( $standards, array $restrictions = array(), array $exclusions = array() ) |
|
522 | 522 | { |
523 | 523 | $standards = (array) $standards; |
524 | 524 | |
@@ -531,72 +531,72 @@ discard block |
||
531 | 531 | |
532 | 532 | // Ensure this option is enabled or else line endings will not always |
533 | 533 | // be detected properly for files created on a Mac with the /r line ending. |
534 | - ini_set('auto_detect_line_endings', true); |
|
534 | + ini_set( 'auto_detect_line_endings', true ); |
|
535 | 535 | |
536 | - if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) { |
|
536 | + if ( defined( 'PHP_CODESNIFFER_IN_TESTS' ) === true && empty( $restrictions ) === false ) { |
|
537 | 537 | // Should be one standard and one sniff being tested at a time. |
538 | - $installed = $this->getInstalledStandardPath($standards[0]); |
|
539 | - if ($installed !== null) { |
|
538 | + $installed = $this->getInstalledStandardPath( $standards[ 0 ] ); |
|
539 | + if ( $installed !== null ) { |
|
540 | 540 | $standard = $installed; |
541 | 541 | } else { |
542 | - $standard = self::realpath($standards[0]); |
|
543 | - if (is_dir($standard) === true |
|
544 | - && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
542 | + $standard = self::realpath( $standards[ 0 ] ); |
|
543 | + if ( is_dir( $standard ) === true |
|
544 | + && is_file( self::realpath( $standard . DIRECTORY_SEPARATOR . 'ruleset.xml' ) ) === true |
|
545 | 545 | ) { |
546 | - $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
546 | + $standard = self::realpath( $standard . DIRECTORY_SEPARATOR . 'ruleset.xml' ); |
|
547 | 547 | } |
548 | 548 | } |
549 | 549 | |
550 | - $sniffs = $this->_expandRulesetReference($restrictions[0], dirname($standard)); |
|
550 | + $sniffs = $this->_expandRulesetReference( $restrictions[ 0 ], dirname( $standard ) ); |
|
551 | 551 | } else { |
552 | 552 | $sniffs = array(); |
553 | - foreach ($standards as $standard) { |
|
554 | - $installed = $this->getInstalledStandardPath($standard); |
|
555 | - if ($installed !== null) { |
|
553 | + foreach ( $standards as $standard ) { |
|
554 | + $installed = $this->getInstalledStandardPath( $standard ); |
|
555 | + if ( $installed !== null ) { |
|
556 | 556 | $standard = $installed; |
557 | 557 | } else { |
558 | - $standard = self::realpath($standard); |
|
559 | - if (is_dir($standard) === true |
|
560 | - && is_file(self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml')) === true |
|
558 | + $standard = self::realpath( $standard ); |
|
559 | + if ( is_dir( $standard ) === true |
|
560 | + && is_file( self::realpath( $standard . DIRECTORY_SEPARATOR . 'ruleset.xml' ) ) === true |
|
561 | 561 | ) { |
562 | - $standard = self::realpath($standard.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
562 | + $standard = self::realpath( $standard . DIRECTORY_SEPARATOR . 'ruleset.xml' ); |
|
563 | 563 | } |
564 | 564 | } |
565 | 565 | |
566 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
567 | - $ruleset = simplexml_load_string(file_get_contents($standard)); |
|
568 | - if ($ruleset !== false) { |
|
569 | - $standardName = (string) $ruleset['name']; |
|
566 | + if ( PHP_CODESNIFFER_VERBOSITY === 1 ) { |
|
567 | + $ruleset = simplexml_load_string( file_get_contents( $standard ) ); |
|
568 | + if ( $ruleset !== false ) { |
|
569 | + $standardName = (string) $ruleset[ 'name' ]; |
|
570 | 570 | } |
571 | 571 | |
572 | 572 | echo "Registering sniffs in the $standardName standard... "; |
573 | - if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) { |
|
573 | + if ( count( $standards ) > 1 || PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
574 | 574 | echo PHP_EOL; |
575 | 575 | } |
576 | 576 | } |
577 | 577 | |
578 | - $sniffs = array_merge($sniffs, $this->processRuleset($standard)); |
|
578 | + $sniffs = array_merge( $sniffs, $this->processRuleset( $standard ) ); |
|
579 | 579 | }//end foreach |
580 | 580 | }//end if |
581 | 581 | |
582 | 582 | $sniffRestrictions = array(); |
583 | - foreach ($restrictions as $sniffCode) { |
|
584 | - $parts = explode('.', strtolower($sniffCode)); |
|
585 | - $sniffRestrictions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
583 | + foreach ( $restrictions as $sniffCode ) { |
|
584 | + $parts = explode( '.', strtolower( $sniffCode ) ); |
|
585 | + $sniffRestrictions[ ] = $parts[ 0 ] . '_sniffs_' . $parts[ 1 ] . '_' . $parts[ 2 ] . 'sniff'; |
|
586 | 586 | } |
587 | 587 | |
588 | 588 | $sniffExclusions = array(); |
589 | - foreach ($exclusions as $sniffCode) { |
|
590 | - $parts = explode('.', strtolower($sniffCode)); |
|
591 | - $sniffExclusions[] = $parts[0].'_sniffs_'.$parts[1].'_'.$parts[2].'sniff'; |
|
589 | + foreach ( $exclusions as $sniffCode ) { |
|
590 | + $parts = explode( '.', strtolower( $sniffCode ) ); |
|
591 | + $sniffExclusions[ ] = $parts[ 0 ] . '_sniffs_' . $parts[ 1 ] . '_' . $parts[ 2 ] . 'sniff'; |
|
592 | 592 | } |
593 | 593 | |
594 | - $this->registerSniffs($sniffs, $sniffRestrictions, $sniffExclusions); |
|
594 | + $this->registerSniffs( $sniffs, $sniffRestrictions, $sniffExclusions ); |
|
595 | 595 | $this->populateTokenListeners(); |
596 | 596 | |
597 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
598 | - $numSniffs = count($this->sniffs); |
|
599 | - echo "DONE ($numSniffs sniffs registered)".PHP_EOL; |
|
597 | + if ( PHP_CODESNIFFER_VERBOSITY === 1 ) { |
|
598 | + $numSniffs = count( $this->sniffs ); |
|
599 | + echo "DONE ($numSniffs sniffs registered)" . PHP_EOL; |
|
600 | 600 | } |
601 | 601 | |
602 | 602 | }//end initStandard() |
@@ -613,47 +613,47 @@ discard block |
||
613 | 613 | * @return void |
614 | 614 | * @throws PHP_CodeSniffer_Exception If files are invalid. |
615 | 615 | */ |
616 | - public function processFiles($files, $local=false) |
|
616 | + public function processFiles( $files, $local = false ) |
|
617 | 617 | { |
618 | 618 | $files = (array) $files; |
619 | 619 | $cliValues = $this->cli->getCommandLineValues(); |
620 | - $showProgress = $cliValues['showProgress']; |
|
621 | - $useColors = $cliValues['colors']; |
|
620 | + $showProgress = $cliValues[ 'showProgress' ]; |
|
621 | + $useColors = $cliValues[ 'colors' ]; |
|
622 | 622 | |
623 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
623 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
624 | 624 | echo 'Creating file list... '; |
625 | 625 | } |
626 | 626 | |
627 | - if (empty($this->allowedFileExtensions) === true) { |
|
627 | + if ( empty( $this->allowedFileExtensions ) === true ) { |
|
628 | 628 | $this->allowedFileExtensions = $this->defaultFileExtensions; |
629 | 629 | } |
630 | 630 | |
631 | - $todo = $this->getFilesToProcess($files, $local); |
|
632 | - $numFiles = count($todo); |
|
631 | + $todo = $this->getFilesToProcess( $files, $local ); |
|
632 | + $numFiles = count( $todo ); |
|
633 | 633 | |
634 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
635 | - echo "DONE ($numFiles files in queue)".PHP_EOL; |
|
634 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
635 | + echo "DONE ($numFiles files in queue)" . PHP_EOL; |
|
636 | 636 | } |
637 | 637 | |
638 | 638 | $numProcessed = 0; |
639 | 639 | $dots = 0; |
640 | - $maxLength = strlen($numFiles); |
|
640 | + $maxLength = strlen( $numFiles ); |
|
641 | 641 | $lastDir = ''; |
642 | - foreach ($todo as $file) { |
|
642 | + foreach ( $todo as $file ) { |
|
643 | 643 | $this->file = $file; |
644 | - $currDir = dirname($file); |
|
645 | - if ($lastDir !== $currDir) { |
|
646 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true) { |
|
647 | - echo 'Changing into directory '.$currDir.PHP_EOL; |
|
644 | + $currDir = dirname( $file ); |
|
645 | + if ( $lastDir !== $currDir ) { |
|
646 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true ) { |
|
647 | + echo 'Changing into directory ' . $currDir . PHP_EOL; |
|
648 | 648 | } |
649 | 649 | |
650 | 650 | $lastDir = $currDir; |
651 | 651 | } |
652 | 652 | |
653 | - $phpcsFile = $this->processFile($file, null); |
|
653 | + $phpcsFile = $this->processFile( $file, null ); |
|
654 | 654 | $numProcessed++; |
655 | 655 | |
656 | - if (PHP_CODESNIFFER_VERBOSITY > 0 |
|
656 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 |
|
657 | 657 | || PHP_CODESNIFFER_INTERACTIVE === true |
658 | 658 | || $showProgress === false |
659 | 659 | ) { |
@@ -661,19 +661,19 @@ discard block |
||
661 | 661 | } |
662 | 662 | |
663 | 663 | // Show progress information. |
664 | - if ($phpcsFile === null) { |
|
664 | + if ( $phpcsFile === null ) { |
|
665 | 665 | echo 'S'; |
666 | 666 | } else { |
667 | 667 | $errors = $phpcsFile->getErrorCount(); |
668 | 668 | $warnings = $phpcsFile->getWarningCount(); |
669 | - if ($errors > 0) { |
|
670 | - if ($useColors === true) { |
|
669 | + if ( $errors > 0 ) { |
|
670 | + if ( $useColors === true ) { |
|
671 | 671 | echo "\033[31m"; |
672 | 672 | } |
673 | 673 | |
674 | 674 | echo 'E'; |
675 | - } else if ($warnings > 0) { |
|
676 | - if ($useColors === true) { |
|
675 | + } else if ( $warnings > 0 ) { |
|
676 | + if ( $useColors === true ) { |
|
677 | 677 | echo "\033[33m"; |
678 | 678 | } |
679 | 679 | |
@@ -682,26 +682,26 @@ discard block |
||
682 | 682 | echo '.'; |
683 | 683 | } |
684 | 684 | |
685 | - if ($useColors === true) { |
|
685 | + if ( $useColors === true ) { |
|
686 | 686 | echo "\033[0m"; |
687 | 687 | } |
688 | 688 | }//end if |
689 | 689 | |
690 | 690 | $dots++; |
691 | - if ($dots === 60) { |
|
692 | - $padding = ($maxLength - strlen($numProcessed)); |
|
693 | - echo str_repeat(' ', $padding); |
|
694 | - $percent = round(($numProcessed / $numFiles) * 100); |
|
695 | - echo " $numProcessed / $numFiles ($percent%)".PHP_EOL; |
|
691 | + if ( $dots === 60 ) { |
|
692 | + $padding = ( $maxLength - strlen( $numProcessed ) ); |
|
693 | + echo str_repeat( ' ', $padding ); |
|
694 | + $percent = round( ( $numProcessed / $numFiles ) * 100 ); |
|
695 | + echo " $numProcessed / $numFiles ($percent%)" . PHP_EOL; |
|
696 | 696 | $dots = 0; |
697 | 697 | } |
698 | 698 | }//end foreach |
699 | 699 | |
700 | - if (PHP_CODESNIFFER_VERBOSITY === 0 |
|
700 | + if ( PHP_CODESNIFFER_VERBOSITY === 0 |
|
701 | 701 | && PHP_CODESNIFFER_INTERACTIVE === false |
702 | 702 | && $showProgress === true |
703 | 703 | ) { |
704 | - echo PHP_EOL.PHP_EOL; |
|
704 | + echo PHP_EOL . PHP_EOL; |
|
705 | 705 | } |
706 | 706 | |
707 | 707 | }//end processFiles() |
@@ -720,17 +720,17 @@ discard block |
||
720 | 720 | * @return array |
721 | 721 | * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid. |
722 | 722 | */ |
723 | - public function processRuleset($rulesetPath, $depth=0) |
|
723 | + public function processRuleset( $rulesetPath, $depth = 0 ) |
|
724 | 724 | { |
725 | - $rulesetPath = self::realpath($rulesetPath); |
|
726 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
727 | - echo str_repeat("\t", $depth); |
|
728 | - echo "Processing ruleset $rulesetPath".PHP_EOL; |
|
725 | + $rulesetPath = self::realpath( $rulesetPath ); |
|
726 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
727 | + echo str_repeat( "\t", $depth ); |
|
728 | + echo "Processing ruleset $rulesetPath" . PHP_EOL; |
|
729 | 729 | } |
730 | 730 | |
731 | - $ruleset = simplexml_load_string(file_get_contents($rulesetPath)); |
|
732 | - if ($ruleset === false) { |
|
733 | - throw new PHP_CodeSniffer_Exception("Ruleset $rulesetPath is not valid"); |
|
731 | + $ruleset = simplexml_load_string( file_get_contents( $rulesetPath ) ); |
|
732 | + if ( $ruleset === false ) { |
|
733 | + throw new PHP_CodeSniffer_Exception( "Ruleset $rulesetPath is not valid" ); |
|
734 | 734 | } |
735 | 735 | |
736 | 736 | $ownSniffs = array(); |
@@ -738,196 +738,196 @@ discard block |
||
738 | 738 | $excludedSniffs = array(); |
739 | 739 | $cliValues = $this->cli->getCommandLineValues(); |
740 | 740 | |
741 | - $rulesetDir = dirname($rulesetPath); |
|
742 | - self::$rulesetDirs[] = $rulesetDir; |
|
741 | + $rulesetDir = dirname( $rulesetPath ); |
|
742 | + self::$rulesetDirs[ ] = $rulesetDir; |
|
743 | 743 | |
744 | - if (is_dir($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs') === true) { |
|
745 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
746 | - echo str_repeat("\t", $depth); |
|
747 | - echo "\tAdding sniff files from \"/.../".basename($rulesetDir)."/Sniffs/\" directory".PHP_EOL; |
|
744 | + if ( is_dir( $rulesetDir . DIRECTORY_SEPARATOR . 'Sniffs' ) === true ) { |
|
745 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
746 | + echo str_repeat( "\t", $depth ); |
|
747 | + echo "\tAdding sniff files from \"/.../" . basename( $rulesetDir ) . "/Sniffs/\" directory" . PHP_EOL; |
|
748 | 748 | } |
749 | 749 | |
750 | - $ownSniffs = $this->_expandSniffDirectory($rulesetDir.DIRECTORY_SEPARATOR.'Sniffs', $depth); |
|
750 | + $ownSniffs = $this->_expandSniffDirectory( $rulesetDir . DIRECTORY_SEPARATOR . 'Sniffs', $depth ); |
|
751 | 751 | } |
752 | 752 | |
753 | 753 | // Process custom sniff config settings. |
754 | - foreach ($ruleset->{'config'} as $config) { |
|
755 | - if ($this->_shouldProcessElement($config) === false) { |
|
754 | + foreach ( $ruleset->{'config'} as $config ) { |
|
755 | + if ( $this->_shouldProcessElement( $config ) === false ) { |
|
756 | 756 | continue; |
757 | 757 | } |
758 | 758 | |
759 | - $this->setConfigData((string) $config['name'], (string) $config['value'], true); |
|
760 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
761 | - echo str_repeat("\t", $depth); |
|
762 | - echo "\t=> set config value ".(string) $config['name'].': '.(string) $config['value'].PHP_EOL; |
|
759 | + $this->setConfigData( (string) $config[ 'name' ], (string) $config[ 'value' ], true ); |
|
760 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
761 | + echo str_repeat( "\t", $depth ); |
|
762 | + echo "\t=> set config value " . (string) $config[ 'name' ] . ': ' . (string) $config[ 'value' ] . PHP_EOL; |
|
763 | 763 | } |
764 | 764 | } |
765 | 765 | |
766 | - foreach ($ruleset->rule as $rule) { |
|
767 | - if (isset($rule['ref']) === false |
|
768 | - || $this->_shouldProcessElement($rule) === false |
|
766 | + foreach ( $ruleset->rule as $rule ) { |
|
767 | + if ( isset( $rule[ 'ref' ] ) === false |
|
768 | + || $this->_shouldProcessElement( $rule ) === false |
|
769 | 769 | ) { |
770 | 770 | continue; |
771 | 771 | } |
772 | 772 | |
773 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
774 | - echo str_repeat("\t", $depth); |
|
775 | - echo "\tProcessing rule \"".$rule['ref'].'"'.PHP_EOL; |
|
773 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
774 | + echo str_repeat( "\t", $depth ); |
|
775 | + echo "\tProcessing rule \"" . $rule[ 'ref' ] . '"' . PHP_EOL; |
|
776 | 776 | } |
777 | 777 | |
778 | 778 | $includedSniffs = array_merge( |
779 | 779 | $includedSniffs, |
780 | - $this->_expandRulesetReference($rule['ref'], $rulesetDir, $depth) |
|
780 | + $this->_expandRulesetReference( $rule[ 'ref' ], $rulesetDir, $depth ) |
|
781 | 781 | ); |
782 | 782 | |
783 | - if (isset($rule->exclude) === true) { |
|
784 | - foreach ($rule->exclude as $exclude) { |
|
785 | - if ($this->_shouldProcessElement($exclude) === false) { |
|
783 | + if ( isset( $rule->exclude ) === true ) { |
|
784 | + foreach ( $rule->exclude as $exclude ) { |
|
785 | + if ( $this->_shouldProcessElement( $exclude ) === false ) { |
|
786 | 786 | continue; |
787 | 787 | } |
788 | 788 | |
789 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
790 | - echo str_repeat("\t", $depth); |
|
791 | - echo "\t\tExcluding rule \"".$exclude['name'].'"'.PHP_EOL; |
|
789 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
790 | + echo str_repeat( "\t", $depth ); |
|
791 | + echo "\t\tExcluding rule \"" . $exclude[ 'name' ] . '"' . PHP_EOL; |
|
792 | 792 | } |
793 | 793 | |
794 | 794 | // Check if a single code is being excluded, which is a shortcut |
795 | 795 | // for setting the severity of the message to 0. |
796 | - $parts = explode('.', $exclude['name']); |
|
797 | - if (count($parts) === 4) { |
|
798 | - $this->ruleset[(string) $exclude['name']]['severity'] = 0; |
|
799 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
800 | - echo str_repeat("\t", $depth); |
|
801 | - echo "\t\t=> severity set to 0".PHP_EOL; |
|
796 | + $parts = explode( '.', $exclude[ 'name' ] ); |
|
797 | + if ( count( $parts ) === 4 ) { |
|
798 | + $this->ruleset[ (string) $exclude[ 'name' ] ][ 'severity' ] = 0; |
|
799 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
800 | + echo str_repeat( "\t", $depth ); |
|
801 | + echo "\t\t=> severity set to 0" . PHP_EOL; |
|
802 | 802 | } |
803 | 803 | } else { |
804 | 804 | $excludedSniffs = array_merge( |
805 | 805 | $excludedSniffs, |
806 | - $this->_expandRulesetReference($exclude['name'], $rulesetDir, ($depth + 1)) |
|
806 | + $this->_expandRulesetReference( $exclude[ 'name' ], $rulesetDir, ( $depth + 1 ) ) |
|
807 | 807 | ); |
808 | 808 | } |
809 | 809 | }//end foreach |
810 | 810 | }//end if |
811 | 811 | |
812 | - $this->_processRule($rule, $depth); |
|
812 | + $this->_processRule( $rule, $depth ); |
|
813 | 813 | }//end foreach |
814 | 814 | |
815 | 815 | // Process custom command line arguments. |
816 | 816 | $cliArgs = array(); |
817 | - foreach ($ruleset->{'arg'} as $arg) { |
|
818 | - if ($this->_shouldProcessElement($arg) === false) { |
|
817 | + foreach ( $ruleset->{'arg'} as $arg ) { |
|
818 | + if ( $this->_shouldProcessElement( $arg ) === false ) { |
|
819 | 819 | continue; |
820 | 820 | } |
821 | 821 | |
822 | - if (isset($arg['name']) === true) { |
|
823 | - $argString = '--'.(string) $arg['name']; |
|
824 | - if (isset($arg['value']) === true) { |
|
825 | - $argString .= '='.(string) $arg['value']; |
|
822 | + if ( isset( $arg[ 'name' ] ) === true ) { |
|
823 | + $argString = '--' . (string) $arg[ 'name' ]; |
|
824 | + if ( isset( $arg[ 'value' ] ) === true ) { |
|
825 | + $argString .= '=' . (string) $arg[ 'value' ]; |
|
826 | 826 | } |
827 | 827 | } else { |
828 | - $argString = '-'.(string) $arg['value']; |
|
828 | + $argString = '-' . (string) $arg[ 'value' ]; |
|
829 | 829 | } |
830 | 830 | |
831 | - $cliArgs[] = $argString; |
|
831 | + $cliArgs[ ] = $argString; |
|
832 | 832 | |
833 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
834 | - echo str_repeat("\t", $depth); |
|
835 | - echo "\t=> set command line value $argString".PHP_EOL; |
|
833 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
834 | + echo str_repeat( "\t", $depth ); |
|
835 | + echo "\t=> set command line value $argString" . PHP_EOL; |
|
836 | 836 | } |
837 | 837 | }//end foreach |
838 | 838 | |
839 | 839 | // Set custom php ini values as CLI args. |
840 | - foreach ($ruleset->{'ini'} as $arg) { |
|
841 | - if ($this->_shouldProcessElement($arg) === false) { |
|
840 | + foreach ( $ruleset->{'ini'} as $arg ) { |
|
841 | + if ( $this->_shouldProcessElement( $arg ) === false ) { |
|
842 | 842 | continue; |
843 | 843 | } |
844 | 844 | |
845 | - if (isset($arg['name']) === false) { |
|
845 | + if ( isset( $arg[ 'name' ] ) === false ) { |
|
846 | 846 | continue; |
847 | 847 | } |
848 | 848 | |
849 | - $name = (string) $arg['name']; |
|
849 | + $name = (string) $arg[ 'name' ]; |
|
850 | 850 | $argString = $name; |
851 | - if (isset($arg['value']) === true) { |
|
852 | - $value = (string) $arg['value']; |
|
851 | + if ( isset( $arg[ 'value' ] ) === true ) { |
|
852 | + $value = (string) $arg[ 'value' ]; |
|
853 | 853 | $argString .= "=$value"; |
854 | 854 | } else { |
855 | 855 | $value = 'true'; |
856 | 856 | } |
857 | 857 | |
858 | - $cliArgs[] = '-d'; |
|
859 | - $cliArgs[] = $argString; |
|
858 | + $cliArgs[ ] = '-d'; |
|
859 | + $cliArgs[ ] = $argString; |
|
860 | 860 | |
861 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
862 | - echo str_repeat("\t", $depth); |
|
863 | - echo "\t=> set PHP ini value $name to $value".PHP_EOL; |
|
861 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
862 | + echo str_repeat( "\t", $depth ); |
|
863 | + echo "\t=> set PHP ini value $name to $value" . PHP_EOL; |
|
864 | 864 | } |
865 | 865 | }//end foreach |
866 | 866 | |
867 | - if (empty($cliValues['files']) === true && $cliValues['stdin'] === null) { |
|
867 | + if ( empty( $cliValues[ 'files' ] ) === true && $cliValues[ 'stdin' ] === null ) { |
|
868 | 868 | // Process hard-coded file paths. |
869 | - foreach ($ruleset->{'file'} as $file) { |
|
869 | + foreach ( $ruleset->{'file'} as $file ) { |
|
870 | 870 | $file = (string) $file; |
871 | - $cliArgs[] = $file; |
|
872 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
873 | - echo str_repeat("\t", $depth); |
|
874 | - echo "\t=> added \"$file\" to the file list".PHP_EOL; |
|
871 | + $cliArgs[ ] = $file; |
|
872 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
873 | + echo str_repeat( "\t", $depth ); |
|
874 | + echo "\t=> added \"$file\" to the file list" . PHP_EOL; |
|
875 | 875 | } |
876 | 876 | } |
877 | 877 | } |
878 | 878 | |
879 | - if (empty($cliArgs) === false) { |
|
879 | + if ( empty( $cliArgs ) === false ) { |
|
880 | 880 | // Change the directory so all relative paths are worked |
881 | 881 | // out based on the location of the ruleset instead of |
882 | 882 | // the location of the user. |
883 | - $inPhar = self::isPharFile($rulesetDir); |
|
884 | - if ($inPhar === false) { |
|
883 | + $inPhar = self::isPharFile( $rulesetDir ); |
|
884 | + if ( $inPhar === false ) { |
|
885 | 885 | $currentDir = getcwd(); |
886 | - chdir($rulesetDir); |
|
886 | + chdir( $rulesetDir ); |
|
887 | 887 | } |
888 | 888 | |
889 | - $this->cli->setCommandLineValues($cliArgs); |
|
889 | + $this->cli->setCommandLineValues( $cliArgs ); |
|
890 | 890 | |
891 | - if ($inPhar === false) { |
|
892 | - chdir($currentDir); |
|
891 | + if ( $inPhar === false ) { |
|
892 | + chdir( $currentDir ); |
|
893 | 893 | } |
894 | 894 | } |
895 | 895 | |
896 | 896 | // Process custom ignore pattern rules. |
897 | - foreach ($ruleset->{'exclude-pattern'} as $pattern) { |
|
898 | - if ($this->_shouldProcessElement($pattern) === false) { |
|
897 | + foreach ( $ruleset->{'exclude-pattern'} as $pattern ) { |
|
898 | + if ( $this->_shouldProcessElement( $pattern ) === false ) { |
|
899 | 899 | continue; |
900 | 900 | } |
901 | 901 | |
902 | - if (isset($pattern['type']) === false) { |
|
903 | - $pattern['type'] = 'absolute'; |
|
902 | + if ( isset( $pattern[ 'type' ] ) === false ) { |
|
903 | + $pattern[ 'type' ] = 'absolute'; |
|
904 | 904 | } |
905 | 905 | |
906 | - $this->ignorePatterns[(string) $pattern] = (string) $pattern['type']; |
|
907 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
908 | - echo str_repeat("\t", $depth); |
|
909 | - echo "\t=> added global ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
906 | + $this->ignorePatterns[ (string) $pattern ] = (string) $pattern[ 'type' ]; |
|
907 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
908 | + echo str_repeat( "\t", $depth ); |
|
909 | + echo "\t=> added global " . (string) $pattern[ 'type' ] . ' ignore pattern: ' . (string) $pattern . PHP_EOL; |
|
910 | 910 | } |
911 | 911 | } |
912 | 912 | |
913 | - $includedSniffs = array_unique(array_merge($ownSniffs, $includedSniffs)); |
|
914 | - $excludedSniffs = array_unique($excludedSniffs); |
|
913 | + $includedSniffs = array_unique( array_merge( $ownSniffs, $includedSniffs ) ); |
|
914 | + $excludedSniffs = array_unique( $excludedSniffs ); |
|
915 | 915 | |
916 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
917 | - $included = count($includedSniffs); |
|
918 | - $excluded = count($excludedSniffs); |
|
919 | - echo str_repeat("\t", $depth); |
|
920 | - echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded".PHP_EOL; |
|
916 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
917 | + $included = count( $includedSniffs ); |
|
918 | + $excluded = count( $excludedSniffs ); |
|
919 | + echo str_repeat( "\t", $depth ); |
|
920 | + echo "=> Ruleset processing complete; included $included sniffs and excluded $excluded" . PHP_EOL; |
|
921 | 921 | } |
922 | 922 | |
923 | 923 | // Merge our own sniff list with our externally included |
924 | 924 | // sniff list, but filter out any excluded sniffs. |
925 | 925 | $files = array(); |
926 | - foreach ($includedSniffs as $sniff) { |
|
927 | - if (in_array($sniff, $excludedSniffs) === true) { |
|
926 | + foreach ( $includedSniffs as $sniff ) { |
|
927 | + if ( in_array( $sniff, $excludedSniffs ) === true ) { |
|
928 | 928 | continue; |
929 | 929 | } else { |
930 | - $files[] = self::realpath($sniff); |
|
930 | + $files[ ] = self::realpath( $sniff ); |
|
931 | 931 | } |
932 | 932 | } |
933 | 933 | |
@@ -945,36 +945,36 @@ discard block |
||
945 | 945 | * |
946 | 946 | * @return array |
947 | 947 | */ |
948 | - private function _expandSniffDirectory($directory, $depth=0) |
|
948 | + private function _expandSniffDirectory( $directory, $depth = 0 ) |
|
949 | 949 | { |
950 | 950 | $sniffs = array(); |
951 | 951 | |
952 | - if (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') === true) { |
|
953 | - $rdi = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS); |
|
952 | + if ( defined( 'RecursiveDirectoryIterator::FOLLOW_SYMLINKS' ) === true ) { |
|
953 | + $rdi = new RecursiveDirectoryIterator( $directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS ); |
|
954 | 954 | } else { |
955 | - $rdi = new RecursiveDirectoryIterator($directory); |
|
955 | + $rdi = new RecursiveDirectoryIterator( $directory ); |
|
956 | 956 | } |
957 | 957 | |
958 | - $di = new RecursiveIteratorIterator($rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD); |
|
958 | + $di = new RecursiveIteratorIterator( $rdi, 0, RecursiveIteratorIterator::CATCH_GET_CHILD ); |
|
959 | 959 | |
960 | - $dirLen = strlen($directory); |
|
960 | + $dirLen = strlen( $directory ); |
|
961 | 961 | |
962 | - foreach ($di as $file) { |
|
962 | + foreach ( $di as $file ) { |
|
963 | 963 | $filename = $file->getFilename(); |
964 | 964 | |
965 | 965 | // Skip hidden files. |
966 | - if (substr($filename, 0, 1) === '.') { |
|
966 | + if ( substr( $filename, 0, 1 ) === '.' ) { |
|
967 | 967 | continue; |
968 | 968 | } |
969 | 969 | |
970 | 970 | // We are only interested in PHP and sniff files. |
971 | - $fileParts = explode('.', $filename); |
|
972 | - if (array_pop($fileParts) !== 'php') { |
|
971 | + $fileParts = explode( '.', $filename ); |
|
972 | + if ( array_pop( $fileParts ) !== 'php' ) { |
|
973 | 973 | continue; |
974 | 974 | } |
975 | 975 | |
976 | - $basename = basename($filename, '.php'); |
|
977 | - if (substr($basename, -5) !== 'Sniff') { |
|
976 | + $basename = basename( $filename, '.php' ); |
|
977 | + if ( substr( $basename, -5 ) !== 'Sniff' ) { |
|
978 | 978 | continue; |
979 | 979 | } |
980 | 980 | |
@@ -984,16 +984,16 @@ discard block |
||
984 | 984 | // standard. We use the offset with strpos() to allow hidden directories |
985 | 985 | // before, valid example: |
986 | 986 | // /home/foo/.composer/vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/... |
987 | - if (strpos($path, DIRECTORY_SEPARATOR.'.', $dirLen) !== false) { |
|
987 | + if ( strpos( $path, DIRECTORY_SEPARATOR . '.', $dirLen ) !== false ) { |
|
988 | 988 | continue; |
989 | 989 | } |
990 | 990 | |
991 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
992 | - echo str_repeat("\t", $depth); |
|
993 | - echo "\t\t=> $path".PHP_EOL; |
|
991 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
992 | + echo str_repeat( "\t", $depth ); |
|
993 | + echo "\t\t=> $path" . PHP_EOL; |
|
994 | 994 | } |
995 | 995 | |
996 | - $sniffs[] = $path; |
|
996 | + $sniffs[ ] = $path; |
|
997 | 997 | }//end foreach |
998 | 998 | |
999 | 999 | return $sniffs; |
@@ -1013,14 +1013,14 @@ discard block |
||
1013 | 1013 | * @return array |
1014 | 1014 | * @throws PHP_CodeSniffer_Exception If the reference is invalid. |
1015 | 1015 | */ |
1016 | - private function _expandRulesetReference($ref, $rulesetDir, $depth=0) |
|
1016 | + private function _expandRulesetReference( $ref, $rulesetDir, $depth = 0 ) |
|
1017 | 1017 | { |
1018 | 1018 | // Ignore internal sniffs codes as they are used to only |
1019 | 1019 | // hide and change internal messages. |
1020 | - if (substr($ref, 0, 9) === 'Internal.') { |
|
1021 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1022 | - echo str_repeat("\t", $depth); |
|
1023 | - echo "\t\t* ignoring internal sniff code *".PHP_EOL; |
|
1020 | + if ( substr( $ref, 0, 9 ) === 'Internal.' ) { |
|
1021 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1022 | + echo str_repeat( "\t", $depth ); |
|
1023 | + echo "\t\t* ignoring internal sniff code *" . PHP_EOL; |
|
1024 | 1024 | } |
1025 | 1025 | |
1026 | 1026 | return array(); |
@@ -1030,103 +1030,103 @@ discard block |
||
1030 | 1030 | // this format are relative paths and attempt to convert them |
1031 | 1031 | // to absolute paths. If this fails, let the reference run through |
1032 | 1032 | // the normal checks and have it fail as normal. |
1033 | - if (substr($ref, 0, 1) === '.') { |
|
1034 | - $realpath = self::realpath($rulesetDir.'/'.$ref); |
|
1035 | - if ($realpath !== false) { |
|
1033 | + if ( substr( $ref, 0, 1 ) === '.' ) { |
|
1034 | + $realpath = self::realpath( $rulesetDir . '/' . $ref ); |
|
1035 | + if ( $realpath !== false ) { |
|
1036 | 1036 | $ref = $realpath; |
1037 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1038 | - echo str_repeat("\t", $depth); |
|
1039 | - echo "\t\t=> $ref".PHP_EOL; |
|
1037 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1038 | + echo str_repeat( "\t", $depth ); |
|
1039 | + echo "\t\t=> $ref" . PHP_EOL; |
|
1040 | 1040 | } |
1041 | 1041 | } |
1042 | 1042 | } |
1043 | 1043 | |
1044 | 1044 | // As sniffs can't begin with a tilde, assume references in |
1045 | 1045 | // this format at relative to the user's home directory. |
1046 | - if (substr($ref, 0, 2) === '~/') { |
|
1047 | - $realpath = self::realpath($ref); |
|
1048 | - if ($realpath !== false) { |
|
1046 | + if ( substr( $ref, 0, 2 ) === '~/' ) { |
|
1047 | + $realpath = self::realpath( $ref ); |
|
1048 | + if ( $realpath !== false ) { |
|
1049 | 1049 | $ref = $realpath; |
1050 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1051 | - echo str_repeat("\t", $depth); |
|
1052 | - echo "\t\t=> $ref".PHP_EOL; |
|
1050 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1051 | + echo str_repeat( "\t", $depth ); |
|
1052 | + echo "\t\t=> $ref" . PHP_EOL; |
|
1053 | 1053 | } |
1054 | 1054 | } |
1055 | 1055 | } |
1056 | 1056 | |
1057 | - if (is_file($ref) === true) { |
|
1058 | - if (substr($ref, -9) === 'Sniff.php') { |
|
1057 | + if ( is_file( $ref ) === true ) { |
|
1058 | + if ( substr( $ref, -9 ) === 'Sniff.php' ) { |
|
1059 | 1059 | // A single external sniff. |
1060 | - self::$rulesetDirs[] = dirname(dirname(dirname($ref))); |
|
1061 | - return array($ref); |
|
1060 | + self::$rulesetDirs[ ] = dirname( dirname( dirname( $ref ) ) ); |
|
1061 | + return array( $ref ); |
|
1062 | 1062 | } |
1063 | 1063 | } else { |
1064 | 1064 | // See if this is a whole standard being referenced. |
1065 | - $path = $this->getInstalledStandardPath($ref); |
|
1066 | - if (self::isPharFile($path) === true && strpos($path, 'ruleset.xml') === false) { |
|
1065 | + $path = $this->getInstalledStandardPath( $ref ); |
|
1066 | + if ( self::isPharFile( $path ) === true && strpos( $path, 'ruleset.xml' ) === false ) { |
|
1067 | 1067 | // If the ruleset exists inside the phar file, use it. |
1068 | - if (file_exists($path.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1069 | - $path = $path.DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
1068 | + if ( file_exists( $path . DIRECTORY_SEPARATOR . 'ruleset.xml' ) === true ) { |
|
1069 | + $path = $path . DIRECTORY_SEPARATOR . 'ruleset.xml'; |
|
1070 | 1070 | } else { |
1071 | 1071 | $path = null; |
1072 | 1072 | } |
1073 | 1073 | } |
1074 | 1074 | |
1075 | - if ($path !== null) { |
|
1075 | + if ( $path !== null ) { |
|
1076 | 1076 | $ref = $path; |
1077 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1078 | - echo str_repeat("\t", $depth); |
|
1079 | - echo "\t\t=> $ref".PHP_EOL; |
|
1077 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1078 | + echo str_repeat( "\t", $depth ); |
|
1079 | + echo "\t\t=> $ref" . PHP_EOL; |
|
1080 | 1080 | } |
1081 | - } else if (is_dir($ref) === false) { |
|
1081 | + } else if ( is_dir( $ref ) === false ) { |
|
1082 | 1082 | // Work out the sniff path. |
1083 | - $sepPos = strpos($ref, DIRECTORY_SEPARATOR); |
|
1084 | - if ($sepPos !== false) { |
|
1085 | - $stdName = substr($ref, 0, $sepPos); |
|
1086 | - $path = substr($ref, $sepPos); |
|
1083 | + $sepPos = strpos( $ref, DIRECTORY_SEPARATOR ); |
|
1084 | + if ( $sepPos !== false ) { |
|
1085 | + $stdName = substr( $ref, 0, $sepPos ); |
|
1086 | + $path = substr( $ref, $sepPos ); |
|
1087 | 1087 | } else { |
1088 | - $parts = explode('.', $ref); |
|
1089 | - $stdName = $parts[0]; |
|
1090 | - if (count($parts) === 1) { |
|
1088 | + $parts = explode( '.', $ref ); |
|
1089 | + $stdName = $parts[ 0 ]; |
|
1090 | + if ( count( $parts ) === 1 ) { |
|
1091 | 1091 | // A whole standard? |
1092 | 1092 | $path = ''; |
1093 | - } else if (count($parts) === 2) { |
|
1093 | + } else if ( count( $parts ) === 2 ) { |
|
1094 | 1094 | // A directory of sniffs? |
1095 | - $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1]; |
|
1095 | + $path = DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR . $parts[ 1 ]; |
|
1096 | 1096 | } else { |
1097 | 1097 | // A single sniff? |
1098 | - $path = DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[1].DIRECTORY_SEPARATOR.$parts[2].'Sniff.php'; |
|
1098 | + $path = DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR . $parts[ 1 ] . DIRECTORY_SEPARATOR . $parts[ 2 ] . 'Sniff.php'; |
|
1099 | 1099 | } |
1100 | 1100 | } |
1101 | 1101 | |
1102 | 1102 | $newRef = false; |
1103 | - $stdPath = $this->getInstalledStandardPath($stdName); |
|
1104 | - if ($stdPath !== null && $path !== '') { |
|
1105 | - if (self::isPharFile($stdPath) === true |
|
1106 | - && strpos($stdPath, 'ruleset.xml') === false |
|
1103 | + $stdPath = $this->getInstalledStandardPath( $stdName ); |
|
1104 | + if ( $stdPath !== null && $path !== '' ) { |
|
1105 | + if ( self::isPharFile( $stdPath ) === true |
|
1106 | + && strpos( $stdPath, 'ruleset.xml' ) === false |
|
1107 | 1107 | ) { |
1108 | 1108 | // Phar files can only return the directory, |
1109 | 1109 | // since ruleset can be omitted if building one standard. |
1110 | - $newRef = self::realpath($stdPath.$path); |
|
1110 | + $newRef = self::realpath( $stdPath . $path ); |
|
1111 | 1111 | } else { |
1112 | - $newRef = self::realpath(dirname($stdPath).$path); |
|
1112 | + $newRef = self::realpath( dirname( $stdPath ) . $path ); |
|
1113 | 1113 | } |
1114 | 1114 | } |
1115 | 1115 | |
1116 | - if ($newRef === false) { |
|
1116 | + if ( $newRef === false ) { |
|
1117 | 1117 | // The sniff is not locally installed, so check if it is being |
1118 | 1118 | // referenced as a remote sniff outside the install. We do this |
1119 | 1119 | // by looking through all directories where we have found ruleset |
1120 | 1120 | // files before, looking for ones for this particular standard, |
1121 | 1121 | // and seeing if it is in there. |
1122 | - foreach (self::$rulesetDirs as $dir) { |
|
1123 | - if (strtolower(basename($dir)) !== strtolower($stdName)) { |
|
1122 | + foreach ( self::$rulesetDirs as $dir ) { |
|
1123 | + if ( strtolower( basename( $dir ) ) !== strtolower( $stdName ) ) { |
|
1124 | 1124 | continue; |
1125 | 1125 | } |
1126 | 1126 | |
1127 | - $newRef = self::realpath($dir.$path); |
|
1127 | + $newRef = self::realpath( $dir . $path ); |
|
1128 | 1128 | |
1129 | - if ($newRef !== false) { |
|
1129 | + if ( $newRef !== false ) { |
|
1130 | 1130 | $ref = $newRef; |
1131 | 1131 | } |
1132 | 1132 | } |
@@ -1134,50 +1134,50 @@ discard block |
||
1134 | 1134 | $ref = $newRef; |
1135 | 1135 | } |
1136 | 1136 | |
1137 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1138 | - echo str_repeat("\t", $depth); |
|
1139 | - echo "\t\t=> $ref".PHP_EOL; |
|
1137 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1138 | + echo str_repeat( "\t", $depth ); |
|
1139 | + echo "\t\t=> $ref" . PHP_EOL; |
|
1140 | 1140 | } |
1141 | 1141 | }//end if |
1142 | 1142 | }//end if |
1143 | 1143 | |
1144 | - if (is_dir($ref) === true) { |
|
1145 | - if (is_file($ref.DIRECTORY_SEPARATOR.'ruleset.xml') === true) { |
|
1144 | + if ( is_dir( $ref ) === true ) { |
|
1145 | + if ( is_file( $ref . DIRECTORY_SEPARATOR . 'ruleset.xml' ) === true ) { |
|
1146 | 1146 | // We are referencing an external coding standard. |
1147 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1148 | - echo str_repeat("\t", $depth); |
|
1149 | - echo "\t\t* rule is referencing a standard using directory name; processing *".PHP_EOL; |
|
1147 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1148 | + echo str_repeat( "\t", $depth ); |
|
1149 | + echo "\t\t* rule is referencing a standard using directory name; processing *" . PHP_EOL; |
|
1150 | 1150 | } |
1151 | 1151 | |
1152 | - return $this->processRuleset($ref.DIRECTORY_SEPARATOR.'ruleset.xml', ($depth + 2)); |
|
1152 | + return $this->processRuleset( $ref . DIRECTORY_SEPARATOR . 'ruleset.xml', ( $depth + 2 ) ); |
|
1153 | 1153 | } else { |
1154 | 1154 | // We are referencing a whole directory of sniffs. |
1155 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1156 | - echo str_repeat("\t", $depth); |
|
1157 | - echo "\t\t* rule is referencing a directory of sniffs *".PHP_EOL; |
|
1158 | - echo str_repeat("\t", $depth); |
|
1159 | - echo "\t\tAdding sniff files from directory".PHP_EOL; |
|
1155 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1156 | + echo str_repeat( "\t", $depth ); |
|
1157 | + echo "\t\t* rule is referencing a directory of sniffs *" . PHP_EOL; |
|
1158 | + echo str_repeat( "\t", $depth ); |
|
1159 | + echo "\t\tAdding sniff files from directory" . PHP_EOL; |
|
1160 | 1160 | } |
1161 | 1161 | |
1162 | - return $this->_expandSniffDirectory($ref, ($depth + 1)); |
|
1162 | + return $this->_expandSniffDirectory( $ref, ( $depth + 1 ) ); |
|
1163 | 1163 | } |
1164 | 1164 | } else { |
1165 | - if (is_file($ref) === false) { |
|
1165 | + if ( is_file( $ref ) === false ) { |
|
1166 | 1166 | $error = "Referenced sniff \"$ref\" does not exist"; |
1167 | - throw new PHP_CodeSniffer_Exception($error); |
|
1167 | + throw new PHP_CodeSniffer_Exception( $error ); |
|
1168 | 1168 | } |
1169 | 1169 | |
1170 | - if (substr($ref, -9) === 'Sniff.php') { |
|
1170 | + if ( substr( $ref, -9 ) === 'Sniff.php' ) { |
|
1171 | 1171 | // A single sniff. |
1172 | - return array($ref); |
|
1172 | + return array( $ref ); |
|
1173 | 1173 | } else { |
1174 | 1174 | // Assume an external ruleset.xml file. |
1175 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1176 | - echo str_repeat("\t", $depth); |
|
1177 | - echo "\t\t* rule is referencing a standard using ruleset path; processing *".PHP_EOL; |
|
1175 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1176 | + echo str_repeat( "\t", $depth ); |
|
1177 | + echo "\t\t* rule is referencing a standard using ruleset path; processing *" . PHP_EOL; |
|
1178 | 1178 | } |
1179 | 1179 | |
1180 | - return $this->processRuleset($ref, ($depth + 2)); |
|
1180 | + return $this->processRuleset( $ref, ( $depth + 2 ) ); |
|
1181 | 1181 | } |
1182 | 1182 | }//end if |
1183 | 1183 | |
@@ -1193,122 +1193,122 @@ discard block |
||
1193 | 1193 | * |
1194 | 1194 | * @return void |
1195 | 1195 | */ |
1196 | - private function _processRule($rule, $depth=0) |
|
1196 | + private function _processRule( $rule, $depth = 0 ) |
|
1197 | 1197 | { |
1198 | - $code = (string) $rule['ref']; |
|
1198 | + $code = (string) $rule[ 'ref' ]; |
|
1199 | 1199 | |
1200 | 1200 | // Custom severity. |
1201 | - if (isset($rule->severity) === true |
|
1202 | - && $this->_shouldProcessElement($rule->severity) === true |
|
1201 | + if ( isset( $rule->severity ) === true |
|
1202 | + && $this->_shouldProcessElement( $rule->severity ) === true |
|
1203 | 1203 | ) { |
1204 | - if (isset($this->ruleset[$code]) === false) { |
|
1205 | - $this->ruleset[$code] = array(); |
|
1204 | + if ( isset( $this->ruleset[ $code ] ) === false ) { |
|
1205 | + $this->ruleset[ $code ] = array(); |
|
1206 | 1206 | } |
1207 | 1207 | |
1208 | - $this->ruleset[$code]['severity'] = (int) $rule->severity; |
|
1209 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1210 | - echo str_repeat("\t", $depth); |
|
1211 | - echo "\t\t=> severity set to ".(int) $rule->severity.PHP_EOL; |
|
1208 | + $this->ruleset[ $code ][ 'severity' ] = (int) $rule->severity; |
|
1209 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1210 | + echo str_repeat( "\t", $depth ); |
|
1211 | + echo "\t\t=> severity set to " . (int) $rule->severity . PHP_EOL; |
|
1212 | 1212 | } |
1213 | 1213 | } |
1214 | 1214 | |
1215 | 1215 | // Custom message type. |
1216 | - if (isset($rule->type) === true |
|
1217 | - && $this->_shouldProcessElement($rule->type) === true |
|
1216 | + if ( isset( $rule->type ) === true |
|
1217 | + && $this->_shouldProcessElement( $rule->type ) === true |
|
1218 | 1218 | ) { |
1219 | - if (isset($this->ruleset[$code]) === false) { |
|
1220 | - $this->ruleset[$code] = array(); |
|
1219 | + if ( isset( $this->ruleset[ $code ] ) === false ) { |
|
1220 | + $this->ruleset[ $code ] = array(); |
|
1221 | 1221 | } |
1222 | 1222 | |
1223 | - $this->ruleset[$code]['type'] = (string) $rule->type; |
|
1224 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1225 | - echo str_repeat("\t", $depth); |
|
1226 | - echo "\t\t=> message type set to ".(string) $rule->type.PHP_EOL; |
|
1223 | + $this->ruleset[ $code ][ 'type' ] = (string) $rule->type; |
|
1224 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1225 | + echo str_repeat( "\t", $depth ); |
|
1226 | + echo "\t\t=> message type set to " . (string) $rule->type . PHP_EOL; |
|
1227 | 1227 | } |
1228 | 1228 | } |
1229 | 1229 | |
1230 | 1230 | // Custom message. |
1231 | - if (isset($rule->message) === true |
|
1232 | - && $this->_shouldProcessElement($rule->message) === true |
|
1231 | + if ( isset( $rule->message ) === true |
|
1232 | + && $this->_shouldProcessElement( $rule->message ) === true |
|
1233 | 1233 | ) { |
1234 | - if (isset($this->ruleset[$code]) === false) { |
|
1235 | - $this->ruleset[$code] = array(); |
|
1234 | + if ( isset( $this->ruleset[ $code ] ) === false ) { |
|
1235 | + $this->ruleset[ $code ] = array(); |
|
1236 | 1236 | } |
1237 | 1237 | |
1238 | - $this->ruleset[$code]['message'] = (string) $rule->message; |
|
1239 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1240 | - echo str_repeat("\t", $depth); |
|
1241 | - echo "\t\t=> message set to ".(string) $rule->message.PHP_EOL; |
|
1238 | + $this->ruleset[ $code ][ 'message' ] = (string) $rule->message; |
|
1239 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1240 | + echo str_repeat( "\t", $depth ); |
|
1241 | + echo "\t\t=> message set to " . (string) $rule->message . PHP_EOL; |
|
1242 | 1242 | } |
1243 | 1243 | } |
1244 | 1244 | |
1245 | 1245 | // Custom properties. |
1246 | - if (isset($rule->properties) === true |
|
1247 | - && $this->_shouldProcessElement($rule->properties) === true |
|
1246 | + if ( isset( $rule->properties ) === true |
|
1247 | + && $this->_shouldProcessElement( $rule->properties ) === true |
|
1248 | 1248 | ) { |
1249 | - foreach ($rule->properties->property as $prop) { |
|
1250 | - if ($this->_shouldProcessElement($prop) === false) { |
|
1249 | + foreach ( $rule->properties->property as $prop ) { |
|
1250 | + if ( $this->_shouldProcessElement( $prop ) === false ) { |
|
1251 | 1251 | continue; |
1252 | 1252 | } |
1253 | 1253 | |
1254 | - if (isset($this->ruleset[$code]) === false) { |
|
1255 | - $this->ruleset[$code] = array( |
|
1254 | + if ( isset( $this->ruleset[ $code ] ) === false ) { |
|
1255 | + $this->ruleset[ $code ] = array( |
|
1256 | 1256 | 'properties' => array(), |
1257 | 1257 | ); |
1258 | - } else if (isset($this->ruleset[$code]['properties']) === false) { |
|
1259 | - $this->ruleset[$code]['properties'] = array(); |
|
1258 | + } else if ( isset( $this->ruleset[ $code ][ 'properties' ] ) === false ) { |
|
1259 | + $this->ruleset[ $code ][ 'properties' ] = array(); |
|
1260 | 1260 | } |
1261 | 1261 | |
1262 | - $name = (string) $prop['name']; |
|
1263 | - if (isset($prop['type']) === true |
|
1264 | - && (string) $prop['type'] === 'array' |
|
1262 | + $name = (string) $prop[ 'name' ]; |
|
1263 | + if ( isset( $prop[ 'type' ] ) === true |
|
1264 | + && (string) $prop[ 'type' ] === 'array' |
|
1265 | 1265 | ) { |
1266 | - $value = (string) $prop['value']; |
|
1266 | + $value = (string) $prop[ 'value' ]; |
|
1267 | 1267 | $values = array(); |
1268 | - foreach (explode(',', $value) as $val) { |
|
1268 | + foreach ( explode( ',', $value ) as $val ) { |
|
1269 | 1269 | $v = ''; |
1270 | 1270 | |
1271 | - list($k,$v) = explode('=>', $val.'=>'); |
|
1272 | - if ($v !== '') { |
|
1273 | - $values[$k] = $v; |
|
1271 | + list( $k, $v ) = explode( '=>', $val . '=>' ); |
|
1272 | + if ( $v !== '' ) { |
|
1273 | + $values[ $k ] = $v; |
|
1274 | 1274 | } else { |
1275 | - $values[] = $k; |
|
1275 | + $values[ ] = $k; |
|
1276 | 1276 | } |
1277 | 1277 | } |
1278 | 1278 | |
1279 | - $this->ruleset[$code]['properties'][$name] = $values; |
|
1280 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1281 | - echo str_repeat("\t", $depth); |
|
1282 | - echo "\t\t=> array property \"$name\" set to \"$value\"".PHP_EOL; |
|
1279 | + $this->ruleset[ $code ][ 'properties' ][ $name ] = $values; |
|
1280 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1281 | + echo str_repeat( "\t", $depth ); |
|
1282 | + echo "\t\t=> array property \"$name\" set to \"$value\"" . PHP_EOL; |
|
1283 | 1283 | } |
1284 | 1284 | } else { |
1285 | - $this->ruleset[$code]['properties'][$name] = (string) $prop['value']; |
|
1286 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1287 | - echo str_repeat("\t", $depth); |
|
1288 | - echo "\t\t=> property \"$name\" set to \"".(string) $prop['value'].'"'.PHP_EOL; |
|
1285 | + $this->ruleset[ $code ][ 'properties' ][ $name ] = (string) $prop[ 'value' ]; |
|
1286 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1287 | + echo str_repeat( "\t", $depth ); |
|
1288 | + echo "\t\t=> property \"$name\" set to \"" . (string) $prop[ 'value' ] . '"' . PHP_EOL; |
|
1289 | 1289 | } |
1290 | 1290 | }//end if |
1291 | 1291 | }//end foreach |
1292 | 1292 | }//end if |
1293 | 1293 | |
1294 | 1294 | // Ignore patterns. |
1295 | - foreach ($rule->{'exclude-pattern'} as $pattern) { |
|
1296 | - if ($this->_shouldProcessElement($pattern) === false) { |
|
1295 | + foreach ( $rule->{'exclude-pattern'} as $pattern ) { |
|
1296 | + if ( $this->_shouldProcessElement( $pattern ) === false ) { |
|
1297 | 1297 | continue; |
1298 | 1298 | } |
1299 | 1299 | |
1300 | - if (isset($this->ignorePatterns[$code]) === false) { |
|
1301 | - $this->ignorePatterns[$code] = array(); |
|
1300 | + if ( isset( $this->ignorePatterns[ $code ] ) === false ) { |
|
1301 | + $this->ignorePatterns[ $code ] = array(); |
|
1302 | 1302 | } |
1303 | 1303 | |
1304 | - if (isset($pattern['type']) === false) { |
|
1305 | - $pattern['type'] = 'absolute'; |
|
1304 | + if ( isset( $pattern[ 'type' ] ) === false ) { |
|
1305 | + $pattern[ 'type' ] = 'absolute'; |
|
1306 | 1306 | } |
1307 | 1307 | |
1308 | - $this->ignorePatterns[$code][(string) $pattern] = (string) $pattern['type']; |
|
1309 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1310 | - echo str_repeat("\t", $depth); |
|
1311 | - echo "\t\t=> added sniff-specific ".(string) $pattern['type'].' ignore pattern: '.(string) $pattern.PHP_EOL; |
|
1308 | + $this->ignorePatterns[ $code ][ (string) $pattern ] = (string) $pattern[ 'type' ]; |
|
1309 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1310 | + echo str_repeat( "\t", $depth ); |
|
1311 | + echo "\t\t=> added sniff-specific " . (string) $pattern[ 'type' ] . ' ignore pattern: ' . (string) $pattern . PHP_EOL; |
|
1312 | 1312 | } |
1313 | 1313 | } |
1314 | 1314 | |
@@ -1324,25 +1324,25 @@ discard block |
||
1324 | 1324 | * |
1325 | 1325 | * @return bool |
1326 | 1326 | */ |
1327 | - private function _shouldProcessElement($element, $depth=0) |
|
1327 | + private function _shouldProcessElement( $element, $depth = 0 ) |
|
1328 | 1328 | { |
1329 | - if (isset($element['phpcbf-only']) === false |
|
1330 | - && isset($element['phpcs-only']) === false |
|
1329 | + if ( isset( $element[ 'phpcbf-only' ] ) === false |
|
1330 | + && isset( $element[ 'phpcs-only' ] ) === false |
|
1331 | 1331 | ) { |
1332 | 1332 | // No exceptions are being made. |
1333 | 1333 | return true; |
1334 | 1334 | } |
1335 | 1335 | |
1336 | - if (PHP_CODESNIFFER_CBF === true |
|
1337 | - && isset($element['phpcbf-only']) === true |
|
1338 | - && (string) $element['phpcbf-only'] === 'true' |
|
1336 | + if ( PHP_CODESNIFFER_CBF === true |
|
1337 | + && isset( $element[ 'phpcbf-only' ] ) === true |
|
1338 | + && (string) $element[ 'phpcbf-only' ] === 'true' |
|
1339 | 1339 | ) { |
1340 | 1340 | return true; |
1341 | 1341 | } |
1342 | 1342 | |
1343 | - if (PHP_CODESNIFFER_CBF === false |
|
1344 | - && isset($element['phpcs-only']) === true |
|
1345 | - && (string) $element['phpcs-only'] === 'true' |
|
1343 | + if ( PHP_CODESNIFFER_CBF === false |
|
1344 | + && isset( $element[ 'phpcs-only' ] ) === true |
|
1345 | + && (string) $element[ 'phpcs-only' ] === 'true' |
|
1346 | 1346 | ) { |
1347 | 1347 | return true; |
1348 | 1348 | } |
@@ -1364,44 +1364,44 @@ discard block |
||
1364 | 1364 | * @return void |
1365 | 1365 | * @throws PHP_CodeSniffer_Exception If a sniff file path is invalid. |
1366 | 1366 | */ |
1367 | - public function registerSniffs($files, $restrictions, $exclusions) |
|
1367 | + public function registerSniffs( $files, $restrictions, $exclusions ) |
|
1368 | 1368 | { |
1369 | 1369 | $listeners = array(); |
1370 | 1370 | |
1371 | - foreach ($files as $file) { |
|
1371 | + foreach ( $files as $file ) { |
|
1372 | 1372 | // Work out where the position of /StandardName/Sniffs/... is |
1373 | 1373 | // so we can determine what the class will be called. |
1374 | - $sniffPos = strrpos($file, DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR); |
|
1375 | - if ($sniffPos === false) { |
|
1374 | + $sniffPos = strrpos( $file, DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR ); |
|
1375 | + if ( $sniffPos === false ) { |
|
1376 | 1376 | continue; |
1377 | 1377 | } |
1378 | 1378 | |
1379 | - $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR); |
|
1380 | - if ($slashPos === false) { |
|
1379 | + $slashPos = strrpos( substr( $file, 0, $sniffPos ), DIRECTORY_SEPARATOR ); |
|
1380 | + if ( $slashPos === false ) { |
|
1381 | 1381 | continue; |
1382 | 1382 | } |
1383 | 1383 | |
1384 | - $className = substr($file, ($slashPos + 1)); |
|
1384 | + $className = substr( $file, ( $slashPos + 1 ) ); |
|
1385 | 1385 | |
1386 | - if (substr_count($className, DIRECTORY_SEPARATOR) !== 3) { |
|
1387 | - throw new PHP_CodeSniffer_Exception("Sniff file $className is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory"); |
|
1386 | + if ( substr_count( $className, DIRECTORY_SEPARATOR ) !== 3 ) { |
|
1387 | + throw new PHP_CodeSniffer_Exception( "Sniff file $className is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory" ); |
|
1388 | 1388 | } |
1389 | 1389 | |
1390 | - $className = substr($className, 0, -4); |
|
1391 | - $className = str_replace(DIRECTORY_SEPARATOR, '_', $className); |
|
1390 | + $className = substr( $className, 0, -4 ); |
|
1391 | + $className = str_replace( DIRECTORY_SEPARATOR, '_', $className ); |
|
1392 | 1392 | |
1393 | 1393 | // If they have specified a list of sniffs to restrict to, check |
1394 | 1394 | // to see if this sniff is allowed. |
1395 | - if (empty($restrictions) === false |
|
1396 | - && in_array(strtolower($className), $restrictions) === false |
|
1395 | + if ( empty( $restrictions ) === false |
|
1396 | + && in_array( strtolower( $className ), $restrictions ) === false |
|
1397 | 1397 | ) { |
1398 | 1398 | continue; |
1399 | 1399 | } |
1400 | 1400 | |
1401 | 1401 | // If they have specified a list of sniffs to exclude, check |
1402 | 1402 | // to see if this sniff is allowed. |
1403 | - if (empty($exclusions) === false |
|
1404 | - && in_array(strtolower($className), $exclusions) === true |
|
1403 | + if ( empty( $exclusions ) === false |
|
1404 | + && in_array( strtolower( $className ), $exclusions ) === true |
|
1405 | 1405 | ) { |
1406 | 1406 | continue; |
1407 | 1407 | } |
@@ -1411,21 +1411,21 @@ discard block |
||
1411 | 1411 | // Support the use of PHP namespaces. If the class name we included |
1412 | 1412 | // contains namespace separators instead of underscores, use this as the |
1413 | 1413 | // class name from now on. |
1414 | - $classNameNS = str_replace('_', '\\', $className); |
|
1415 | - if (class_exists($classNameNS, false) === true) { |
|
1414 | + $classNameNS = str_replace( '_', '\\', $className ); |
|
1415 | + if ( class_exists( $classNameNS, false ) === true ) { |
|
1416 | 1416 | $className = $classNameNS; |
1417 | 1417 | } |
1418 | 1418 | |
1419 | 1419 | // Skip abstract classes. |
1420 | - $reflection = new ReflectionClass($className); |
|
1421 | - if ($reflection->isAbstract() === true) { |
|
1420 | + $reflection = new ReflectionClass( $className ); |
|
1421 | + if ( $reflection->isAbstract() === true ) { |
|
1422 | 1422 | continue; |
1423 | 1423 | } |
1424 | 1424 | |
1425 | - $listeners[$className] = $className; |
|
1425 | + $listeners[ $className ] = $className; |
|
1426 | 1426 | |
1427 | - if (PHP_CODESNIFFER_VERBOSITY > 2) { |
|
1428 | - echo "Registered $className".PHP_EOL; |
|
1427 | + if ( PHP_CODESNIFFER_VERBOSITY > 2 ) { |
|
1428 | + echo "Registered $className" . PHP_EOL; |
|
1429 | 1429 | } |
1430 | 1430 | }//end foreach |
1431 | 1431 | |
@@ -1445,49 +1445,49 @@ discard block |
||
1445 | 1445 | // Construct a list of listeners indexed by token being listened for. |
1446 | 1446 | $this->_tokenListeners = array(); |
1447 | 1447 | |
1448 | - foreach ($this->sniffs as $listenerClass) { |
|
1448 | + foreach ( $this->sniffs as $listenerClass ) { |
|
1449 | 1449 | // Work out the internal code for this sniff. Detect usage of namespace |
1450 | 1450 | // separators instead of underscores to support PHP namespaces. |
1451 | - if (strstr($listenerClass, '\\') === false) { |
|
1452 | - $parts = explode('_', $listenerClass); |
|
1451 | + if ( strstr( $listenerClass, '\\' ) === false ) { |
|
1452 | + $parts = explode( '_', $listenerClass ); |
|
1453 | 1453 | } else { |
1454 | - $parts = explode('\\', $listenerClass); |
|
1454 | + $parts = explode( '\\', $listenerClass ); |
|
1455 | 1455 | } |
1456 | 1456 | |
1457 | - $code = $parts[0].'.'.$parts[2].'.'.$parts[3]; |
|
1458 | - $code = substr($code, 0, -5); |
|
1457 | + $code = $parts[ 0 ] . '.' . $parts[ 2 ] . '.' . $parts[ 3 ]; |
|
1458 | + $code = substr( $code, 0, -5 ); |
|
1459 | 1459 | |
1460 | - $this->listeners[$listenerClass] = new $listenerClass(); |
|
1461 | - $this->sniffCodes[$code] = $listenerClass; |
|
1460 | + $this->listeners[ $listenerClass ] = new $listenerClass(); |
|
1461 | + $this->sniffCodes[ $code ] = $listenerClass; |
|
1462 | 1462 | |
1463 | 1463 | // Set custom properties. |
1464 | - if (isset($this->ruleset[$code]['properties']) === true) { |
|
1465 | - foreach ($this->ruleset[$code]['properties'] as $name => $value) { |
|
1466 | - $this->setSniffProperty($listenerClass, $name, $value); |
|
1464 | + if ( isset( $this->ruleset[ $code ][ 'properties' ] ) === true ) { |
|
1465 | + foreach ( $this->ruleset[ $code ][ 'properties' ] as $name => $value ) { |
|
1466 | + $this->setSniffProperty( $listenerClass, $name, $value ); |
|
1467 | 1467 | } |
1468 | 1468 | } |
1469 | 1469 | |
1470 | 1470 | $tokenizers = array(); |
1471 | - $vars = get_class_vars($listenerClass); |
|
1472 | - if (isset($vars['supportedTokenizers']) === true) { |
|
1473 | - foreach ($vars['supportedTokenizers'] as $tokenizer) { |
|
1474 | - $tokenizers[$tokenizer] = $tokenizer; |
|
1471 | + $vars = get_class_vars( $listenerClass ); |
|
1472 | + if ( isset( $vars[ 'supportedTokenizers' ] ) === true ) { |
|
1473 | + foreach ( $vars[ 'supportedTokenizers' ] as $tokenizer ) { |
|
1474 | + $tokenizers[ $tokenizer ] = $tokenizer; |
|
1475 | 1475 | } |
1476 | 1476 | } else { |
1477 | - $tokenizers = array('PHP' => 'PHP'); |
|
1477 | + $tokenizers = array( 'PHP' => 'PHP' ); |
|
1478 | 1478 | } |
1479 | 1479 | |
1480 | - $tokens = $this->listeners[$listenerClass]->register(); |
|
1481 | - if (is_array($tokens) === false) { |
|
1480 | + $tokens = $this->listeners[ $listenerClass ]->register(); |
|
1481 | + if ( is_array( $tokens ) === false ) { |
|
1482 | 1482 | $msg = "Sniff $listenerClass register() method must return an array"; |
1483 | - throw new PHP_CodeSniffer_Exception($msg); |
|
1483 | + throw new PHP_CodeSniffer_Exception( $msg ); |
|
1484 | 1484 | } |
1485 | 1485 | |
1486 | - $parts = explode('_', str_replace('\\', '_', $listenerClass)); |
|
1487 | - $listenerSource = $parts[0].'.'.$parts[2].'.'.substr($parts[3], 0, -5); |
|
1486 | + $parts = explode( '_', str_replace( '\\', '_', $listenerClass ) ); |
|
1487 | + $listenerSource = $parts[ 0 ] . '.' . $parts[ 2 ] . '.' . substr( $parts[ 3 ], 0, -5 ); |
|
1488 | 1488 | $ignorePatterns = array(); |
1489 | - $patterns = $this->getIgnorePatterns($listenerSource); |
|
1490 | - foreach ($patterns as $pattern => $type) { |
|
1489 | + $patterns = $this->getIgnorePatterns( $listenerSource ); |
|
1490 | + foreach ( $patterns as $pattern => $type ) { |
|
1491 | 1491 | // While there is support for a type of each pattern |
1492 | 1492 | // (absolute or relative) we don't actually support it here. |
1493 | 1493 | $replacements = array( |
@@ -1495,16 +1495,16 @@ discard block |
||
1495 | 1495 | '*' => '.*', |
1496 | 1496 | ); |
1497 | 1497 | |
1498 | - $ignorePatterns[] = strtr($pattern, $replacements); |
|
1498 | + $ignorePatterns[ ] = strtr( $pattern, $replacements ); |
|
1499 | 1499 | } |
1500 | 1500 | |
1501 | - foreach ($tokens as $token) { |
|
1502 | - if (isset($this->_tokenListeners[$token]) === false) { |
|
1503 | - $this->_tokenListeners[$token] = array(); |
|
1501 | + foreach ( $tokens as $token ) { |
|
1502 | + if ( isset( $this->_tokenListeners[ $token ] ) === false ) { |
|
1503 | + $this->_tokenListeners[ $token ] = array(); |
|
1504 | 1504 | } |
1505 | 1505 | |
1506 | - if (isset($this->_tokenListeners[$token][$listenerClass]) === false) { |
|
1507 | - $this->_tokenListeners[$token][$listenerClass] = array( |
|
1506 | + if ( isset( $this->_tokenListeners[ $token ][ $listenerClass ] ) === false ) { |
|
1507 | + $this->_tokenListeners[ $token ][ $listenerClass ] = array( |
|
1508 | 1508 | 'class' => $listenerClass, |
1509 | 1509 | 'source' => $listenerSource, |
1510 | 1510 | 'tokenizers' => $tokenizers, |
@@ -1526,26 +1526,26 @@ discard block |
||
1526 | 1526 | * |
1527 | 1527 | * @return void |
1528 | 1528 | */ |
1529 | - public function setSniffProperty($listenerClass, $name, $value) |
|
1529 | + public function setSniffProperty( $listenerClass, $name, $value ) |
|
1530 | 1530 | { |
1531 | 1531 | // Setting a property for a sniff we are not using. |
1532 | - if (isset($this->listeners[$listenerClass]) === false) { |
|
1532 | + if ( isset( $this->listeners[ $listenerClass ] ) === false ) { |
|
1533 | 1533 | return; |
1534 | 1534 | } |
1535 | 1535 | |
1536 | - $name = trim($name); |
|
1537 | - if (is_string($value) === true) { |
|
1538 | - $value = trim($value); |
|
1536 | + $name = trim( $name ); |
|
1537 | + if ( is_string( $value ) === true ) { |
|
1538 | + $value = trim( $value ); |
|
1539 | 1539 | } |
1540 | 1540 | |
1541 | 1541 | // Special case for booleans. |
1542 | - if ($value === 'true') { |
|
1542 | + if ( $value === 'true' ) { |
|
1543 | 1543 | $value = true; |
1544 | - } else if ($value === 'false') { |
|
1544 | + } else if ( $value === 'false' ) { |
|
1545 | 1545 | $value = false; |
1546 | 1546 | } |
1547 | 1547 | |
1548 | - $this->listeners[$listenerClass]->$name = $value; |
|
1548 | + $this->listeners[ $listenerClass ]->$name = $value; |
|
1549 | 1549 | |
1550 | 1550 | }//end setSniffProperty() |
1551 | 1551 | |
@@ -1563,49 +1563,49 @@ discard block |
||
1563 | 1563 | * @throws Exception If there was an error opening a directory. |
1564 | 1564 | * @see shouldProcessFile() |
1565 | 1565 | */ |
1566 | - public function getFilesToProcess($paths, $local=false) |
|
1566 | + public function getFilesToProcess( $paths, $local = false ) |
|
1567 | 1567 | { |
1568 | 1568 | $files = array(); |
1569 | 1569 | |
1570 | - foreach ($paths as $path) { |
|
1571 | - if (is_dir($path) === true || self::isPharFile($path) === true) { |
|
1572 | - if (self::isPharFile($path) === true) { |
|
1573 | - $path = 'phar://'.$path; |
|
1570 | + foreach ( $paths as $path ) { |
|
1571 | + if ( is_dir( $path ) === true || self::isPharFile( $path ) === true ) { |
|
1572 | + if ( self::isPharFile( $path ) === true ) { |
|
1573 | + $path = 'phar://' . $path; |
|
1574 | 1574 | } |
1575 | 1575 | |
1576 | - if ($local === true) { |
|
1577 | - $di = new DirectoryIterator($path); |
|
1576 | + if ( $local === true ) { |
|
1577 | + $di = new DirectoryIterator( $path ); |
|
1578 | 1578 | } else { |
1579 | 1579 | $di = new RecursiveIteratorIterator( |
1580 | - new RecursiveDirectoryIterator($path), |
|
1580 | + new RecursiveDirectoryIterator( $path ), |
|
1581 | 1581 | 0, |
1582 | 1582 | RecursiveIteratorIterator::CATCH_GET_CHILD |
1583 | 1583 | ); |
1584 | 1584 | } |
1585 | 1585 | |
1586 | - foreach ($di as $file) { |
|
1586 | + foreach ( $di as $file ) { |
|
1587 | 1587 | // Check if the file exists after all symlinks are resolved. |
1588 | - $filePath = self::realpath($file->getPathname()); |
|
1589 | - if ($filePath === false) { |
|
1588 | + $filePath = self::realpath( $file->getPathname() ); |
|
1589 | + if ( $filePath === false ) { |
|
1590 | 1590 | continue; |
1591 | 1591 | } |
1592 | 1592 | |
1593 | - if (is_dir($filePath) === true) { |
|
1593 | + if ( is_dir( $filePath ) === true ) { |
|
1594 | 1594 | continue; |
1595 | 1595 | } |
1596 | 1596 | |
1597 | - if ($this->shouldProcessFile($file->getPathname(), $path) === false) { |
|
1597 | + if ( $this->shouldProcessFile( $file->getPathname(), $path ) === false ) { |
|
1598 | 1598 | continue; |
1599 | 1599 | } |
1600 | 1600 | |
1601 | - $files[] = $file->getPathname(); |
|
1601 | + $files[ ] = $file->getPathname(); |
|
1602 | 1602 | }//end foreach |
1603 | 1603 | } else { |
1604 | - if ($this->shouldIgnoreFile($path, dirname($path)) === true) { |
|
1604 | + if ( $this->shouldIgnoreFile( $path, dirname( $path ) ) === true ) { |
|
1605 | 1605 | continue; |
1606 | 1606 | } |
1607 | 1607 | |
1608 | - $files[] = $path; |
|
1608 | + $files[ ] = $path; |
|
1609 | 1609 | }//end if |
1610 | 1610 | }//end foreach |
1611 | 1611 | |
@@ -1624,33 +1624,33 @@ discard block |
||
1624 | 1624 | * |
1625 | 1625 | * @return bool |
1626 | 1626 | */ |
1627 | - public function shouldProcessFile($path, $basedir) |
|
1627 | + public function shouldProcessFile( $path, $basedir ) |
|
1628 | 1628 | { |
1629 | 1629 | // Check that the file's extension is one we are checking. |
1630 | 1630 | // We are strict about checking the extension and we don't |
1631 | 1631 | // let files through with no extension or that start with a dot. |
1632 | - $fileName = basename($path); |
|
1633 | - $fileParts = explode('.', $fileName); |
|
1634 | - if ($fileParts[0] === $fileName || $fileParts[0] === '') { |
|
1632 | + $fileName = basename( $path ); |
|
1633 | + $fileParts = explode( '.', $fileName ); |
|
1634 | + if ( $fileParts[ 0 ] === $fileName || $fileParts[ 0 ] === '' ) { |
|
1635 | 1635 | return false; |
1636 | 1636 | } |
1637 | 1637 | |
1638 | 1638 | // Checking multi-part file extensions, so need to create a |
1639 | 1639 | // complete extension list and make sure one is allowed. |
1640 | 1640 | $extensions = array(); |
1641 | - array_shift($fileParts); |
|
1642 | - foreach ($fileParts as $part) { |
|
1643 | - $extensions[implode('.', $fileParts)] = 1; |
|
1644 | - array_shift($fileParts); |
|
1641 | + array_shift( $fileParts ); |
|
1642 | + foreach ( $fileParts as $part ) { |
|
1643 | + $extensions[ implode( '.', $fileParts ) ] = 1; |
|
1644 | + array_shift( $fileParts ); |
|
1645 | 1645 | } |
1646 | 1646 | |
1647 | - $matches = array_intersect_key($extensions, $this->allowedFileExtensions); |
|
1648 | - if (empty($matches) === true) { |
|
1647 | + $matches = array_intersect_key( $extensions, $this->allowedFileExtensions ); |
|
1648 | + if ( empty( $matches ) === true ) { |
|
1649 | 1649 | return false; |
1650 | 1650 | } |
1651 | 1651 | |
1652 | 1652 | // If the file's path matches one of our ignore patterns, skip it. |
1653 | - if ($this->shouldIgnoreFile($path, $basedir) === true) { |
|
1653 | + if ( $this->shouldIgnoreFile( $path, $basedir ) === true ) { |
|
1654 | 1654 | return false; |
1655 | 1655 | } |
1656 | 1656 | |
@@ -1667,23 +1667,23 @@ discard block |
||
1667 | 1667 | * |
1668 | 1668 | * @return bool |
1669 | 1669 | */ |
1670 | - public function shouldIgnoreFile($path, $basedir) |
|
1670 | + public function shouldIgnoreFile( $path, $basedir ) |
|
1671 | 1671 | { |
1672 | 1672 | $relativePath = $path; |
1673 | - if (strpos($path, $basedir) === 0) { |
|
1673 | + if ( strpos( $path, $basedir ) === 0 ) { |
|
1674 | 1674 | // The +1 cuts off the directory separator as well. |
1675 | - $relativePath = substr($path, (strlen($basedir) + 1)); |
|
1675 | + $relativePath = substr( $path, ( strlen( $basedir ) + 1 ) ); |
|
1676 | 1676 | } |
1677 | 1677 | |
1678 | - foreach ($this->ignorePatterns as $pattern => $type) { |
|
1679 | - if (is_array($type) === true) { |
|
1678 | + foreach ( $this->ignorePatterns as $pattern => $type ) { |
|
1679 | + if ( is_array( $type ) === true ) { |
|
1680 | 1680 | // A sniff specific ignore pattern. |
1681 | 1681 | continue; |
1682 | 1682 | } |
1683 | 1683 | |
1684 | 1684 | // Maintains backwards compatibility in case the ignore pattern does |
1685 | 1685 | // not have a relative/absolute value. |
1686 | - if (is_int($pattern) === true) { |
|
1686 | + if ( is_int( $pattern ) === true ) { |
|
1687 | 1687 | $pattern = $type; |
1688 | 1688 | $type = 'absolute'; |
1689 | 1689 | } |
@@ -1696,20 +1696,20 @@ discard block |
||
1696 | 1696 | // We assume a / directory separator, as do the exclude rules |
1697 | 1697 | // most developers write, so we need a special case for any system |
1698 | 1698 | // that is different. |
1699 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
1700 | - $replacements['/'] = '\\\\'; |
|
1699 | + if ( DIRECTORY_SEPARATOR === '\\' ) { |
|
1700 | + $replacements[ '/' ] = '\\\\'; |
|
1701 | 1701 | } |
1702 | 1702 | |
1703 | - $pattern = strtr($pattern, $replacements); |
|
1703 | + $pattern = strtr( $pattern, $replacements ); |
|
1704 | 1704 | |
1705 | - if ($type === 'relative') { |
|
1705 | + if ( $type === 'relative' ) { |
|
1706 | 1706 | $testPath = $relativePath; |
1707 | 1707 | } else { |
1708 | 1708 | $testPath = $path; |
1709 | 1709 | } |
1710 | 1710 | |
1711 | - $pattern = '`'.$pattern.'`i'; |
|
1712 | - if (preg_match($pattern, $testPath) === 1) { |
|
1711 | + $pattern = '`' . $pattern . '`i'; |
|
1712 | + if ( preg_match( $pattern, $testPath ) === 1 ) { |
|
1713 | 1713 | return true; |
1714 | 1714 | } |
1715 | 1715 | }//end foreach |
@@ -1734,14 +1734,14 @@ discard block |
||
1734 | 1734 | * @throws PHP_CodeSniffer_Exception If the file could not be processed. |
1735 | 1735 | * @see _processFile() |
1736 | 1736 | */ |
1737 | - public function processFile($file, $contents=null) |
|
1737 | + public function processFile( $file, $contents = null ) |
|
1738 | 1738 | { |
1739 | - if ($contents === null && file_exists($file) === false) { |
|
1740 | - throw new PHP_CodeSniffer_Exception("Source file $file does not exist"); |
|
1739 | + if ( $contents === null && file_exists( $file ) === false ) { |
|
1740 | + throw new PHP_CodeSniffer_Exception( "Source file $file does not exist" ); |
|
1741 | 1741 | } |
1742 | 1742 | |
1743 | - $filePath = self::realpath($file); |
|
1744 | - if ($filePath === false) { |
|
1743 | + $filePath = self::realpath( $file ); |
|
1744 | + if ( $filePath === false ) { |
|
1745 | 1745 | $filePath = $file; |
1746 | 1746 | } |
1747 | 1747 | |
@@ -1749,18 +1749,18 @@ discard block |
||
1749 | 1749 | // to see if there is a tag up top to indicate that the whole |
1750 | 1750 | // file should be ignored. It must be on one of the first two lines. |
1751 | 1751 | $firstContent = $contents; |
1752 | - if ($contents === null && is_readable($filePath) === true) { |
|
1753 | - $handle = fopen($filePath, 'r'); |
|
1754 | - stream_set_blocking($handle, true); |
|
1755 | - if ($handle !== false) { |
|
1756 | - $firstContent = fgets($handle); |
|
1757 | - $firstContent .= fgets($handle); |
|
1758 | - fclose($handle); |
|
1759 | - |
|
1760 | - if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) { |
|
1752 | + if ( $contents === null && is_readable( $filePath ) === true ) { |
|
1753 | + $handle = fopen( $filePath, 'r' ); |
|
1754 | + stream_set_blocking( $handle, true ); |
|
1755 | + if ( $handle !== false ) { |
|
1756 | + $firstContent = fgets( $handle ); |
|
1757 | + $firstContent .= fgets( $handle ); |
|
1758 | + fclose( $handle ); |
|
1759 | + |
|
1760 | + if ( strpos( $firstContent, '@codingStandardsIgnoreFile' ) !== false ) { |
|
1761 | 1761 | // We are ignoring the whole file. |
1762 | - if (PHP_CODESNIFFER_VERBOSITY > 0) { |
|
1763 | - echo 'Ignoring '.basename($filePath).PHP_EOL; |
|
1762 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 ) { |
|
1763 | + echo 'Ignoring ' . basename( $filePath ) . PHP_EOL; |
|
1764 | 1764 | } |
1765 | 1765 | |
1766 | 1766 | return null; |
@@ -1769,29 +1769,29 @@ discard block |
||
1769 | 1769 | }//end if |
1770 | 1770 | |
1771 | 1771 | try { |
1772 | - $phpcsFile = $this->_processFile($file, $contents); |
|
1773 | - } catch (Exception $e) { |
|
1772 | + $phpcsFile = $this->_processFile( $file, $contents ); |
|
1773 | + } catch ( Exception $e ) { |
|
1774 | 1774 | $trace = $e->getTrace(); |
1775 | 1775 | |
1776 | - $filename = $trace[0]['args'][0]; |
|
1777 | - if (is_object($filename) === true |
|
1778 | - && get_class($filename) === 'PHP_CodeSniffer_File' |
|
1776 | + $filename = $trace[ 0 ][ 'args' ][ 0 ]; |
|
1777 | + if ( is_object( $filename ) === true |
|
1778 | + && get_class( $filename ) === 'PHP_CodeSniffer_File' |
|
1779 | 1779 | ) { |
1780 | 1780 | $filename = $filename->getFilename(); |
1781 | - } else if (is_numeric($filename) === true) { |
|
1781 | + } else if ( is_numeric( $filename ) === true ) { |
|
1782 | 1782 | // See if we can find the PHP_CodeSniffer_File object. |
1783 | - foreach ($trace as $data) { |
|
1784 | - if (isset($data['args'][0]) === true |
|
1785 | - && ($data['args'][0] instanceof PHP_CodeSniffer_File) === true |
|
1783 | + foreach ( $trace as $data ) { |
|
1784 | + if ( isset( $data[ 'args' ][ 0 ] ) === true |
|
1785 | + && ( $data[ 'args' ][ 0 ] instanceof PHP_CodeSniffer_File ) === true |
|
1786 | 1786 | ) { |
1787 | - $filename = $data['args'][0]->getFilename(); |
|
1787 | + $filename = $data[ 'args' ][ 0 ]->getFilename(); |
|
1788 | 1788 | } |
1789 | 1789 | } |
1790 | - } else if (is_string($filename) === false) { |
|
1790 | + } else if ( is_string( $filename ) === false ) { |
|
1791 | 1791 | $filename = (string) $filename; |
1792 | 1792 | } |
1793 | 1793 | |
1794 | - $errorMessage = '"'.$e->getMessage().'" at '.$e->getFile().':'.$e->getLine(); |
|
1794 | + $errorMessage = '"' . $e->getMessage() . '" at ' . $e->getFile() . ':' . $e->getLine(); |
|
1795 | 1795 | $error = "An error occurred during processing; checking has been aborted. The error message was: $errorMessage"; |
1796 | 1796 | |
1797 | 1797 | $phpcsFile = new PHP_CodeSniffer_File( |
@@ -1801,14 +1801,14 @@ discard block |
||
1801 | 1801 | $this |
1802 | 1802 | ); |
1803 | 1803 | |
1804 | - $phpcsFile->addError($error, null); |
|
1804 | + $phpcsFile->addError( $error, null ); |
|
1805 | 1805 | }//end try |
1806 | 1806 | |
1807 | 1807 | $cliValues = $this->cli->getCommandLineValues(); |
1808 | 1808 | |
1809 | - if (PHP_CODESNIFFER_INTERACTIVE === false) { |
|
1809 | + if ( PHP_CODESNIFFER_INTERACTIVE === false ) { |
|
1810 | 1810 | // Cache the report data for this file so we can unset it to save memory. |
1811 | - $this->reporting->cacheFileReport($phpcsFile, $cliValues); |
|
1811 | + $this->reporting->cacheFileReport( $phpcsFile, $cliValues ); |
|
1812 | 1812 | $phpcsFile->cleanUp(); |
1813 | 1813 | return $phpcsFile; |
1814 | 1814 | } |
@@ -1821,32 +1821,32 @@ discard block |
||
1821 | 1821 | // Get current violations and then clear the list to make sure |
1822 | 1822 | // we only print violations for a single file each time. |
1823 | 1823 | $numErrors = null; |
1824 | - while ($numErrors !== 0) { |
|
1825 | - $numErrors = ($phpcsFile->getErrorCount() + $phpcsFile->getWarningCount()); |
|
1826 | - if ($numErrors === 0) { |
|
1824 | + while ( $numErrors !== 0 ) { |
|
1825 | + $numErrors = ( $phpcsFile->getErrorCount() + $phpcsFile->getWarningCount() ); |
|
1826 | + if ( $numErrors === 0 ) { |
|
1827 | 1827 | continue; |
1828 | 1828 | } |
1829 | 1829 | |
1830 | - $reportClass = $this->reporting->factory('full'); |
|
1831 | - $reportData = $this->reporting->prepareFileReport($phpcsFile); |
|
1832 | - $reportClass->generateFileReport($reportData, $phpcsFile, $cliValues['showSources'], $cliValues['reportWidth']); |
|
1830 | + $reportClass = $this->reporting->factory( 'full' ); |
|
1831 | + $reportData = $this->reporting->prepareFileReport( $phpcsFile ); |
|
1832 | + $reportClass->generateFileReport( $reportData, $phpcsFile, $cliValues[ 'showSources' ], $cliValues[ 'reportWidth' ] ); |
|
1833 | 1833 | |
1834 | 1834 | echo '<ENTER> to recheck, [s] to skip or [q] to quit : '; |
1835 | - $input = fgets(STDIN); |
|
1836 | - $input = trim($input); |
|
1835 | + $input = fgets( STDIN ); |
|
1836 | + $input = trim( $input ); |
|
1837 | 1837 | |
1838 | - switch ($input) { |
|
1838 | + switch ( $input ) { |
|
1839 | 1839 | case 's': |
1840 | - break(2); |
|
1840 | + break( 2 ); |
|
1841 | 1841 | case 'q': |
1842 | - exit(0); |
|
1842 | + exit( 0 ); |
|
1843 | 1843 | break; |
1844 | 1844 | default: |
1845 | 1845 | // Repopulate the sniffs because some of them save their state |
1846 | 1846 | // and only clear it when the file changes, but we are rechecking |
1847 | 1847 | // the same file. |
1848 | 1848 | $this->populateTokenListeners(); |
1849 | - $phpcsFile = $this->_processFile($file, $contents); |
|
1849 | + $phpcsFile = $this->_processFile( $file, $contents ); |
|
1850 | 1850 | break; |
1851 | 1851 | } |
1852 | 1852 | }//end while |
@@ -1868,18 +1868,18 @@ discard block |
||
1868 | 1868 | * @return PHP_CodeSniffer_File |
1869 | 1869 | * @see processFile() |
1870 | 1870 | */ |
1871 | - private function _processFile($file, $contents) |
|
1871 | + private function _processFile( $file, $contents ) |
|
1872 | 1872 | { |
1873 | 1873 | $stdin = false; |
1874 | 1874 | $cliValues = $this->cli->getCommandLineValues(); |
1875 | - if (empty($cliValues['files']) === true) { |
|
1875 | + if ( empty( $cliValues[ 'files' ] ) === true ) { |
|
1876 | 1876 | $stdin = true; |
1877 | 1877 | } |
1878 | 1878 | |
1879 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1880 | - $startTime = microtime(true); |
|
1881 | - echo 'Processing '.basename($file).' '; |
|
1882 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
1879 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 || ( PHP_CODESNIFFER_CBF === true && $stdin === false ) ) { |
|
1880 | + $startTime = microtime( true ); |
|
1881 | + echo 'Processing ' . basename( $file ) . ' '; |
|
1882 | + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) { |
|
1883 | 1883 | echo PHP_EOL; |
1884 | 1884 | } |
1885 | 1885 | } |
@@ -1891,25 +1891,25 @@ discard block |
||
1891 | 1891 | $this |
1892 | 1892 | ); |
1893 | 1893 | |
1894 | - $phpcsFile->start($contents); |
|
1894 | + $phpcsFile->start( $contents ); |
|
1895 | 1895 | |
1896 | - if (PHP_CODESNIFFER_VERBOSITY > 0 || (PHP_CODESNIFFER_CBF === true && $stdin === false)) { |
|
1897 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
1898 | - if ($timeTaken < 1000) { |
|
1899 | - $timeTaken = round($timeTaken); |
|
1896 | + if ( PHP_CODESNIFFER_VERBOSITY > 0 || ( PHP_CODESNIFFER_CBF === true && $stdin === false ) ) { |
|
1897 | + $timeTaken = ( ( microtime( true ) - $startTime ) * 1000 ); |
|
1898 | + if ( $timeTaken < 1000 ) { |
|
1899 | + $timeTaken = round( $timeTaken ); |
|
1900 | 1900 | echo "DONE in {$timeTaken}ms"; |
1901 | 1901 | } else { |
1902 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
1902 | + $timeTaken = round( ( $timeTaken / 1000 ), 2 ); |
|
1903 | 1903 | echo "DONE in $timeTaken secs"; |
1904 | 1904 | } |
1905 | 1905 | |
1906 | - if (PHP_CODESNIFFER_CBF === true) { |
|
1906 | + if ( PHP_CODESNIFFER_CBF === true ) { |
|
1907 | 1907 | $errors = $phpcsFile->getFixableCount(); |
1908 | - echo " ($errors fixable violations)".PHP_EOL; |
|
1908 | + echo " ($errors fixable violations)" . PHP_EOL; |
|
1909 | 1909 | } else { |
1910 | 1910 | $errors = $phpcsFile->getErrorCount(); |
1911 | 1911 | $warnings = $phpcsFile->getWarningCount(); |
1912 | - echo " ($errors errors, $warnings warnings)".PHP_EOL; |
|
1912 | + echo " ($errors errors, $warnings warnings)" . PHP_EOL; |
|
1913 | 1913 | } |
1914 | 1914 | } |
1915 | 1915 | |
@@ -1927,14 +1927,14 @@ discard block |
||
1927 | 1927 | * |
1928 | 1928 | * @return void |
1929 | 1929 | */ |
1930 | - public function generateDocs($standard, array $sniffs=array(), $generator='Text') |
|
1930 | + public function generateDocs( $standard, array $sniffs = array(), $generator = 'Text' ) |
|
1931 | 1931 | { |
1932 | - if (class_exists('PHP_CodeSniffer_DocGenerators_'.$generator, true) === false) { |
|
1933 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_'.$generator.' not found'); |
|
1932 | + if ( class_exists( 'PHP_CodeSniffer_DocGenerators_' . $generator, true ) === false ) { |
|
1933 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_DocGenerators_' . $generator . ' not found' ); |
|
1934 | 1934 | } |
1935 | 1935 | |
1936 | 1936 | $class = "PHP_CodeSniffer_DocGenerators_$generator"; |
1937 | - $generator = new $class($standard, $sniffs); |
|
1937 | + $generator = new $class( $standard, $sniffs ); |
|
1938 | 1938 | |
1939 | 1939 | $generator->generate(); |
1940 | 1940 | |
@@ -1987,18 +1987,18 @@ discard block |
||
1987 | 1987 | */ |
1988 | 1988 | public static function isCamelCaps( |
1989 | 1989 | $string, |
1990 | - $classFormat=false, |
|
1991 | - $public=true, |
|
1992 | - $strict=true |
|
1990 | + $classFormat = false, |
|
1991 | + $public = true, |
|
1992 | + $strict = true |
|
1993 | 1993 | ) { |
1994 | 1994 | // Check the first character first. |
1995 | - if ($classFormat === false) { |
|
1995 | + if ( $classFormat === false ) { |
|
1996 | 1996 | $legalFirstChar = ''; |
1997 | - if ($public === false) { |
|
1997 | + if ( $public === false ) { |
|
1998 | 1998 | $legalFirstChar = '[_]'; |
1999 | 1999 | } |
2000 | 2000 | |
2001 | - if ($strict === false) { |
|
2001 | + if ( $strict === false ) { |
|
2002 | 2002 | // Can either start with a lowercase letter, or multiple uppercase |
2003 | 2003 | // in a row, representing an acronym. |
2004 | 2004 | $legalFirstChar .= '([A-Z]{2,}|[a-z])'; |
@@ -2009,35 +2009,35 @@ discard block |
||
2009 | 2009 | $legalFirstChar = '[A-Z]'; |
2010 | 2010 | } |
2011 | 2011 | |
2012 | - if (preg_match("/^$legalFirstChar/", $string) === 0) { |
|
2012 | + if ( preg_match( "/^$legalFirstChar/", $string ) === 0 ) { |
|
2013 | 2013 | return false; |
2014 | 2014 | } |
2015 | 2015 | |
2016 | 2016 | // Check that the name only contains legal characters. |
2017 | 2017 | $legalChars = 'a-zA-Z0-9'; |
2018 | - if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) { |
|
2018 | + if ( preg_match( "|[^$legalChars]|", substr( $string, 1 ) ) > 0 ) { |
|
2019 | 2019 | return false; |
2020 | 2020 | } |
2021 | 2021 | |
2022 | - if ($strict === true) { |
|
2022 | + if ( $strict === true ) { |
|
2023 | 2023 | // Check that there are not two capital letters next to each other. |
2024 | - $length = strlen($string); |
|
2024 | + $length = strlen( $string ); |
|
2025 | 2025 | $lastCharWasCaps = $classFormat; |
2026 | 2026 | |
2027 | - for ($i = 1; $i < $length; $i++) { |
|
2028 | - $ascii = ord($string{$i}); |
|
2029 | - if ($ascii >= 48 && $ascii <= 57) { |
|
2027 | + for ( $i = 1; $i < $length; $i++ ) { |
|
2028 | + $ascii = ord( $string{$i}); |
|
2029 | + if ( $ascii >= 48 && $ascii <= 57 ) { |
|
2030 | 2030 | // The character is a number, so it cant be a capital. |
2031 | 2031 | $isCaps = false; |
2032 | 2032 | } else { |
2033 | - if (strtoupper($string{$i}) === $string{$i}) { |
|
2033 | + if ( strtoupper( $string{$i}) === $string{$i}) { |
|
2034 | 2034 | $isCaps = true; |
2035 | 2035 | } else { |
2036 | 2036 | $isCaps = false; |
2037 | 2037 | } |
2038 | 2038 | } |
2039 | 2039 | |
2040 | - if ($isCaps === true && $lastCharWasCaps === true) { |
|
2040 | + if ( $isCaps === true && $lastCharWasCaps === true ) { |
|
2041 | 2041 | return false; |
2042 | 2042 | } |
2043 | 2043 | |
@@ -2057,26 +2057,26 @@ discard block |
||
2057 | 2057 | * |
2058 | 2058 | * @return boolean |
2059 | 2059 | */ |
2060 | - public static function isUnderscoreName($string) |
|
2060 | + public static function isUnderscoreName( $string ) |
|
2061 | 2061 | { |
2062 | 2062 | // If there are space in the name, it can't be valid. |
2063 | - if (strpos($string, ' ') !== false) { |
|
2063 | + if ( strpos( $string, ' ' ) !== false ) { |
|
2064 | 2064 | return false; |
2065 | 2065 | } |
2066 | 2066 | |
2067 | 2067 | $validName = true; |
2068 | - $nameBits = explode('_', $string); |
|
2068 | + $nameBits = explode( '_', $string ); |
|
2069 | 2069 | |
2070 | - if (preg_match('|^[A-Z]|', $string) === 0) { |
|
2070 | + if ( preg_match( '|^[A-Z]|', $string ) === 0 ) { |
|
2071 | 2071 | // Name does not begin with a capital letter. |
2072 | 2072 | $validName = false; |
2073 | 2073 | } else { |
2074 | - foreach ($nameBits as $bit) { |
|
2075 | - if ($bit === '') { |
|
2074 | + foreach ( $nameBits as $bit ) { |
|
2075 | + if ( $bit === '' ) { |
|
2076 | 2076 | continue; |
2077 | 2077 | } |
2078 | 2078 | |
2079 | - if ($bit{0} !== strtoupper($bit{0})) { |
|
2079 | + if ( $bit{0} !== strtoupper( $bit{0}) ) { |
|
2080 | 2080 | $validName = false; |
2081 | 2081 | break; |
2082 | 2082 | } |
@@ -2098,17 +2098,17 @@ discard block |
||
2098 | 2098 | * |
2099 | 2099 | * @return string |
2100 | 2100 | */ |
2101 | - public static function suggestType($varType) |
|
2101 | + public static function suggestType( $varType ) |
|
2102 | 2102 | { |
2103 | - if ($varType === '') { |
|
2103 | + if ( $varType === '' ) { |
|
2104 | 2104 | return ''; |
2105 | 2105 | } |
2106 | 2106 | |
2107 | - if (in_array($varType, self::$allowedTypes) === true) { |
|
2107 | + if ( in_array( $varType, self::$allowedTypes ) === true ) { |
|
2108 | 2108 | return $varType; |
2109 | 2109 | } else { |
2110 | - $lowerVarType = strtolower($varType); |
|
2111 | - switch ($lowerVarType) { |
|
2110 | + $lowerVarType = strtolower( $varType ); |
|
2111 | + switch ( $lowerVarType ) { |
|
2112 | 2112 | case 'bool': |
2113 | 2113 | case 'boolean': |
2114 | 2114 | return 'boolean'; |
@@ -2124,33 +2124,33 @@ discard block |
||
2124 | 2124 | return 'array'; |
2125 | 2125 | }//end switch |
2126 | 2126 | |
2127 | - if (strpos($lowerVarType, 'array(') !== false) { |
|
2127 | + if ( strpos( $lowerVarType, 'array(' ) !== false ) { |
|
2128 | 2128 | // Valid array declaration: |
2129 | 2129 | // array, array(type), array(type1 => type2). |
2130 | 2130 | $matches = array(); |
2131 | 2131 | $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; |
2132 | - if (preg_match($pattern, $varType, $matches) !== 0) { |
|
2132 | + if ( preg_match( $pattern, $varType, $matches ) !== 0 ) { |
|
2133 | 2133 | $type1 = ''; |
2134 | - if (isset($matches[1]) === true) { |
|
2135 | - $type1 = $matches[1]; |
|
2134 | + if ( isset( $matches[ 1 ] ) === true ) { |
|
2135 | + $type1 = $matches[ 1 ]; |
|
2136 | 2136 | } |
2137 | 2137 | |
2138 | 2138 | $type2 = ''; |
2139 | - if (isset($matches[3]) === true) { |
|
2140 | - $type2 = $matches[3]; |
|
2139 | + if ( isset( $matches[ 3 ] ) === true ) { |
|
2140 | + $type2 = $matches[ 3 ]; |
|
2141 | 2141 | } |
2142 | 2142 | |
2143 | - $type1 = self::suggestType($type1); |
|
2144 | - $type2 = self::suggestType($type2); |
|
2145 | - if ($type2 !== '') { |
|
2146 | - $type2 = ' => '.$type2; |
|
2143 | + $type1 = self::suggestType( $type1 ); |
|
2144 | + $type2 = self::suggestType( $type2 ); |
|
2145 | + if ( $type2 !== '' ) { |
|
2146 | + $type2 = ' => ' . $type2; |
|
2147 | 2147 | } |
2148 | 2148 | |
2149 | 2149 | return "array($type1$type2)"; |
2150 | 2150 | } else { |
2151 | 2151 | return 'array'; |
2152 | 2152 | }//end if |
2153 | - } else if (in_array($lowerVarType, self::$allowedTypes) === true) { |
|
2153 | + } else if ( in_array( $lowerVarType, self::$allowedTypes ) === true ) { |
|
2154 | 2154 | // A valid type, but not lower cased. |
2155 | 2155 | return $lowerVarType; |
2156 | 2156 | } else { |
@@ -2172,17 +2172,17 @@ discard block |
||
2172 | 2172 | * |
2173 | 2173 | * @return string |
2174 | 2174 | */ |
2175 | - public static function prepareForOutput($content) |
|
2175 | + public static function prepareForOutput( $content ) |
|
2176 | 2176 | { |
2177 | - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
2178 | - $content = str_replace("\r", '\r', $content); |
|
2179 | - $content = str_replace("\n", '\n', $content); |
|
2180 | - $content = str_replace("\t", '\t', $content); |
|
2177 | + if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) { |
|
2178 | + $content = str_replace( "\r", '\r', $content ); |
|
2179 | + $content = str_replace( "\n", '\n', $content ); |
|
2180 | + $content = str_replace( "\t", '\t', $content ); |
|
2181 | 2181 | } else { |
2182 | - $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content); |
|
2183 | - $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content); |
|
2184 | - $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content); |
|
2185 | - $content = str_replace(' ', "\033[30;1m·\033[0m", $content); |
|
2182 | + $content = str_replace( "\r", "\033[30;1m\\r\033[0m", $content ); |
|
2183 | + $content = str_replace( "\n", "\033[30;1m\\n\033[0m", $content ); |
|
2184 | + $content = str_replace( "\t", "\033[30;1m\\t\033[0m", $content ); |
|
2185 | + $content = str_replace( ' ', "\033[30;1m·\033[0m", $content ); |
|
2186 | 2186 | } |
2187 | 2187 | |
2188 | 2188 | return $content; |
@@ -2197,19 +2197,19 @@ discard block |
||
2197 | 2197 | */ |
2198 | 2198 | public static function getInstalledStandardPaths() |
2199 | 2199 | { |
2200 | - $installedPaths = array(dirname(__FILE__).DIRECTORY_SEPARATOR.'CodeSniffer'.DIRECTORY_SEPARATOR.'Standards'); |
|
2201 | - $configPaths = PHP_CodeSniffer::getConfigData('installed_paths'); |
|
2202 | - if ($configPaths !== null) { |
|
2203 | - $installedPaths = array_merge($installedPaths, explode(',', $configPaths)); |
|
2200 | + $installedPaths = array( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'CodeSniffer' . DIRECTORY_SEPARATOR . 'Standards' ); |
|
2201 | + $configPaths = PHP_CodeSniffer::getConfigData( 'installed_paths' ); |
|
2202 | + if ( $configPaths !== null ) { |
|
2203 | + $installedPaths = array_merge( $installedPaths, explode( ',', $configPaths ) ); |
|
2204 | 2204 | } |
2205 | 2205 | |
2206 | 2206 | $resolvedInstalledPaths = array(); |
2207 | - foreach ($installedPaths as $installedPath) { |
|
2208 | - if (substr($installedPath, 0, 1) === '.') { |
|
2209 | - $installedPath = dirname(__FILE__).DIRECTORY_SEPARATOR.$installedPath; |
|
2207 | + foreach ( $installedPaths as $installedPath ) { |
|
2208 | + if ( substr( $installedPath, 0, 1 ) === '.' ) { |
|
2209 | + $installedPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $installedPath; |
|
2210 | 2210 | } |
2211 | 2211 | |
2212 | - $resolvedInstalledPaths[] = $installedPath; |
|
2212 | + $resolvedInstalledPaths[ ] = $installedPath; |
|
2213 | 2213 | } |
2214 | 2214 | |
2215 | 2215 | return $resolvedInstalledPaths; |
@@ -2235,32 +2235,32 @@ discard block |
||
2235 | 2235 | * @see isInstalledStandard() |
2236 | 2236 | */ |
2237 | 2237 | public static function getInstalledStandards( |
2238 | - $includeGeneric=false, |
|
2239 | - $standardsDir='' |
|
2238 | + $includeGeneric = false, |
|
2239 | + $standardsDir = '' |
|
2240 | 2240 | ) { |
2241 | 2241 | $installedStandards = array(); |
2242 | 2242 | |
2243 | - if ($standardsDir === '') { |
|
2243 | + if ( $standardsDir === '' ) { |
|
2244 | 2244 | $installedPaths = self::getInstalledStandardPaths(); |
2245 | 2245 | } else { |
2246 | - $installedPaths = array($standardsDir); |
|
2246 | + $installedPaths = array( $standardsDir ); |
|
2247 | 2247 | } |
2248 | 2248 | |
2249 | - foreach ($installedPaths as $standardsDir) { |
|
2250 | - $di = new DirectoryIterator($standardsDir); |
|
2251 | - foreach ($di as $file) { |
|
2252 | - if ($file->isDir() === true && $file->isDot() === false) { |
|
2249 | + foreach ( $installedPaths as $standardsDir ) { |
|
2250 | + $di = new DirectoryIterator( $standardsDir ); |
|
2251 | + foreach ( $di as $file ) { |
|
2252 | + if ( $file->isDir() === true && $file->isDot() === false ) { |
|
2253 | 2253 | $filename = $file->getFilename(); |
2254 | 2254 | |
2255 | 2255 | // Ignore the special "Generic" standard. |
2256 | - if ($includeGeneric === false && $filename === 'Generic') { |
|
2256 | + if ( $includeGeneric === false && $filename === 'Generic' ) { |
|
2257 | 2257 | continue; |
2258 | 2258 | } |
2259 | 2259 | |
2260 | 2260 | // Valid coding standard dirs include a ruleset. |
2261 | - $csFile = $file->getPathname().'/ruleset.xml'; |
|
2262 | - if (is_file($csFile) === true) { |
|
2263 | - $installedStandards[] = $filename; |
|
2261 | + $csFile = $file->getPathname() . '/ruleset.xml'; |
|
2262 | + if ( is_file( $csFile ) === true ) { |
|
2263 | + $installedStandards[ ] = $filename; |
|
2264 | 2264 | } |
2265 | 2265 | } |
2266 | 2266 | } |
@@ -2283,29 +2283,29 @@ discard block |
||
2283 | 2283 | * @return boolean |
2284 | 2284 | * @see getInstalledStandards() |
2285 | 2285 | */ |
2286 | - public static function isInstalledStandard($standard) |
|
2286 | + public static function isInstalledStandard( $standard ) |
|
2287 | 2287 | { |
2288 | - $path = self::getInstalledStandardPath($standard); |
|
2289 | - if ($path !== null && strpos($path, 'ruleset.xml') !== false) { |
|
2288 | + $path = self::getInstalledStandardPath( $standard ); |
|
2289 | + if ( $path !== null && strpos( $path, 'ruleset.xml' ) !== false ) { |
|
2290 | 2290 | return true; |
2291 | 2291 | } else { |
2292 | 2292 | // This could be a custom standard, installed outside our |
2293 | 2293 | // standards directory. |
2294 | - $standard = self::realPath($standard); |
|
2294 | + $standard = self::realPath( $standard ); |
|
2295 | 2295 | |
2296 | 2296 | // Might be an actual ruleset file itself. |
2297 | 2297 | // If it has an XML extension, let's at least try it. |
2298 | - if (is_file($standard) === true |
|
2299 | - && (substr(strtolower($standard), -4) === '.xml' |
|
2300 | - || substr(strtolower($standard), -9) === '.xml.dist') |
|
2298 | + if ( is_file( $standard ) === true |
|
2299 | + && ( substr( strtolower( $standard ), -4 ) === '.xml' |
|
2300 | + || substr( strtolower( $standard ), -9 ) === '.xml.dist' ) |
|
2301 | 2301 | ) { |
2302 | 2302 | return true; |
2303 | 2303 | } |
2304 | 2304 | |
2305 | 2305 | // If it is a directory with a ruleset.xml file in it, |
2306 | 2306 | // it is a standard. |
2307 | - $ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
2308 | - if (is_file($ruleset) === true) { |
|
2307 | + $ruleset = rtrim( $standard, ' /\\' ) . DIRECTORY_SEPARATOR . 'ruleset.xml'; |
|
2308 | + if ( is_file( $ruleset ) === true ) { |
|
2309 | 2309 | return true; |
2310 | 2310 | } |
2311 | 2311 | }//end if |
@@ -2326,17 +2326,17 @@ discard block |
||
2326 | 2326 | * |
2327 | 2327 | * @return string|null |
2328 | 2328 | */ |
2329 | - public static function getInstalledStandardPath($standard) |
|
2329 | + public static function getInstalledStandardPath( $standard ) |
|
2330 | 2330 | { |
2331 | 2331 | $installedPaths = self::getInstalledStandardPaths(); |
2332 | - foreach ($installedPaths as $installedPath) { |
|
2333 | - $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard; |
|
2334 | - $path = self::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
2335 | - if (is_file($path) === true) { |
|
2332 | + foreach ( $installedPaths as $installedPath ) { |
|
2333 | + $standardPath = $installedPath . DIRECTORY_SEPARATOR . $standard; |
|
2334 | + $path = self::realpath( $standardPath . DIRECTORY_SEPARATOR . 'ruleset.xml' ); |
|
2335 | + if ( is_file( $path ) === true ) { |
|
2336 | 2336 | return $path; |
2337 | - } else if (self::isPharFile($standardPath) === true) { |
|
2338 | - $path = self::realpath($standardPath); |
|
2339 | - if ($path !== false) { |
|
2337 | + } else if ( self::isPharFile( $standardPath ) === true ) { |
|
2338 | + $path = self::realpath( $standardPath ); |
|
2339 | + if ( $path !== false ) { |
|
2340 | 2340 | return $path; |
2341 | 2341 | } |
2342 | 2342 | } |
@@ -2359,19 +2359,19 @@ discard block |
||
2359 | 2359 | * @see setConfigData() |
2360 | 2360 | * @see getAllConfigData() |
2361 | 2361 | */ |
2362 | - public static function getConfigData($key) |
|
2362 | + public static function getConfigData( $key ) |
|
2363 | 2363 | { |
2364 | 2364 | $phpCodeSnifferConfig = self::getAllConfigData(); |
2365 | 2365 | |
2366 | - if ($phpCodeSnifferConfig === null) { |
|
2366 | + if ( $phpCodeSnifferConfig === null ) { |
|
2367 | 2367 | return null; |
2368 | 2368 | } |
2369 | 2369 | |
2370 | - if (isset($phpCodeSnifferConfig[$key]) === false) { |
|
2370 | + if ( isset( $phpCodeSnifferConfig[ $key ] ) === false ) { |
|
2371 | 2371 | return null; |
2372 | 2372 | } |
2373 | 2373 | |
2374 | - return $phpCodeSnifferConfig[$key]; |
|
2374 | + return $phpCodeSnifferConfig[ $key ]; |
|
2375 | 2375 | |
2376 | 2376 | }//end getConfigData() |
2377 | 2377 | |
@@ -2394,20 +2394,20 @@ discard block |
||
2394 | 2394 | * @see getConfigData() |
2395 | 2395 | * @throws PHP_CodeSniffer_Exception If the config file can not be written. |
2396 | 2396 | */ |
2397 | - public static function setConfigData($key, $value, $temp=false) |
|
2397 | + public static function setConfigData( $key, $value, $temp = false ) |
|
2398 | 2398 | { |
2399 | - if ($temp === false) { |
|
2399 | + if ( $temp === false ) { |
|
2400 | 2400 | $path = ''; |
2401 | - if (is_callable('Phar::running') === true) { |
|
2402 | - $path = Phar::running(false); |
|
2401 | + if ( is_callable( 'Phar::running' ) === true ) { |
|
2402 | + $path = Phar::running( false ); |
|
2403 | 2403 | } |
2404 | 2404 | |
2405 | - if ($path !== '') { |
|
2406 | - $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2405 | + if ( $path !== '' ) { |
|
2406 | + $configFile = dirname( $path ) . '/CodeSniffer.conf'; |
|
2407 | 2407 | } else { |
2408 | - $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2409 | - if (is_file($configFile) === false |
|
2410 | - && strpos('@data_dir@', '@data_dir') === false |
|
2408 | + $configFile = dirname( __FILE__ ) . '/CodeSniffer.conf'; |
|
2409 | + if ( is_file( $configFile ) === false |
|
2410 | + && strpos( '@data_dir@', '@data_dir' ) === false |
|
2411 | 2411 | ) { |
2412 | 2412 | // If data_dir was replaced, this is a PEAR install and we can |
2413 | 2413 | // use the PEAR data dir to store the conf file. |
@@ -2415,35 +2415,35 @@ discard block |
||
2415 | 2415 | } |
2416 | 2416 | } |
2417 | 2417 | |
2418 | - if (is_file($configFile) === true |
|
2419 | - && is_writable($configFile) === false |
|
2418 | + if ( is_file( $configFile ) === true |
|
2419 | + && is_writable( $configFile ) === false |
|
2420 | 2420 | ) { |
2421 | - $error = 'Config file '.$configFile.' is not writable'; |
|
2422 | - throw new PHP_CodeSniffer_Exception($error); |
|
2421 | + $error = 'Config file ' . $configFile . ' is not writable'; |
|
2422 | + throw new PHP_CodeSniffer_Exception( $error ); |
|
2423 | 2423 | } |
2424 | 2424 | }//end if |
2425 | 2425 | |
2426 | 2426 | $phpCodeSnifferConfig = self::getAllConfigData(); |
2427 | 2427 | |
2428 | - if ($value === null) { |
|
2429 | - if (isset($phpCodeSnifferConfig[$key]) === true) { |
|
2430 | - unset($phpCodeSnifferConfig[$key]); |
|
2428 | + if ( $value === null ) { |
|
2429 | + if ( isset( $phpCodeSnifferConfig[ $key ] ) === true ) { |
|
2430 | + unset( $phpCodeSnifferConfig[ $key ] ); |
|
2431 | 2431 | } |
2432 | 2432 | } else { |
2433 | - $phpCodeSnifferConfig[$key] = $value; |
|
2433 | + $phpCodeSnifferConfig[ $key ] = $value; |
|
2434 | 2434 | } |
2435 | 2435 | |
2436 | - if ($temp === false) { |
|
2437 | - $output = '<'.'?php'."\n".' $phpCodeSnifferConfig = '; |
|
2438 | - $output .= var_export($phpCodeSnifferConfig, true); |
|
2439 | - $output .= "\n?".'>'; |
|
2436 | + if ( $temp === false ) { |
|
2437 | + $output = '<' . '?php' . "\n" . ' $phpCodeSnifferConfig = '; |
|
2438 | + $output .= var_export( $phpCodeSnifferConfig, true ); |
|
2439 | + $output .= "\n?" . '>'; |
|
2440 | 2440 | |
2441 | - if (file_put_contents($configFile, $output) === false) { |
|
2441 | + if ( file_put_contents( $configFile, $output ) === false ) { |
|
2442 | 2442 | return false; |
2443 | 2443 | } |
2444 | 2444 | } |
2445 | 2445 | |
2446 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2446 | + $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ] = $phpCodeSnifferConfig; |
|
2447 | 2447 | |
2448 | 2448 | return true; |
2449 | 2449 | |
@@ -2458,32 +2458,32 @@ discard block |
||
2458 | 2458 | */ |
2459 | 2459 | public static function getAllConfigData() |
2460 | 2460 | { |
2461 | - if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) { |
|
2462 | - return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2461 | + if ( isset( $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ] ) === true ) { |
|
2462 | + return $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ]; |
|
2463 | 2463 | } |
2464 | 2464 | |
2465 | 2465 | $path = ''; |
2466 | - if (is_callable('Phar::running') === true) { |
|
2467 | - $path = Phar::running(false); |
|
2466 | + if ( is_callable( 'Phar::running' ) === true ) { |
|
2467 | + $path = Phar::running( false ); |
|
2468 | 2468 | } |
2469 | 2469 | |
2470 | - if ($path !== '') { |
|
2471 | - $configFile = dirname($path).'/CodeSniffer.conf'; |
|
2470 | + if ( $path !== '' ) { |
|
2471 | + $configFile = dirname( $path ) . '/CodeSniffer.conf'; |
|
2472 | 2472 | } else { |
2473 | - $configFile = dirname(__FILE__).'/CodeSniffer.conf'; |
|
2474 | - if (is_file($configFile) === false) { |
|
2473 | + $configFile = dirname( __FILE__ ) . '/CodeSniffer.conf'; |
|
2474 | + if ( is_file( $configFile ) === false ) { |
|
2475 | 2475 | $configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf'; |
2476 | 2476 | } |
2477 | 2477 | } |
2478 | 2478 | |
2479 | - if (is_file($configFile) === false) { |
|
2480 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = array(); |
|
2479 | + if ( is_file( $configFile ) === false ) { |
|
2480 | + $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ] = array(); |
|
2481 | 2481 | return array(); |
2482 | 2482 | } |
2483 | 2483 | |
2484 | 2484 | include $configFile; |
2485 | - $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig; |
|
2486 | - return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
|
2485 | + $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ] = $phpCodeSnifferConfig; |
|
2486 | + return $GLOBALS[ 'PHP_CODESNIFFER_CONFIG_DATA' ]; |
|
2487 | 2487 | |
2488 | 2488 | }//end getAllConfigData() |
2489 | 2489 | |
@@ -2495,9 +2495,9 @@ discard block |
||
2495 | 2495 | * |
2496 | 2496 | * @return mixed |
2497 | 2497 | */ |
2498 | - public static function isPharFile($path) |
|
2498 | + public static function isPharFile( $path ) |
|
2499 | 2499 | { |
2500 | - if (strpos($path, 'phar://') === 0) { |
|
2500 | + if ( strpos( $path, 'phar://' ) === 0 ) { |
|
2501 | 2501 | return true; |
2502 | 2502 | } |
2503 | 2503 | |
@@ -2515,37 +2515,37 @@ discard block |
||
2515 | 2515 | * |
2516 | 2516 | * @return mixed |
2517 | 2517 | */ |
2518 | - public static function realpath($path) |
|
2518 | + public static function realpath( $path ) |
|
2519 | 2519 | { |
2520 | 2520 | // Support the path replacement of ~ with the user's home directory. |
2521 | - if (substr($path, 0, 2) === '~/') { |
|
2522 | - $homeDir = getenv('HOME'); |
|
2523 | - if ($homeDir !== false) { |
|
2524 | - $path = $homeDir.substr($path, 1); |
|
2521 | + if ( substr( $path, 0, 2 ) === '~/' ) { |
|
2522 | + $homeDir = getenv( 'HOME' ); |
|
2523 | + if ( $homeDir !== false ) { |
|
2524 | + $path = $homeDir . substr( $path, 1 ); |
|
2525 | 2525 | } |
2526 | 2526 | } |
2527 | 2527 | |
2528 | 2528 | // No extra work needed if this is not a phar file. |
2529 | - if (self::isPharFile($path) === false) { |
|
2530 | - return realpath($path); |
|
2529 | + if ( self::isPharFile( $path ) === false ) { |
|
2530 | + return realpath( $path ); |
|
2531 | 2531 | } |
2532 | 2532 | |
2533 | 2533 | // Before trying to break down the file path, |
2534 | 2534 | // check if it exists first because it will mostly not |
2535 | 2535 | // change after running the below code. |
2536 | - if (file_exists($path) === true) { |
|
2536 | + if ( file_exists( $path ) === true ) { |
|
2537 | 2537 | return $path; |
2538 | 2538 | } |
2539 | 2539 | |
2540 | - $phar = Phar::running(false); |
|
2541 | - $extra = str_replace('phar://'.$phar, '', $path); |
|
2542 | - $path = realpath($phar); |
|
2543 | - if ($path === false) { |
|
2540 | + $phar = Phar::running( false ); |
|
2541 | + $extra = str_replace( 'phar://' . $phar, '', $path ); |
|
2542 | + $path = realpath( $phar ); |
|
2543 | + if ( $path === false ) { |
|
2544 | 2544 | return false; |
2545 | 2545 | } |
2546 | 2546 | |
2547 | - $path = 'phar://'.$path.$extra; |
|
2548 | - if (file_exists($path) === true) { |
|
2547 | + $path = 'phar://' . $path . $extra; |
|
2548 | + if ( file_exists( $path ) === true ) { |
|
2549 | 2549 | return $path; |
2550 | 2550 | } |
2551 | 2551 | |
@@ -2563,13 +2563,13 @@ discard block |
||
2563 | 2563 | * |
2564 | 2564 | * @return void |
2565 | 2565 | */ |
2566 | - public static function chdir($path) |
|
2566 | + public static function chdir( $path ) |
|
2567 | 2567 | { |
2568 | - if (self::isPharFile($path) === true) { |
|
2569 | - $phar = Phar::running(false); |
|
2570 | - chdir(dirname($phar)); |
|
2568 | + if ( self::isPharFile( $path ) === true ) { |
|
2569 | + $phar = Phar::running( false ); |
|
2570 | + chdir( dirname( $phar ) ); |
|
2571 | 2571 | } else { |
2572 | - chdir($path); |
|
2572 | + chdir( $path ); |
|
2573 | 2573 | } |
2574 | 2574 | |
2575 | 2575 | }//end chdir() |
@@ -65,8 +65,7 @@ discard block |
||
65 | 65 | * @version Release: @package_version@ |
66 | 66 | * @link http://pear.php.net/package/PHP_CodeSniffer |
67 | 67 | */ |
68 | -class PHP_CodeSniffer |
|
69 | -{ |
|
68 | +class PHP_CodeSniffer { |
|
70 | 69 | |
71 | 70 | /** |
72 | 71 | * The current version. |
@@ -277,8 +276,7 @@ discard block |
||
277 | 276 | * |
278 | 277 | * @return void |
279 | 278 | */ |
280 | - public static function autoload($className) |
|
281 | - { |
|
279 | + public static function autoload($className) { |
|
282 | 280 | if (substr($className, 0, 4) === 'PHP_') { |
283 | 281 | $newClassName = substr($className, 4); |
284 | 282 | } else { |
@@ -326,8 +324,7 @@ discard block |
||
326 | 324 | * |
327 | 325 | * @return void |
328 | 326 | */ |
329 | - public function setVerbosity($verbosity) |
|
330 | - { |
|
327 | + public function setVerbosity($verbosity) { |
|
331 | 328 | if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { |
332 | 329 | define('PHP_CODESNIFFER_VERBOSITY', $verbosity); |
333 | 330 | } |
@@ -344,8 +341,7 @@ discard block |
||
344 | 341 | * |
345 | 342 | * @return void |
346 | 343 | */ |
347 | - public function setTabWidth($tabWidth) |
|
348 | - { |
|
344 | + public function setTabWidth($tabWidth) { |
|
349 | 345 | if (defined('PHP_CODESNIFFER_TAB_WIDTH') === false) { |
350 | 346 | define('PHP_CODESNIFFER_TAB_WIDTH', $tabWidth); |
351 | 347 | } |
@@ -363,8 +359,7 @@ discard block |
||
363 | 359 | * |
364 | 360 | * @return void |
365 | 361 | */ |
366 | - public function setEncoding($encoding) |
|
367 | - { |
|
362 | + public function setEncoding($encoding) { |
|
368 | 363 | if (defined('PHP_CODESNIFFER_ENCODING') === false) { |
369 | 364 | define('PHP_CODESNIFFER_ENCODING', $encoding); |
370 | 365 | } |
@@ -380,8 +375,7 @@ discard block |
||
380 | 375 | * |
381 | 376 | * @return void |
382 | 377 | */ |
383 | - public function setInteractive($interactive) |
|
384 | - { |
|
378 | + public function setInteractive($interactive) { |
|
385 | 379 | if (defined('PHP_CODESNIFFER_INTERACTIVE') === false) { |
386 | 380 | define('PHP_CODESNIFFER_INTERACTIVE', $interactive); |
387 | 381 | } |
@@ -400,8 +394,7 @@ discard block |
||
400 | 394 | * |
401 | 395 | * @return void |
402 | 396 | */ |
403 | - public function setAllowedFileExtensions(array $extensions) |
|
404 | - { |
|
397 | + public function setAllowedFileExtensions(array $extensions) { |
|
405 | 398 | $newExtensions = array(); |
406 | 399 | foreach ($extensions as $ext) { |
407 | 400 | $slash = strpos($ext, '/'); |
@@ -438,8 +431,7 @@ discard block |
||
438 | 431 | * |
439 | 432 | * @return void |
440 | 433 | */ |
441 | - public function setIgnorePatterns(array $patterns) |
|
442 | - { |
|
434 | + public function setIgnorePatterns(array $patterns) { |
|
443 | 435 | $this->ignorePatterns = $patterns; |
444 | 436 | |
445 | 437 | }//end setIgnorePatterns() |
@@ -456,8 +448,7 @@ discard block |
||
456 | 448 | * |
457 | 449 | * @return array |
458 | 450 | */ |
459 | - public function getIgnorePatterns($listener=null) |
|
460 | - { |
|
451 | + public function getIgnorePatterns($listener=null) { |
|
461 | 452 | if ($listener === null) { |
462 | 453 | return $this->ignorePatterns; |
463 | 454 | } |
@@ -478,8 +469,7 @@ discard block |
||
478 | 469 | * |
479 | 470 | * @return void |
480 | 471 | */ |
481 | - public function setCli($cli) |
|
482 | - { |
|
472 | + public function setCli($cli) { |
|
483 | 473 | $this->cli = $cli; |
484 | 474 | |
485 | 475 | }//end setCli() |
@@ -499,8 +489,7 @@ discard block |
||
499 | 489 | * |
500 | 490 | * @return void |
501 | 491 | */ |
502 | - public function process($files, $standards, array $restrictions=array(), $local=false) |
|
503 | - { |
|
492 | + public function process($files, $standards, array $restrictions=array(), $local=false) { |
|
504 | 493 | $files = (array) $files; |
505 | 494 | $this->initStandard($standards, $restrictions); |
506 | 495 | $this->processFiles($files, $local); |
@@ -518,8 +507,7 @@ discard block |
||
518 | 507 | * |
519 | 508 | * @return void |
520 | 509 | */ |
521 | - public function initStandard($standards, array $restrictions=array(), array $exclusions=array()) |
|
522 | - { |
|
510 | + public function initStandard($standards, array $restrictions=array(), array $exclusions=array()) { |
|
523 | 511 | $standards = (array) $standards; |
524 | 512 | |
525 | 513 | // Reset the members. |
@@ -613,8 +601,7 @@ discard block |
||
613 | 601 | * @return void |
614 | 602 | * @throws PHP_CodeSniffer_Exception If files are invalid. |
615 | 603 | */ |
616 | - public function processFiles($files, $local=false) |
|
617 | - { |
|
604 | + public function processFiles($files, $local=false) { |
|
618 | 605 | $files = (array) $files; |
619 | 606 | $cliValues = $this->cli->getCommandLineValues(); |
620 | 607 | $showProgress = $cliValues['showProgress']; |
@@ -720,8 +707,7 @@ discard block |
||
720 | 707 | * @return array |
721 | 708 | * @throws PHP_CodeSniffer_Exception If the ruleset path is invalid. |
722 | 709 | */ |
723 | - public function processRuleset($rulesetPath, $depth=0) |
|
724 | - { |
|
710 | + public function processRuleset($rulesetPath, $depth=0) { |
|
725 | 711 | $rulesetPath = self::realpath($rulesetPath); |
726 | 712 | if (PHP_CODESNIFFER_VERBOSITY > 1) { |
727 | 713 | echo str_repeat("\t", $depth); |
@@ -945,8 +931,7 @@ discard block |
||
945 | 931 | * |
946 | 932 | * @return array |
947 | 933 | */ |
948 | - private function _expandSniffDirectory($directory, $depth=0) |
|
949 | - { |
|
934 | + private function _expandSniffDirectory($directory, $depth=0) { |
|
950 | 935 | $sniffs = array(); |
951 | 936 | |
952 | 937 | if (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') === true) { |
@@ -1013,8 +998,7 @@ discard block |
||
1013 | 998 | * @return array |
1014 | 999 | * @throws PHP_CodeSniffer_Exception If the reference is invalid. |
1015 | 1000 | */ |
1016 | - private function _expandRulesetReference($ref, $rulesetDir, $depth=0) |
|
1017 | - { |
|
1001 | + private function _expandRulesetReference($ref, $rulesetDir, $depth=0) { |
|
1018 | 1002 | // Ignore internal sniffs codes as they are used to only |
1019 | 1003 | // hide and change internal messages. |
1020 | 1004 | if (substr($ref, 0, 9) === 'Internal.') { |
@@ -1193,8 +1177,7 @@ discard block |
||
1193 | 1177 | * |
1194 | 1178 | * @return void |
1195 | 1179 | */ |
1196 | - private function _processRule($rule, $depth=0) |
|
1197 | - { |
|
1180 | + private function _processRule($rule, $depth=0) { |
|
1198 | 1181 | $code = (string) $rule['ref']; |
1199 | 1182 | |
1200 | 1183 | // Custom severity. |
@@ -1324,8 +1307,7 @@ discard block |
||
1324 | 1307 | * |
1325 | 1308 | * @return bool |
1326 | 1309 | */ |
1327 | - private function _shouldProcessElement($element, $depth=0) |
|
1328 | - { |
|
1310 | + private function _shouldProcessElement($element, $depth=0) { |
|
1329 | 1311 | if (isset($element['phpcbf-only']) === false |
1330 | 1312 | && isset($element['phpcs-only']) === false |
1331 | 1313 | ) { |
@@ -1364,8 +1346,7 @@ discard block |
||
1364 | 1346 | * @return void |
1365 | 1347 | * @throws PHP_CodeSniffer_Exception If a sniff file path is invalid. |
1366 | 1348 | */ |
1367 | - public function registerSniffs($files, $restrictions, $exclusions) |
|
1368 | - { |
|
1349 | + public function registerSniffs($files, $restrictions, $exclusions) { |
|
1369 | 1350 | $listeners = array(); |
1370 | 1351 | |
1371 | 1352 | foreach ($files as $file) { |
@@ -1440,8 +1421,7 @@ discard block |
||
1440 | 1421 | * @return void |
1441 | 1422 | * @throws PHP_CodeSniffer_Exception If sniff registration fails. |
1442 | 1423 | */ |
1443 | - public function populateTokenListeners() |
|
1444 | - { |
|
1424 | + public function populateTokenListeners() { |
|
1445 | 1425 | // Construct a list of listeners indexed by token being listened for. |
1446 | 1426 | $this->_tokenListeners = array(); |
1447 | 1427 | |
@@ -1526,8 +1506,7 @@ discard block |
||
1526 | 1506 | * |
1527 | 1507 | * @return void |
1528 | 1508 | */ |
1529 | - public function setSniffProperty($listenerClass, $name, $value) |
|
1530 | - { |
|
1509 | + public function setSniffProperty($listenerClass, $name, $value) { |
|
1531 | 1510 | // Setting a property for a sniff we are not using. |
1532 | 1511 | if (isset($this->listeners[$listenerClass]) === false) { |
1533 | 1512 | return; |
@@ -1563,8 +1542,7 @@ discard block |
||
1563 | 1542 | * @throws Exception If there was an error opening a directory. |
1564 | 1543 | * @see shouldProcessFile() |
1565 | 1544 | */ |
1566 | - public function getFilesToProcess($paths, $local=false) |
|
1567 | - { |
|
1545 | + public function getFilesToProcess($paths, $local=false) { |
|
1568 | 1546 | $files = array(); |
1569 | 1547 | |
1570 | 1548 | foreach ($paths as $path) { |
@@ -1624,8 +1602,7 @@ discard block |
||
1624 | 1602 | * |
1625 | 1603 | * @return bool |
1626 | 1604 | */ |
1627 | - public function shouldProcessFile($path, $basedir) |
|
1628 | - { |
|
1605 | + public function shouldProcessFile($path, $basedir) { |
|
1629 | 1606 | // Check that the file's extension is one we are checking. |
1630 | 1607 | // We are strict about checking the extension and we don't |
1631 | 1608 | // let files through with no extension or that start with a dot. |
@@ -1667,8 +1644,7 @@ discard block |
||
1667 | 1644 | * |
1668 | 1645 | * @return bool |
1669 | 1646 | */ |
1670 | - public function shouldIgnoreFile($path, $basedir) |
|
1671 | - { |
|
1647 | + public function shouldIgnoreFile($path, $basedir) { |
|
1672 | 1648 | $relativePath = $path; |
1673 | 1649 | if (strpos($path, $basedir) === 0) { |
1674 | 1650 | // The +1 cuts off the directory separator as well. |
@@ -1734,8 +1710,7 @@ discard block |
||
1734 | 1710 | * @throws PHP_CodeSniffer_Exception If the file could not be processed. |
1735 | 1711 | * @see _processFile() |
1736 | 1712 | */ |
1737 | - public function processFile($file, $contents=null) |
|
1738 | - { |
|
1713 | + public function processFile($file, $contents=null) { |
|
1739 | 1714 | if ($contents === null && file_exists($file) === false) { |
1740 | 1715 | throw new PHP_CodeSniffer_Exception("Source file $file does not exist"); |
1741 | 1716 | } |
@@ -1868,8 +1843,7 @@ discard block |
||
1868 | 1843 | * @return PHP_CodeSniffer_File |
1869 | 1844 | * @see processFile() |
1870 | 1845 | */ |
1871 | - private function _processFile($file, $contents) |
|
1872 | - { |
|
1846 | + private function _processFile($file, $contents) { |
|
1873 | 1847 | $stdin = false; |
1874 | 1848 | $cliValues = $this->cli->getCommandLineValues(); |
1875 | 1849 | if (empty($cliValues['files']) === true) { |
@@ -1927,8 +1901,7 @@ discard block |
||
1927 | 1901 | * |
1928 | 1902 | * @return void |
1929 | 1903 | */ |
1930 | - public function generateDocs($standard, array $sniffs=array(), $generator='Text') |
|
1931 | - { |
|
1904 | + public function generateDocs($standard, array $sniffs=array(), $generator='Text') { |
|
1932 | 1905 | if (class_exists('PHP_CodeSniffer_DocGenerators_'.$generator, true) === false) { |
1933 | 1906 | throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_'.$generator.' not found'); |
1934 | 1907 | } |
@@ -1946,8 +1919,7 @@ discard block |
||
1946 | 1919 | * |
1947 | 1920 | * @return PHP_CodeSniffer_Sniff[] |
1948 | 1921 | */ |
1949 | - public function getSniffs() |
|
1950 | - { |
|
1922 | + public function getSniffs() { |
|
1951 | 1923 | return $this->listeners; |
1952 | 1924 | |
1953 | 1925 | }//end getSniffs() |
@@ -1958,8 +1930,7 @@ discard block |
||
1958 | 1930 | * |
1959 | 1931 | * @return array |
1960 | 1932 | */ |
1961 | - public function getTokenSniffs() |
|
1962 | - { |
|
1933 | + public function getTokenSniffs() { |
|
1963 | 1934 | return $this->_tokenListeners; |
1964 | 1935 | |
1965 | 1936 | }//end getTokenSniffs() |
@@ -2057,8 +2028,7 @@ discard block |
||
2057 | 2028 | * |
2058 | 2029 | * @return boolean |
2059 | 2030 | */ |
2060 | - public static function isUnderscoreName($string) |
|
2061 | - { |
|
2031 | + public static function isUnderscoreName($string) { |
|
2062 | 2032 | // If there are space in the name, it can't be valid. |
2063 | 2033 | if (strpos($string, ' ') !== false) { |
2064 | 2034 | return false; |
@@ -2098,8 +2068,7 @@ discard block |
||
2098 | 2068 | * |
2099 | 2069 | * @return string |
2100 | 2070 | */ |
2101 | - public static function suggestType($varType) |
|
2102 | - { |
|
2071 | + public static function suggestType($varType) { |
|
2103 | 2072 | if ($varType === '') { |
2104 | 2073 | return ''; |
2105 | 2074 | } |
@@ -2172,8 +2141,7 @@ discard block |
||
2172 | 2141 | * |
2173 | 2142 | * @return string |
2174 | 2143 | */ |
2175 | - public static function prepareForOutput($content) |
|
2176 | - { |
|
2144 | + public static function prepareForOutput($content) { |
|
2177 | 2145 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
2178 | 2146 | $content = str_replace("\r", '\r', $content); |
2179 | 2147 | $content = str_replace("\n", '\n', $content); |
@@ -2195,8 +2163,7 @@ discard block |
||
2195 | 2163 | * |
2196 | 2164 | * @return array |
2197 | 2165 | */ |
2198 | - public static function getInstalledStandardPaths() |
|
2199 | - { |
|
2166 | + public static function getInstalledStandardPaths() { |
|
2200 | 2167 | $installedPaths = array(dirname(__FILE__).DIRECTORY_SEPARATOR.'CodeSniffer'.DIRECTORY_SEPARATOR.'Standards'); |
2201 | 2168 | $configPaths = PHP_CodeSniffer::getConfigData('installed_paths'); |
2202 | 2169 | if ($configPaths !== null) { |
@@ -2283,8 +2250,7 @@ discard block |
||
2283 | 2250 | * @return boolean |
2284 | 2251 | * @see getInstalledStandards() |
2285 | 2252 | */ |
2286 | - public static function isInstalledStandard($standard) |
|
2287 | - { |
|
2253 | + public static function isInstalledStandard($standard) { |
|
2288 | 2254 | $path = self::getInstalledStandardPath($standard); |
2289 | 2255 | if ($path !== null && strpos($path, 'ruleset.xml') !== false) { |
2290 | 2256 | return true; |
@@ -2326,8 +2292,7 @@ discard block |
||
2326 | 2292 | * |
2327 | 2293 | * @return string|null |
2328 | 2294 | */ |
2329 | - public static function getInstalledStandardPath($standard) |
|
2330 | - { |
|
2295 | + public static function getInstalledStandardPath($standard) { |
|
2331 | 2296 | $installedPaths = self::getInstalledStandardPaths(); |
2332 | 2297 | foreach ($installedPaths as $installedPath) { |
2333 | 2298 | $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard; |
@@ -2359,8 +2324,7 @@ discard block |
||
2359 | 2324 | * @see setConfigData() |
2360 | 2325 | * @see getAllConfigData() |
2361 | 2326 | */ |
2362 | - public static function getConfigData($key) |
|
2363 | - { |
|
2327 | + public static function getConfigData($key) { |
|
2364 | 2328 | $phpCodeSnifferConfig = self::getAllConfigData(); |
2365 | 2329 | |
2366 | 2330 | if ($phpCodeSnifferConfig === null) { |
@@ -2394,8 +2358,7 @@ discard block |
||
2394 | 2358 | * @see getConfigData() |
2395 | 2359 | * @throws PHP_CodeSniffer_Exception If the config file can not be written. |
2396 | 2360 | */ |
2397 | - public static function setConfigData($key, $value, $temp=false) |
|
2398 | - { |
|
2361 | + public static function setConfigData($key, $value, $temp=false) { |
|
2399 | 2362 | if ($temp === false) { |
2400 | 2363 | $path = ''; |
2401 | 2364 | if (is_callable('Phar::running') === true) { |
@@ -2456,8 +2419,7 @@ discard block |
||
2456 | 2419 | * @return array<string, string> |
2457 | 2420 | * @see getConfigData() |
2458 | 2421 | */ |
2459 | - public static function getAllConfigData() |
|
2460 | - { |
|
2422 | + public static function getAllConfigData() { |
|
2461 | 2423 | if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) { |
2462 | 2424 | return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']; |
2463 | 2425 | } |
@@ -2495,8 +2457,7 @@ discard block |
||
2495 | 2457 | * |
2496 | 2458 | * @return mixed |
2497 | 2459 | */ |
2498 | - public static function isPharFile($path) |
|
2499 | - { |
|
2460 | + public static function isPharFile($path) { |
|
2500 | 2461 | if (strpos($path, 'phar://') === 0) { |
2501 | 2462 | return true; |
2502 | 2463 | } |
@@ -2515,8 +2476,7 @@ discard block |
||
2515 | 2476 | * |
2516 | 2477 | * @return mixed |
2517 | 2478 | */ |
2518 | - public static function realpath($path) |
|
2519 | - { |
|
2479 | + public static function realpath($path) { |
|
2520 | 2480 | // Support the path replacement of ~ with the user's home directory. |
2521 | 2481 | if (substr($path, 0, 2) === '~/') { |
2522 | 2482 | $homeDir = getenv('HOME'); |
@@ -2563,8 +2523,7 @@ discard block |
||
2563 | 2523 | * |
2564 | 2524 | * @return void |
2565 | 2525 | */ |
2566 | - public static function chdir($path) |
|
2567 | - { |
|
2526 | + public static function chdir($path) { |
|
2568 | 2527 | if (self::isPharFile($path) === true) { |
2569 | 2528 | $phar = Phar::running(false); |
2570 | 2529 | chdir(dirname($phar)); |
@@ -157,7 +157,7 @@ |
||
157 | 157 | * |
158 | 158 | * The TOC is just an unordered list of bookmarks to sniffs on the page. |
159 | 159 | * |
160 | - * @param array $standardFiles An array of paths to the XML standard files. |
|
160 | + * @param string[] $standardFiles An array of paths to the XML standard files. |
|
161 | 161 | * |
162 | 162 | * @return void |
163 | 163 | */ |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | */ |
15 | 15 | |
16 | 16 | if (class_exists('PHP_CodeSniffer_DocGenerators_Generator', true) === false) { |
17 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_Generator not found'); |
|
17 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_Generator not found'); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | /** |
@@ -37,49 +37,49 @@ discard block |
||
37 | 37 | { |
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * Generates the documentation for a standard. |
|
42 | - * |
|
43 | - * @return void |
|
44 | - * @see processSniff() |
|
45 | - */ |
|
46 | - public function generate() |
|
47 | - { |
|
48 | - ob_start(); |
|
49 | - $this->printHeader(); |
|
50 | - |
|
51 | - $standardFiles = $this->getStandardFiles(); |
|
52 | - $this->printToc($standardFiles); |
|
53 | - |
|
54 | - foreach ($standardFiles as $standard) { |
|
55 | - $doc = new DOMDocument(); |
|
56 | - $doc->load($standard); |
|
57 | - $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
58 | - $this->processSniff($documentation); |
|
59 | - } |
|
60 | - |
|
61 | - $this->printFooter(); |
|
62 | - |
|
63 | - $content = ob_get_contents(); |
|
64 | - ob_end_clean(); |
|
65 | - |
|
66 | - echo $content; |
|
67 | - |
|
68 | - }//end generate() |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * Print the header of the HTML page. |
|
73 | - * |
|
74 | - * @return void |
|
75 | - */ |
|
76 | - protected function printHeader() |
|
77 | - { |
|
78 | - $standard = $this->getStandard(); |
|
79 | - echo '<html>'.PHP_EOL; |
|
80 | - echo ' <head>'.PHP_EOL; |
|
81 | - echo " <title>$standard Coding Standards</title>".PHP_EOL; |
|
82 | - echo ' <style> |
|
40 | + /** |
|
41 | + * Generates the documentation for a standard. |
|
42 | + * |
|
43 | + * @return void |
|
44 | + * @see processSniff() |
|
45 | + */ |
|
46 | + public function generate() |
|
47 | + { |
|
48 | + ob_start(); |
|
49 | + $this->printHeader(); |
|
50 | + |
|
51 | + $standardFiles = $this->getStandardFiles(); |
|
52 | + $this->printToc($standardFiles); |
|
53 | + |
|
54 | + foreach ($standardFiles as $standard) { |
|
55 | + $doc = new DOMDocument(); |
|
56 | + $doc->load($standard); |
|
57 | + $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
58 | + $this->processSniff($documentation); |
|
59 | + } |
|
60 | + |
|
61 | + $this->printFooter(); |
|
62 | + |
|
63 | + $content = ob_get_contents(); |
|
64 | + ob_end_clean(); |
|
65 | + |
|
66 | + echo $content; |
|
67 | + |
|
68 | + }//end generate() |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * Print the header of the HTML page. |
|
73 | + * |
|
74 | + * @return void |
|
75 | + */ |
|
76 | + protected function printHeader() |
|
77 | + { |
|
78 | + $standard = $this->getStandard(); |
|
79 | + echo '<html>'.PHP_EOL; |
|
80 | + echo ' <head>'.PHP_EOL; |
|
81 | + echo " <title>$standard Coding Standards</title>".PHP_EOL; |
|
82 | + echo ' <style> |
|
83 | 83 | body { |
84 | 84 | background-color: #FFFFFF; |
85 | 85 | font-size: 14px; |
@@ -145,148 +145,148 @@ discard block |
||
145 | 145 | color: #000000; |
146 | 146 | } |
147 | 147 | </style>'.PHP_EOL; |
148 | - echo ' </head>'.PHP_EOL; |
|
149 | - echo ' <body>'.PHP_EOL; |
|
150 | - echo " <h1>$standard Coding Standards</h1>".PHP_EOL; |
|
151 | - |
|
152 | - }//end printHeader() |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Print the table of contents for the standard. |
|
157 | - * |
|
158 | - * The TOC is just an unordered list of bookmarks to sniffs on the page. |
|
159 | - * |
|
160 | - * @param array $standardFiles An array of paths to the XML standard files. |
|
161 | - * |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - protected function printToc($standardFiles) |
|
165 | - { |
|
166 | - echo ' <h2>Table of Contents</h2>'.PHP_EOL; |
|
167 | - echo ' <ul class="toc">'.PHP_EOL; |
|
168 | - |
|
169 | - foreach ($standardFiles as $standard) { |
|
170 | - $doc = new DOMDocument(); |
|
171 | - $doc->load($standard); |
|
172 | - $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
173 | - $title = $this->getTitle($documentation); |
|
174 | - echo ' <li><a href="#'.str_replace(' ', '-', $title)."\">$title</a></li>".PHP_EOL; |
|
175 | - } |
|
176 | - |
|
177 | - echo ' </ul>'.PHP_EOL; |
|
178 | - |
|
179 | - }//end printToc() |
|
180 | - |
|
181 | - |
|
182 | - /** |
|
183 | - * Print the footer of the HTML page. |
|
184 | - * |
|
185 | - * @return void |
|
186 | - */ |
|
187 | - protected function printFooter() |
|
188 | - { |
|
189 | - // Turn off errors so we don't get timezone warnings if people |
|
190 | - // don't have their timezone set. |
|
191 | - $errorLevel = error_reporting(0); |
|
192 | - echo ' <div class="tag-line">'; |
|
193 | - echo 'Documentation generated on '.date('r'); |
|
194 | - echo ' by <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP_CodeSniffer '.PHP_CodeSniffer::VERSION.'</a>'; |
|
195 | - echo '</div>'.PHP_EOL; |
|
196 | - error_reporting($errorLevel); |
|
197 | - |
|
198 | - echo ' </body>'.PHP_EOL; |
|
199 | - echo '</html>'.PHP_EOL; |
|
200 | - |
|
201 | - }//end printFooter() |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Process the documentation for a single sniff. |
|
206 | - * |
|
207 | - * @param DOMNode $doc The DOMNode object for the sniff. |
|
208 | - * It represents the "documentation" tag in the XML |
|
209 | - * standard file. |
|
210 | - * |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - public function processSniff(DOMNode $doc) |
|
214 | - { |
|
215 | - $title = $this->getTitle($doc); |
|
216 | - echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL; |
|
217 | - echo " <h2>$title</h2>".PHP_EOL; |
|
218 | - |
|
219 | - foreach ($doc->childNodes as $node) { |
|
220 | - if ($node->nodeName === 'standard') { |
|
221 | - $this->printTextBlock($node); |
|
222 | - } else if ($node->nodeName === 'code_comparison') { |
|
223 | - $this->printCodeComparisonBlock($node); |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - }//end processSniff() |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Print a text block found in a standard. |
|
232 | - * |
|
233 | - * @param DOMNode $node The DOMNode object for the text block. |
|
234 | - * |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function printTextBlock($node) |
|
238 | - { |
|
239 | - $content = trim($node->nodeValue); |
|
240 | - $content = htmlspecialchars($content); |
|
241 | - |
|
242 | - // Allow em tags only. |
|
243 | - $content = str_replace('<em>', '<em>', $content); |
|
244 | - $content = str_replace('</em>', '</em>', $content); |
|
245 | - |
|
246 | - echo " <p class=\"text\">$content</p>".PHP_EOL; |
|
247 | - |
|
248 | - }//end printTextBlock() |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * Print a code comparison block found in a standard. |
|
253 | - * |
|
254 | - * @param DOMNode $node The DOMNode object for the code comparison block. |
|
255 | - * |
|
256 | - * @return void |
|
257 | - */ |
|
258 | - protected function printCodeComparisonBlock($node) |
|
259 | - { |
|
260 | - $codeBlocks = $node->getElementsByTagName('code'); |
|
261 | - |
|
262 | - $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |
|
263 | - $first = trim($codeBlocks->item(0)->nodeValue); |
|
264 | - $first = str_replace('<?php', '<?php', $first); |
|
265 | - $first = str_replace("\n", '</br>', $first); |
|
266 | - $first = str_replace(' ', ' ', $first); |
|
267 | - $first = str_replace('<em>', '<span class="code-comparison-highlight">', $first); |
|
268 | - $first = str_replace('</em>', '</span>', $first); |
|
269 | - |
|
270 | - $secondTitle = $codeBlocks->item(1)->getAttribute('title'); |
|
271 | - $second = trim($codeBlocks->item(1)->nodeValue); |
|
272 | - $second = str_replace('<?php', '<?php', $second); |
|
273 | - $second = str_replace("\n", '</br>', $second); |
|
274 | - $second = str_replace(' ', ' ', $second); |
|
275 | - $second = str_replace('<em>', '<span class="code-comparison-highlight">', $second); |
|
276 | - $second = str_replace('</em>', '</span>', $second); |
|
277 | - |
|
278 | - echo ' <table class="code-comparison">'.PHP_EOL; |
|
279 | - echo ' <tr>'.PHP_EOL; |
|
280 | - echo " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL; |
|
281 | - echo " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL; |
|
282 | - echo ' </tr>'.PHP_EOL; |
|
283 | - echo ' <tr>'.PHP_EOL; |
|
284 | - echo " <td class=\"code-comparison-code\">$first</td>".PHP_EOL; |
|
285 | - echo " <td class=\"code-comparison-code\">$second</td>".PHP_EOL; |
|
286 | - echo ' </tr>'.PHP_EOL; |
|
287 | - echo ' </table>'.PHP_EOL; |
|
288 | - |
|
289 | - }//end printCodeComparisonBlock() |
|
148 | + echo ' </head>'.PHP_EOL; |
|
149 | + echo ' <body>'.PHP_EOL; |
|
150 | + echo " <h1>$standard Coding Standards</h1>".PHP_EOL; |
|
151 | + |
|
152 | + }//end printHeader() |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Print the table of contents for the standard. |
|
157 | + * |
|
158 | + * The TOC is just an unordered list of bookmarks to sniffs on the page. |
|
159 | + * |
|
160 | + * @param array $standardFiles An array of paths to the XML standard files. |
|
161 | + * |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + protected function printToc($standardFiles) |
|
165 | + { |
|
166 | + echo ' <h2>Table of Contents</h2>'.PHP_EOL; |
|
167 | + echo ' <ul class="toc">'.PHP_EOL; |
|
168 | + |
|
169 | + foreach ($standardFiles as $standard) { |
|
170 | + $doc = new DOMDocument(); |
|
171 | + $doc->load($standard); |
|
172 | + $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
173 | + $title = $this->getTitle($documentation); |
|
174 | + echo ' <li><a href="#'.str_replace(' ', '-', $title)."\">$title</a></li>".PHP_EOL; |
|
175 | + } |
|
176 | + |
|
177 | + echo ' </ul>'.PHP_EOL; |
|
178 | + |
|
179 | + }//end printToc() |
|
180 | + |
|
181 | + |
|
182 | + /** |
|
183 | + * Print the footer of the HTML page. |
|
184 | + * |
|
185 | + * @return void |
|
186 | + */ |
|
187 | + protected function printFooter() |
|
188 | + { |
|
189 | + // Turn off errors so we don't get timezone warnings if people |
|
190 | + // don't have their timezone set. |
|
191 | + $errorLevel = error_reporting(0); |
|
192 | + echo ' <div class="tag-line">'; |
|
193 | + echo 'Documentation generated on '.date('r'); |
|
194 | + echo ' by <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP_CodeSniffer '.PHP_CodeSniffer::VERSION.'</a>'; |
|
195 | + echo '</div>'.PHP_EOL; |
|
196 | + error_reporting($errorLevel); |
|
197 | + |
|
198 | + echo ' </body>'.PHP_EOL; |
|
199 | + echo '</html>'.PHP_EOL; |
|
200 | + |
|
201 | + }//end printFooter() |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Process the documentation for a single sniff. |
|
206 | + * |
|
207 | + * @param DOMNode $doc The DOMNode object for the sniff. |
|
208 | + * It represents the "documentation" tag in the XML |
|
209 | + * standard file. |
|
210 | + * |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + public function processSniff(DOMNode $doc) |
|
214 | + { |
|
215 | + $title = $this->getTitle($doc); |
|
216 | + echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL; |
|
217 | + echo " <h2>$title</h2>".PHP_EOL; |
|
218 | + |
|
219 | + foreach ($doc->childNodes as $node) { |
|
220 | + if ($node->nodeName === 'standard') { |
|
221 | + $this->printTextBlock($node); |
|
222 | + } else if ($node->nodeName === 'code_comparison') { |
|
223 | + $this->printCodeComparisonBlock($node); |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + }//end processSniff() |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Print a text block found in a standard. |
|
232 | + * |
|
233 | + * @param DOMNode $node The DOMNode object for the text block. |
|
234 | + * |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function printTextBlock($node) |
|
238 | + { |
|
239 | + $content = trim($node->nodeValue); |
|
240 | + $content = htmlspecialchars($content); |
|
241 | + |
|
242 | + // Allow em tags only. |
|
243 | + $content = str_replace('<em>', '<em>', $content); |
|
244 | + $content = str_replace('</em>', '</em>', $content); |
|
245 | + |
|
246 | + echo " <p class=\"text\">$content</p>".PHP_EOL; |
|
247 | + |
|
248 | + }//end printTextBlock() |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * Print a code comparison block found in a standard. |
|
253 | + * |
|
254 | + * @param DOMNode $node The DOMNode object for the code comparison block. |
|
255 | + * |
|
256 | + * @return void |
|
257 | + */ |
|
258 | + protected function printCodeComparisonBlock($node) |
|
259 | + { |
|
260 | + $codeBlocks = $node->getElementsByTagName('code'); |
|
261 | + |
|
262 | + $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |
|
263 | + $first = trim($codeBlocks->item(0)->nodeValue); |
|
264 | + $first = str_replace('<?php', '<?php', $first); |
|
265 | + $first = str_replace("\n", '</br>', $first); |
|
266 | + $first = str_replace(' ', ' ', $first); |
|
267 | + $first = str_replace('<em>', '<span class="code-comparison-highlight">', $first); |
|
268 | + $first = str_replace('</em>', '</span>', $first); |
|
269 | + |
|
270 | + $secondTitle = $codeBlocks->item(1)->getAttribute('title'); |
|
271 | + $second = trim($codeBlocks->item(1)->nodeValue); |
|
272 | + $second = str_replace('<?php', '<?php', $second); |
|
273 | + $second = str_replace("\n", '</br>', $second); |
|
274 | + $second = str_replace(' ', ' ', $second); |
|
275 | + $second = str_replace('<em>', '<span class="code-comparison-highlight">', $second); |
|
276 | + $second = str_replace('</em>', '</span>', $second); |
|
277 | + |
|
278 | + echo ' <table class="code-comparison">'.PHP_EOL; |
|
279 | + echo ' <tr>'.PHP_EOL; |
|
280 | + echo " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL; |
|
281 | + echo " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL; |
|
282 | + echo ' </tr>'.PHP_EOL; |
|
283 | + echo ' <tr>'.PHP_EOL; |
|
284 | + echo " <td class=\"code-comparison-code\">$first</td>".PHP_EOL; |
|
285 | + echo " <td class=\"code-comparison-code\">$second</td>".PHP_EOL; |
|
286 | + echo ' </tr>'.PHP_EOL; |
|
287 | + echo ' </table>'.PHP_EOL; |
|
288 | + |
|
289 | + }//end printCodeComparisonBlock() |
|
290 | 290 | |
291 | 291 | |
292 | 292 | }//end class |
@@ -13,8 +13,8 @@ discard block |
||
13 | 13 | * @link http://pear.php.net/package/PHP_CodeSniffer |
14 | 14 | */ |
15 | 15 | |
16 | -if (class_exists('PHP_CodeSniffer_DocGenerators_Generator', true) === false) { |
|
17 | - throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_DocGenerators_Generator not found'); |
|
16 | +if ( class_exists( 'PHP_CodeSniffer_DocGenerators_Generator', true ) === false ) { |
|
17 | + throw new PHP_CodeSniffer_Exception( 'Class PHP_CodeSniffer_DocGenerators_Generator not found' ); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | /** |
@@ -49,13 +49,13 @@ discard block |
||
49 | 49 | $this->printHeader(); |
50 | 50 | |
51 | 51 | $standardFiles = $this->getStandardFiles(); |
52 | - $this->printToc($standardFiles); |
|
52 | + $this->printToc( $standardFiles ); |
|
53 | 53 | |
54 | - foreach ($standardFiles as $standard) { |
|
54 | + foreach ( $standardFiles as $standard ) { |
|
55 | 55 | $doc = new DOMDocument(); |
56 | - $doc->load($standard); |
|
57 | - $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
58 | - $this->processSniff($documentation); |
|
56 | + $doc->load( $standard ); |
|
57 | + $documentation = $doc->getElementsByTagName( 'documentation' )->item( 0 ); |
|
58 | + $this->processSniff( $documentation ); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | $this->printFooter(); |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | protected function printHeader() |
77 | 77 | { |
78 | 78 | $standard = $this->getStandard(); |
79 | - echo '<html>'.PHP_EOL; |
|
80 | - echo ' <head>'.PHP_EOL; |
|
81 | - echo " <title>$standard Coding Standards</title>".PHP_EOL; |
|
79 | + echo '<html>' . PHP_EOL; |
|
80 | + echo ' <head>' . PHP_EOL; |
|
81 | + echo " <title>$standard Coding Standards</title>" . PHP_EOL; |
|
82 | 82 | echo ' <style> |
83 | 83 | body { |
84 | 84 | background-color: #FFFFFF; |
@@ -145,9 +145,9 @@ discard block |
||
145 | 145 | color: #000000; |
146 | 146 | } |
147 | 147 | </style>'.PHP_EOL; |
148 | - echo ' </head>'.PHP_EOL; |
|
149 | - echo ' <body>'.PHP_EOL; |
|
150 | - echo " <h1>$standard Coding Standards</h1>".PHP_EOL; |
|
148 | + echo ' </head>' . PHP_EOL; |
|
149 | + echo ' <body>' . PHP_EOL; |
|
150 | + echo " <h1>$standard Coding Standards</h1>" . PHP_EOL; |
|
151 | 151 | |
152 | 152 | }//end printHeader() |
153 | 153 | |
@@ -161,20 +161,20 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @return void |
163 | 163 | */ |
164 | - protected function printToc($standardFiles) |
|
164 | + protected function printToc( $standardFiles ) |
|
165 | 165 | { |
166 | - echo ' <h2>Table of Contents</h2>'.PHP_EOL; |
|
167 | - echo ' <ul class="toc">'.PHP_EOL; |
|
166 | + echo ' <h2>Table of Contents</h2>' . PHP_EOL; |
|
167 | + echo ' <ul class="toc">' . PHP_EOL; |
|
168 | 168 | |
169 | - foreach ($standardFiles as $standard) { |
|
169 | + foreach ( $standardFiles as $standard ) { |
|
170 | 170 | $doc = new DOMDocument(); |
171 | - $doc->load($standard); |
|
172 | - $documentation = $doc->getElementsByTagName('documentation')->item(0); |
|
173 | - $title = $this->getTitle($documentation); |
|
174 | - echo ' <li><a href="#'.str_replace(' ', '-', $title)."\">$title</a></li>".PHP_EOL; |
|
171 | + $doc->load( $standard ); |
|
172 | + $documentation = $doc->getElementsByTagName( 'documentation' )->item( 0 ); |
|
173 | + $title = $this->getTitle( $documentation ); |
|
174 | + echo ' <li><a href="#' . str_replace( ' ', '-', $title ) . "\">$title</a></li>" . PHP_EOL; |
|
175 | 175 | } |
176 | 176 | |
177 | - echo ' </ul>'.PHP_EOL; |
|
177 | + echo ' </ul>' . PHP_EOL; |
|
178 | 178 | |
179 | 179 | }//end printToc() |
180 | 180 | |
@@ -188,15 +188,15 @@ discard block |
||
188 | 188 | { |
189 | 189 | // Turn off errors so we don't get timezone warnings if people |
190 | 190 | // don't have their timezone set. |
191 | - $errorLevel = error_reporting(0); |
|
191 | + $errorLevel = error_reporting( 0 ); |
|
192 | 192 | echo ' <div class="tag-line">'; |
193 | - echo 'Documentation generated on '.date('r'); |
|
194 | - echo ' by <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP_CodeSniffer '.PHP_CodeSniffer::VERSION.'</a>'; |
|
195 | - echo '</div>'.PHP_EOL; |
|
196 | - error_reporting($errorLevel); |
|
193 | + echo 'Documentation generated on ' . date( 'r' ); |
|
194 | + echo ' by <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP_CodeSniffer ' . PHP_CodeSniffer::VERSION . '</a>'; |
|
195 | + echo '</div>' . PHP_EOL; |
|
196 | + error_reporting( $errorLevel ); |
|
197 | 197 | |
198 | - echo ' </body>'.PHP_EOL; |
|
199 | - echo '</html>'.PHP_EOL; |
|
198 | + echo ' </body>' . PHP_EOL; |
|
199 | + echo '</html>' . PHP_EOL; |
|
200 | 200 | |
201 | 201 | }//end printFooter() |
202 | 202 | |
@@ -210,17 +210,17 @@ discard block |
||
210 | 210 | * |
211 | 211 | * @return void |
212 | 212 | */ |
213 | - public function processSniff(DOMNode $doc) |
|
213 | + public function processSniff( DOMNode $doc ) |
|
214 | 214 | { |
215 | - $title = $this->getTitle($doc); |
|
216 | - echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL; |
|
217 | - echo " <h2>$title</h2>".PHP_EOL; |
|
218 | - |
|
219 | - foreach ($doc->childNodes as $node) { |
|
220 | - if ($node->nodeName === 'standard') { |
|
221 | - $this->printTextBlock($node); |
|
222 | - } else if ($node->nodeName === 'code_comparison') { |
|
223 | - $this->printCodeComparisonBlock($node); |
|
215 | + $title = $this->getTitle( $doc ); |
|
216 | + echo ' <a name="' . str_replace( ' ', '-', $title ) . '" />' . PHP_EOL; |
|
217 | + echo " <h2>$title</h2>" . PHP_EOL; |
|
218 | + |
|
219 | + foreach ( $doc->childNodes as $node ) { |
|
220 | + if ( $node->nodeName === 'standard' ) { |
|
221 | + $this->printTextBlock( $node ); |
|
222 | + } else if ( $node->nodeName === 'code_comparison' ) { |
|
223 | + $this->printCodeComparisonBlock( $node ); |
|
224 | 224 | } |
225 | 225 | } |
226 | 226 | |
@@ -234,16 +234,16 @@ discard block |
||
234 | 234 | * |
235 | 235 | * @return void |
236 | 236 | */ |
237 | - protected function printTextBlock($node) |
|
237 | + protected function printTextBlock( $node ) |
|
238 | 238 | { |
239 | - $content = trim($node->nodeValue); |
|
240 | - $content = htmlspecialchars($content); |
|
239 | + $content = trim( $node->nodeValue ); |
|
240 | + $content = htmlspecialchars( $content ); |
|
241 | 241 | |
242 | 242 | // Allow em tags only. |
243 | - $content = str_replace('<em>', '<em>', $content); |
|
244 | - $content = str_replace('</em>', '</em>', $content); |
|
243 | + $content = str_replace( '<em>', '<em>', $content ); |
|
244 | + $content = str_replace( '</em>', '</em>', $content ); |
|
245 | 245 | |
246 | - echo " <p class=\"text\">$content</p>".PHP_EOL; |
|
246 | + echo " <p class=\"text\">$content</p>" . PHP_EOL; |
|
247 | 247 | |
248 | 248 | }//end printTextBlock() |
249 | 249 | |
@@ -255,36 +255,36 @@ discard block |
||
255 | 255 | * |
256 | 256 | * @return void |
257 | 257 | */ |
258 | - protected function printCodeComparisonBlock($node) |
|
258 | + protected function printCodeComparisonBlock( $node ) |
|
259 | 259 | { |
260 | - $codeBlocks = $node->getElementsByTagName('code'); |
|
261 | - |
|
262 | - $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |
|
263 | - $first = trim($codeBlocks->item(0)->nodeValue); |
|
264 | - $first = str_replace('<?php', '<?php', $first); |
|
265 | - $first = str_replace("\n", '</br>', $first); |
|
266 | - $first = str_replace(' ', ' ', $first); |
|
267 | - $first = str_replace('<em>', '<span class="code-comparison-highlight">', $first); |
|
268 | - $first = str_replace('</em>', '</span>', $first); |
|
269 | - |
|
270 | - $secondTitle = $codeBlocks->item(1)->getAttribute('title'); |
|
271 | - $second = trim($codeBlocks->item(1)->nodeValue); |
|
272 | - $second = str_replace('<?php', '<?php', $second); |
|
273 | - $second = str_replace("\n", '</br>', $second); |
|
274 | - $second = str_replace(' ', ' ', $second); |
|
275 | - $second = str_replace('<em>', '<span class="code-comparison-highlight">', $second); |
|
276 | - $second = str_replace('</em>', '</span>', $second); |
|
277 | - |
|
278 | - echo ' <table class="code-comparison">'.PHP_EOL; |
|
279 | - echo ' <tr>'.PHP_EOL; |
|
280 | - echo " <td class=\"code-comparison-title\">$firstTitle</td>".PHP_EOL; |
|
281 | - echo " <td class=\"code-comparison-title\">$secondTitle</td>".PHP_EOL; |
|
282 | - echo ' </tr>'.PHP_EOL; |
|
283 | - echo ' <tr>'.PHP_EOL; |
|
284 | - echo " <td class=\"code-comparison-code\">$first</td>".PHP_EOL; |
|
285 | - echo " <td class=\"code-comparison-code\">$second</td>".PHP_EOL; |
|
286 | - echo ' </tr>'.PHP_EOL; |
|
287 | - echo ' </table>'.PHP_EOL; |
|
260 | + $codeBlocks = $node->getElementsByTagName( 'code' ); |
|
261 | + |
|
262 | + $firstTitle = $codeBlocks->item( 0 )->getAttribute( 'title' ); |
|
263 | + $first = trim( $codeBlocks->item( 0 )->nodeValue ); |
|
264 | + $first = str_replace( '<?php', '<?php', $first ); |
|
265 | + $first = str_replace( "\n", '</br>', $first ); |
|
266 | + $first = str_replace( ' ', ' ', $first ); |
|
267 | + $first = str_replace( '<em>', '<span class="code-comparison-highlight">', $first ); |
|
268 | + $first = str_replace( '</em>', '</span>', $first ); |
|
269 | + |
|
270 | + $secondTitle = $codeBlocks->item( 1 )->getAttribute( 'title' ); |
|
271 | + $second = trim( $codeBlocks->item( 1 )->nodeValue ); |
|
272 | + $second = str_replace( '<?php', '<?php', $second ); |
|
273 | + $second = str_replace( "\n", '</br>', $second ); |
|
274 | + $second = str_replace( ' ', ' ', $second ); |
|
275 | + $second = str_replace( '<em>', '<span class="code-comparison-highlight">', $second ); |
|
276 | + $second = str_replace( '</em>', '</span>', $second ); |
|
277 | + |
|
278 | + echo ' <table class="code-comparison">' . PHP_EOL; |
|
279 | + echo ' <tr>' . PHP_EOL; |
|
280 | + echo " <td class=\"code-comparison-title\">$firstTitle</td>" . PHP_EOL; |
|
281 | + echo " <td class=\"code-comparison-title\">$secondTitle</td>" . PHP_EOL; |
|
282 | + echo ' </tr>' . PHP_EOL; |
|
283 | + echo ' <tr>' . PHP_EOL; |
|
284 | + echo " <td class=\"code-comparison-code\">$first</td>" . PHP_EOL; |
|
285 | + echo " <td class=\"code-comparison-code\">$second</td>" . PHP_EOL; |
|
286 | + echo ' </tr>' . PHP_EOL; |
|
287 | + echo ' </table>' . PHP_EOL; |
|
288 | 288 | |
289 | 289 | }//end printCodeComparisonBlock() |
290 | 290 |
@@ -33,8 +33,7 @@ discard block |
||
33 | 33 | * @version Release: @package_version@ |
34 | 34 | * @link http://pear.php.net/package/PHP_CodeSniffer |
35 | 35 | */ |
36 | -class PHP_CodeSniffer_DocGenerators_HTML extends PHP_CodeSniffer_DocGenerators_Generator |
|
37 | -{ |
|
36 | +class PHP_CodeSniffer_DocGenerators_HTML extends PHP_CodeSniffer_DocGenerators_Generator { |
|
38 | 37 | |
39 | 38 | |
40 | 39 | /** |
@@ -43,8 +42,7 @@ discard block |
||
43 | 42 | * @return void |
44 | 43 | * @see processSniff() |
45 | 44 | */ |
46 | - public function generate() |
|
47 | - { |
|
45 | + public function generate() { |
|
48 | 46 | ob_start(); |
49 | 47 | $this->printHeader(); |
50 | 48 | |
@@ -73,8 +71,7 @@ discard block |
||
73 | 71 | * |
74 | 72 | * @return void |
75 | 73 | */ |
76 | - protected function printHeader() |
|
77 | - { |
|
74 | + protected function printHeader() { |
|
78 | 75 | $standard = $this->getStandard(); |
79 | 76 | echo '<html>'.PHP_EOL; |
80 | 77 | echo ' <head>'.PHP_EOL; |
@@ -161,8 +158,7 @@ discard block |
||
161 | 158 | * |
162 | 159 | * @return void |
163 | 160 | */ |
164 | - protected function printToc($standardFiles) |
|
165 | - { |
|
161 | + protected function printToc($standardFiles) { |
|
166 | 162 | echo ' <h2>Table of Contents</h2>'.PHP_EOL; |
167 | 163 | echo ' <ul class="toc">'.PHP_EOL; |
168 | 164 | |
@@ -184,8 +180,7 @@ discard block |
||
184 | 180 | * |
185 | 181 | * @return void |
186 | 182 | */ |
187 | - protected function printFooter() |
|
188 | - { |
|
183 | + protected function printFooter() { |
|
189 | 184 | // Turn off errors so we don't get timezone warnings if people |
190 | 185 | // don't have their timezone set. |
191 | 186 | $errorLevel = error_reporting(0); |
@@ -210,8 +205,7 @@ discard block |
||
210 | 205 | * |
211 | 206 | * @return void |
212 | 207 | */ |
213 | - public function processSniff(DOMNode $doc) |
|
214 | - { |
|
208 | + public function processSniff(DOMNode $doc) { |
|
215 | 209 | $title = $this->getTitle($doc); |
216 | 210 | echo ' <a name="'.str_replace(' ', '-', $title).'" />'.PHP_EOL; |
217 | 211 | echo " <h2>$title</h2>".PHP_EOL; |
@@ -234,8 +228,7 @@ discard block |
||
234 | 228 | * |
235 | 229 | * @return void |
236 | 230 | */ |
237 | - protected function printTextBlock($node) |
|
238 | - { |
|
231 | + protected function printTextBlock($node) { |
|
239 | 232 | $content = trim($node->nodeValue); |
240 | 233 | $content = htmlspecialchars($content); |
241 | 234 | |
@@ -255,8 +248,7 @@ discard block |
||
255 | 248 | * |
256 | 249 | * @return void |
257 | 250 | */ |
258 | - protected function printCodeComparisonBlock($node) |
|
259 | - { |
|
251 | + protected function printCodeComparisonBlock($node) { |
|
260 | 252 | $codeBlocks = $node->getElementsByTagName('code'); |
261 | 253 | |
262 | 254 | $firstTitle = $codeBlocks->item(0)->getAttribute('title'); |