@@ -38,8 +38,7 @@ discard block |
||
38 | 38 | use PHP_CodeSniffer\Sniffs\AbstactScopeSniff; |
39 | 39 | use PHP_CodeSniffer\Files\File; |
40 | 40 | |
41 | -class ValidMethodNameSniff extends AbstractScopeSniff |
|
42 | -{ |
|
41 | +class ValidMethodNameSniff extends AbstractScopeSniff { |
|
43 | 42 | /** |
44 | 43 | * A list of all PHP magic methods. |
45 | 44 | * |
@@ -84,7 +83,8 @@ discard block |
||
84 | 83 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
85 | 84 | { |
86 | 85 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
87 | - if ($methodName === null) { |
|
86 | + if ($methodName === null) |
|
87 | + { |
|
88 | 88 | // Ignore closures. |
89 | 89 | return; |
90 | 90 | } |
@@ -92,9 +92,11 @@ discard block |
||
92 | 92 | $className = $phpcsFile->getDeclarationName($currScope); |
93 | 93 | |
94 | 94 | // Is this a magic method i.e. is prefixed with "__". |
95 | - if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
|
95 | + if (0 === strcmp(substr($methodName, 0, 2), '__')) |
|
96 | + { |
|
96 | 97 | $magicPart = substr($methodName, 2); |
97 | - if (in_array($magicPart, self::$magicMethods) === false) { |
|
98 | + if (in_array($magicPart, self::$magicMethods) === false) |
|
99 | + { |
|
98 | 100 | $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
99 | 101 | $phpcsFile->addError($error, $stackPtr); |
100 | 102 | } |
@@ -103,16 +105,19 @@ discard block |
||
103 | 105 | } |
104 | 106 | |
105 | 107 | // PHP4 constructors are allowed to break our rules. |
106 | - if ($methodName === $className) { |
|
108 | + if ($methodName === $className) |
|
109 | + { |
|
107 | 110 | return; |
108 | 111 | } |
109 | 112 | |
110 | 113 | // PHP4 destructors are allowed to break our rules. |
111 | - if ($methodName === '_'.$className) { |
|
114 | + if ($methodName === '_'.$className) |
|
115 | + { |
|
112 | 116 | return; |
113 | 117 | } |
114 | 118 | |
115 | - if (0 !== strcmp($methodName, strtolower($methodName))) { |
|
119 | + if (0 !== strcmp($methodName, strtolower($methodName))) |
|
120 | + { |
|
116 | 121 | $uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
117 | 122 | $expectedMethodName = strtolower($uscrdMethodName); |
118 | 123 | $error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
@@ -125,17 +130,22 @@ discard block |
||
125 | 130 | $scopeSpecified = $methodProps['scope_specified']; |
126 | 131 | |
127 | 132 | // If it's a private method, it must have an underscore on the front. |
128 | - if ($scope === 'private' && $methodName{0} !== '_') { |
|
133 | + if ($scope === 'private' && $methodName{0} !== '_') |
|
134 | + { |
|
129 | 135 | $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
130 | 136 | $phpcsFile->addError($error, $stackPtr); |
131 | 137 | return; |
132 | 138 | } |
133 | 139 | |
134 | 140 | // If it's not a private method, it must not have an underscore on the front. |
135 | - if ($scope !== 'private' && $methodName{0} === '_') { |
|
136 | - if (true === $scopeSpecified) { |
|
141 | + if ($scope !== 'private' && $methodName{0} === '_') |
|
142 | + { |
|
143 | + if (true === $scopeSpecified) |
|
144 | + { |
|
137 | 145 | $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
138 | - } else { |
|
146 | + } |
|
147 | + else |
|
148 | + { |
|
139 | 149 | $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
140 | 150 | } |
141 | 151 | $phpcsFile->addError($error, $stackPtr); |
@@ -146,11 +156,14 @@ discard block |
||
146 | 156 | // then either an error or a warning is displayed. |
147 | 157 | $error_limit = 50; |
148 | 158 | $warning_limit = 35; |
149 | - if (strlen($methodName) > $error_limit) { |
|
159 | + if (strlen($methodName) > $error_limit) |
|
160 | + { |
|
150 | 161 | $error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
151 | 162 | $phpcsFile->addError($error, $stackPtr); |
152 | 163 | return; |
153 | - } else if (strlen($methodName) > $warning_limit) { |
|
164 | + } |
|
165 | + else if (strlen($methodName) > $warning_limit) |
|
166 | + { |
|
154 | 167 | $warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
155 | 168 | $phpcsFile->addWarning($warning, $stackPtr); |
156 | 169 | } |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class ValidFileNameSniff implements Sniff |
|
34 | -{ |
|
33 | +class ValidFileNameSniff implements Sniff { |
|
35 | 34 | /** |
36 | 35 | * Returns an array of tokens this test wants to listen for. |
37 | 36 | * |
@@ -67,7 +66,8 @@ discard block |
||
67 | 66 | $fileNameAndExt = basename($fullPath); |
68 | 67 | $fileName = substr($fileNameAndExt, 0, strrpos($fileNameAndExt, '.')); |
69 | 68 | |
70 | - if ($expectedFileName !== $fileName) { |
|
69 | + if ($expectedFileName !== $fileName) |
|
70 | + { |
|
71 | 71 | $errorTemplate = 'Filename "%s" doesn\'t match the name of the %s that it contains "%s" in lower case. "%s" was expected.'; |
72 | 72 | $errorMessage = sprintf( |
73 | 73 | $errorTemplate, |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class Utf8EncodingSniff implements Sniff |
|
34 | -{ |
|
33 | +class Utf8EncodingSniff implements Sniff { |
|
35 | 34 | |
36 | 35 | /** |
37 | 36 | * Returns an array of tokens this test wants to listen for. |
@@ -59,8 +58,10 @@ discard block |
||
59 | 58 | public function process(File $phpcsFile, $stackPtr) |
60 | 59 | { |
61 | 60 | // We are only interested if this is the first open tag. |
62 | - if ($stackPtr !== 0) { |
|
63 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
61 | + if ($stackPtr !== 0) |
|
62 | + { |
|
63 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) |
|
64 | + { |
|
64 | 65 | return; |
65 | 66 | } |
66 | 67 | } |
@@ -68,15 +69,18 @@ discard block |
||
68 | 69 | $file_path = $phpcsFile->getFilename(); |
69 | 70 | $file_name = basename($file_path); |
70 | 71 | $file_content = file_get_contents($file_path); |
71 | - if (false === mb_check_encoding($file_content, 'UTF-8')) { |
|
72 | + if (false === mb_check_encoding($file_content, 'UTF-8')) |
|
73 | + { |
|
72 | 74 | $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding.'; |
73 | 75 | $phpcsFile->addError($error, 0); |
74 | 76 | } |
75 | - if ( ! self::_checkUtf8W3c($file_content)) { |
|
77 | + if ( ! self::_checkUtf8W3c($file_content)) |
|
78 | + { |
|
76 | 79 | $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not successfully pass the W3C test.'; |
77 | 80 | $phpcsFile->addError($error, 0); |
78 | 81 | } |
79 | - if ( ! self::_checkUtf8Rfc3629($file_content)) { |
|
82 | + if ( ! self::_checkUtf8Rfc3629($file_content)) |
|
83 | + { |
|
80 | 84 | $error = 'File "' . $file_name . '" should be saved with Unicode (UTF-8) encoding, but it did not meet RFC3629 requirements.'; |
81 | 85 | $phpcsFile->addError($error, 0); |
82 | 86 | } |
@@ -98,7 +102,7 @@ discard block |
||
98 | 102 | { |
99 | 103 | $content_chunks=self::mb_chunk_split($content, 4096, ''); |
100 | 104 | foreach($content_chunks as $content_chunk) |
101 | - { |
|
105 | + { |
|
102 | 106 | $preg_result= preg_match( |
103 | 107 | '%^(?: |
104 | 108 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
@@ -135,29 +139,47 @@ discard block |
||
135 | 139 | private static function _checkUtf8Rfc3629($content) |
136 | 140 | { |
137 | 141 | $len = strlen($content); |
138 | - for ($i = 0; $i < $len; $i++) { |
|
142 | + for ($i = 0; $i < $len; $i++) |
|
143 | + { |
|
139 | 144 | $c = ord($content[$i]); |
140 | - if ($c > 128) { |
|
141 | - if (($c >= 254)) { |
|
145 | + if ($c > 128) |
|
146 | + { |
|
147 | + if (($c >= 254)) |
|
148 | + { |
|
142 | 149 | return false; |
143 | - } elseif ($c >= 252) { |
|
150 | + } |
|
151 | + elseif ($c >= 252) |
|
152 | + { |
|
144 | 153 | $bits=6; |
145 | - } elseif ($c >= 248) { |
|
154 | + } |
|
155 | + elseif ($c >= 248) |
|
156 | + { |
|
146 | 157 | $bits=5; |
147 | - } elseif ($c >= 240) { |
|
158 | + } |
|
159 | + elseif ($c >= 240) |
|
160 | + { |
|
148 | 161 | $bytes = 4; |
149 | - } elseif ($c >= 224) { |
|
162 | + } |
|
163 | + elseif ($c >= 224) |
|
164 | + { |
|
150 | 165 | $bytes = 3; |
151 | - } elseif ($c >= 192) { |
|
166 | + } |
|
167 | + elseif ($c >= 192) |
|
168 | + { |
|
152 | 169 | $bytes = 2; |
153 | - } else { |
|
170 | + } |
|
171 | + else |
|
172 | + { |
|
154 | 173 | return false; |
155 | - } if (($i + $bytes) > $len) { |
|
174 | + } if (($i + $bytes) > $len) |
|
175 | + { |
|
156 | 176 | return false; |
157 | - } while ($bytes > 1) { |
|
177 | + } while ($bytes > 1) |
|
178 | + { |
|
158 | 179 | $i++; |
159 | 180 | $b = ord($content[$i]); |
160 | - if ($b < 128 || $b > 191) { |
|
181 | + if ($b < 128 || $b > 191) |
|
182 | + { |
|
161 | 183 | return false; |
162 | 184 | } |
163 | 185 | $bytes--; |
@@ -185,10 +207,12 @@ discard block |
||
185 | 207 | $array = self::mbStringToArray ($str); |
186 | 208 | $n = -1; |
187 | 209 | $new = Array(); |
188 | - foreach ($array as $char) { |
|
210 | + foreach ($array as $char) |
|
211 | + { |
|
189 | 212 | $n++; |
190 | 213 | if ($n < $len) $new []= $char; |
191 | - elseif ($n == $len) { |
|
214 | + elseif ($n == $len) |
|
215 | + { |
|
192 | 216 | $new []= $glue . $char; |
193 | 217 | $n = 0; |
194 | 218 | } |
@@ -209,7 +233,8 @@ discard block |
||
209 | 233 | if (empty($str)) return false; |
210 | 234 | $len = mb_strlen($str); |
211 | 235 | $array = array(); |
212 | - for ($i = 0; $i < $len; $i++) { |
|
236 | + for ($i = 0; $i < $len; $i++) |
|
237 | + { |
|
213 | 238 | $array[] = mb_substr($str, $i, 1); |
214 | 239 | } |
215 | 240 | return $array; |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class ByteOrderMarkSniff implements Sniff |
|
34 | -{ |
|
33 | +class ByteOrderMarkSniff implements Sniff { |
|
35 | 34 | /** |
36 | 35 | * Returns an array of tokens this test wants to listen for. |
37 | 36 | * |
@@ -77,18 +76,22 @@ discard block |
||
77 | 76 | public function process(File $phpcsFile, $stackPtr ) |
78 | 77 | { |
79 | 78 | // We are only interested if this is the first open tag. |
80 | - if ($stackPtr !== 0) { |
|
81 | - if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
|
79 | + if ($stackPtr !== 0) |
|
80 | + { |
|
81 | + if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) |
|
82 | + { |
|
82 | 83 | return; |
83 | 84 | } |
84 | 85 | } |
85 | 86 | |
86 | 87 | $tokens = $phpcsFile->getTokens(); |
87 | 88 | $fileStartString = $tokens[0]['content']; |
88 | - foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) { |
|
89 | + foreach ($this->getBomDefinitions() as $bomName => $expectedBomHex) |
|
90 | + { |
|
89 | 91 | $bomByteLength = strlen($expectedBomHex) / 2; |
90 | 92 | $fileStartHex = bin2hex(substr($fileStartString, 0, $bomByteLength)); |
91 | - if ($fileStartHex === $expectedBomHex) { |
|
93 | + if ($fileStartHex === $expectedBomHex) |
|
94 | + { |
|
92 | 95 | $error = "File contains a $bomName byte order mark (BOM)."; |
93 | 96 | $phpcsFile->addError($error, $stackPtr); |
94 | 97 | break; |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class ElseOnNewLineSniff implements Sniff |
|
34 | -{ |
|
33 | +class ElseOnNewLineSniff implements Sniff { |
|
35 | 34 | |
36 | 35 | /** |
37 | 36 | * Returns an array of tokens this test wants to listen for. |
@@ -63,13 +62,15 @@ discard block |
||
63 | 62 | $else_token = $tokens[$stackPtr]; |
64 | 63 | $previous_non_blank_token_ptr = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true); |
65 | 64 | |
66 | - if (false === $previous_non_blank_token_ptr) { |
|
65 | + if (false === $previous_non_blank_token_ptr) |
|
66 | + { |
|
67 | 67 | // else is no preceded with any symbol, but it is not the responsibility of this sniff. |
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
71 | 71 | $previous_non_blank_token = $tokens[$previous_non_blank_token_ptr]; |
72 | - if ($previous_non_blank_token['line'] === $else_token['line']) { |
|
72 | + if ($previous_non_blank_token['line'] === $else_token['line']) |
|
73 | + { |
|
73 | 74 | $error = '"' . $else_token['content'] . '" should be on a new line.'; |
74 | 75 | $phpcsFile->addError($error, $stackPtr); |
75 | 76 | } |
@@ -31,8 +31,7 @@ discard block |
||
31 | 31 | use PHP_CodeSniffer\Sniffs\Sniff; |
32 | 32 | use PHP_CodeSniffer\Files\File; |
33 | 33 | |
34 | -class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff |
|
35 | -{ |
|
34 | +class DisallowWitheSpaceAroundPhpTagsSniff implements Sniff { |
|
36 | 35 | |
37 | 36 | /** |
38 | 37 | * Returns an array of tokens this test wants to listen for. |
@@ -65,16 +64,20 @@ discard block |
||
65 | 64 | $php_tag_token = $tokens[$stackPtr]; |
66 | 65 | $php_tag_code = $php_tag_token['code']; |
67 | 66 | |
68 | - if (T_OPEN_TAG === $php_tag_code) { |
|
67 | + if (T_OPEN_TAG === $php_tag_code) |
|
68 | + { |
|
69 | 69 | // opening php tag should be the first token. |
70 | 70 | // any whitespace beofre an opening php tag is tokenized |
71 | 71 | // as T_INLINE_HTML, so no need to check the content of the token. |
72 | 72 | $isFirst = 0 === $stackPtr; |
73 | - if ( ! $isFirst) { |
|
73 | + if ( ! $isFirst) |
|
74 | + { |
|
74 | 75 | $error = 'Any char before the opening PHP tag is prohibited. Please remove newline or indentation before the opening PHP tag.'; |
75 | 76 | $phpcsFile->addError($error, $stackPtr); |
76 | 77 | } |
77 | - } else { |
|
78 | + } |
|
79 | + else |
|
80 | + { |
|
78 | 81 | // if (T_CLOSE_TAG === $php_tag_code) |
79 | 82 | // closing php tag should be the last token |
80 | 83 | // and it must not contain any whitespace. |
@@ -82,7 +85,8 @@ discard block |
||
82 | 85 | $isLast = count($tokens) - 1 === $stackPtr; |
83 | 86 | // both of the two closing php tags contains 2 chars exactly. |
84 | 87 | $containsEndTagOnly = strlen($php_tag_string) > 2; |
85 | - if ( ! $isLast || ! $containsEndTagOnly ) { |
|
88 | + if ( ! $isLast || ! $containsEndTagOnly ) |
|
89 | + { |
|
86 | 90 | $error = 'Any char after the closing PHP tag is prohibited. Please removes newline or spaces after the closing PHP tag.'; |
87 | 91 | $phpcsFile->addError($error, $stackPtr); |
88 | 92 | } |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class LogicalNotSpacingSniff implements Sniff |
|
34 | -{ |
|
33 | +class LogicalNotSpacingSniff implements Sniff { |
|
35 | 34 | |
36 | 35 | /** |
37 | 36 | * Returns an array of tokens this test wants to listen for. |
@@ -63,7 +62,8 @@ discard block |
||
63 | 62 | |
64 | 63 | $previous_token = $tokens[$stackPtr - 1]; |
65 | 64 | $next_token = $tokens[$stackPtr + 1]; |
66 | - if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) { |
|
65 | + if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) |
|
66 | + { |
|
67 | 67 | $error = 'Logical operator ! should always be preceded and followed with a whitespace.'; |
68 | 68 | $phpcsFile->addError($error, $stackPtr); |
69 | 69 | } |
@@ -30,8 +30,7 @@ discard block |
||
30 | 30 | use PHP_CodeSniffer\Sniffs\Sniff; |
31 | 31 | use PHP_CodeSniffer\Files\File; |
32 | 32 | |
33 | -class DisallowSpaceIndentSniff implements Sniff |
|
34 | -{ |
|
33 | +class DisallowSpaceIndentSniff implements Sniff { |
|
35 | 34 | |
36 | 35 | /** |
37 | 36 | * A list of tokenizers this sniff supports. |
@@ -71,11 +70,13 @@ discard block |
||
71 | 70 | |
72 | 71 | // Make sure this is whitespace used for indentation. |
73 | 72 | $line = $tokens[$stackPtr]['line']; |
74 | - if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) { |
|
73 | + if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) |
|
74 | + { |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | |
78 | - if (strpos($tokens[$stackPtr]['content'], " ") !== false) { |
|
78 | + if (strpos($tokens[$stackPtr]['content'], " ") !== false) |
|
79 | + { |
|
79 | 80 | $error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation'; |
80 | 81 | $phpcsFile->addError($error, $stackPtr, 'SpacesUsedForIndentation'); |
81 | 82 | } |
@@ -35,8 +35,7 @@ discard block |
||
35 | 35 | * @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
36 | 36 | * @link http://pear.php.net/package/PHP_CodeSniffer |
37 | 37 | */ |
38 | -class VariableUsageSniff implements Sniff |
|
39 | -{ |
|
38 | +class VariableUsageSniff implements Sniff { |
|
40 | 39 | /** |
41 | 40 | * Returns an array of tokens this test wants to listen for. |
42 | 41 | * |
@@ -70,9 +69,12 @@ discard block |
||
70 | 69 | // makes sure that it is about a double quote string, |
71 | 70 | // since variables are not parsed out of double quoted string |
72 | 71 | $openDblQtStr = substr($string, 0, 1); |
73 | - if (0 === strcmp($openDblQtStr, '"')) { |
|
72 | + if (0 === strcmp($openDblQtStr, '"')) |
|
73 | + { |
|
74 | 74 | $this->processDoubleQuotedString($phpcsFile, $stackPtr, $string); |
75 | - } else if (0 === strcmp($openDblQtStr, "'")) { |
|
75 | + } |
|
76 | + else if (0 === strcmp($openDblQtStr, "'")) |
|
77 | + { |
|
76 | 78 | $this->processSingleQuotedString($phpcsFile, $stackPtr, $string); |
77 | 79 | } |
78 | 80 | }//end process() |
@@ -95,25 +97,34 @@ discard block |
||
95 | 97 | $strTokens = token_get_all('<?php '.$dblQtString); |
96 | 98 | $strPtr = 1; // skip php opening tag added by ourselves |
97 | 99 | $requireDblQuotes = FALSE; |
98 | - while ($strPtr < count($strTokens)) { |
|
100 | + while ($strPtr < count($strTokens)) |
|
101 | + { |
|
99 | 102 | $strToken = $strTokens[$strPtr]; |
100 | - if (is_array($strToken)) { |
|
101 | - if (in_array($strToken[0], array(T_DOLLAR_OPEN_CURLY_BRACES, T_CURLY_OPEN))) { |
|
103 | + if (is_array($strToken)) |
|
104 | + { |
|
105 | + if (in_array($strToken[0], array(T_DOLLAR_OPEN_CURLY_BRACES, T_CURLY_OPEN))) |
|
106 | + { |
|
102 | 107 | $strPtr++; |
103 | - try { |
|
108 | + try |
|
109 | + { |
|
104 | 110 | $this->_parseVariable($strTokens, $strPtr); |
105 | - } catch (Exception $err) { |
|
111 | + } |
|
112 | + catch (Exception $err) |
|
113 | + { |
|
106 | 114 | $error = 'There is no variable, object nor array between curly braces. Please use the escape char for $ or {.'; |
107 | 115 | $phpcsFile->addError($error, $stackPtr); |
108 | 116 | } |
109 | 117 | $variableFound = TRUE; |
110 | - if ('}' !== $strTokens[$strPtr]) { |
|
118 | + if ('}' !== $strTokens[$strPtr]) |
|
119 | + { |
|
111 | 120 | $error = 'There is no matching closing curly brace.'; |
112 | 121 | $phpcsFile->addError($error, $stackPtr); |
113 | 122 | } |
114 | 123 | // don't move forward, since it will be done in the main loop |
115 | 124 | // $strPtr++; |
116 | - } else if (T_VARIABLE === $strToken[0]) { |
|
125 | + } |
|
126 | + else if (T_VARIABLE === $strToken[0]) |
|
127 | + { |
|
117 | 128 | $variableFound = TRUE; |
118 | 129 | $error = "Variable {$strToken[1]} in double-quoted strings should be enclosed with curly braces. Please consider {{$strToken[1]}}"; |
119 | 130 | $phpcsFile->addError($error, $stackPtr); |
@@ -141,10 +152,13 @@ discard block |
||
141 | 152 | $variableFound = FALSE; |
142 | 153 | $strTokens = token_get_all('<?php '.$sglQtString); |
143 | 154 | $strPtr = 1; // skip php opening tag added by ourselves |
144 | - while ($strPtr < count($strTokens)) { |
|
155 | + while ($strPtr < count($strTokens)) |
|
156 | + { |
|
145 | 157 | $strToken = $strTokens[$strPtr]; |
146 | - if (is_array($strToken)) { |
|
147 | - if (T_VARIABLE === $strToken[0]) { |
|
158 | + if (is_array($strToken)) |
|
159 | + { |
|
160 | + if (T_VARIABLE === $strToken[0]) |
|
161 | + { |
|
148 | 162 | $error = "Variables like {$strToken[1]} should be in double-quoted strings only."; |
149 | 163 | $phpcsFile->addError($error, $stackPtr); |
150 | 164 | } |
@@ -173,25 +187,34 @@ discard block |
||
173 | 187 | */ |
174 | 188 | private function _parseVariable ($strTokens, &$strPtr) |
175 | 189 | { |
176 | - if ( ! in_array($strTokens[$strPtr][0], array(T_VARIABLE, T_STRING_VARNAME))) { |
|
190 | + if ( ! in_array($strTokens[$strPtr][0], array(T_VARIABLE, T_STRING_VARNAME))) |
|
191 | + { |
|
177 | 192 | throw new Exception ('Expected variable name.'); |
178 | 193 | } |
179 | 194 | $var = $strTokens[$strPtr][1]; |
180 | 195 | $strPtr++; |
181 | 196 | $startStrPtr = $strPtr; |
182 | - try { |
|
197 | + try |
|
198 | + { |
|
183 | 199 | $attr = $this->_parseObjectAttribute($strTokens, $strPtr); |
184 | 200 | return array ('obj' => $var, 'attr' => $attr); |
185 | - } catch (Exception $err) { |
|
186 | - if ($strPtr !== $startStrPtr) { |
|
201 | + } |
|
202 | + catch (Exception $err) |
|
203 | + { |
|
204 | + if ($strPtr !== $startStrPtr) |
|
205 | + { |
|
187 | 206 | throw $err; |
188 | 207 | } |
189 | 208 | } |
190 | - try { |
|
209 | + try |
|
210 | + { |
|
191 | 211 | $idx = $this->_parseArrayIndexes($strTokens, $strPtr); |
192 | 212 | return array ('arr' => $var, 'idx' => $idx); |
193 | - } catch (Exception $err) { |
|
194 | - if ($strPtr !== $startStrPtr) { |
|
213 | + } |
|
214 | + catch (Exception $err) |
|
215 | + { |
|
216 | + if ($strPtr !== $startStrPtr) |
|
217 | + { |
|
195 | 218 | throw $err; |
196 | 219 | } |
197 | 220 | } |
@@ -217,29 +240,39 @@ discard block |
||
217 | 240 | */ |
218 | 241 | private function _parseObjectAttribute ($strTokens, &$strPtr) |
219 | 242 | { |
220 | - if (T_OBJECT_OPERATOR !== $strTokens[$strPtr][0]) { |
|
243 | + if (T_OBJECT_OPERATOR !== $strTokens[$strPtr][0]) |
|
244 | + { |
|
221 | 245 | throw new Exception ('Expected ->.'); |
222 | 246 | } |
223 | 247 | $strPtr++; |
224 | - if (T_STRING !== $strTokens[$strPtr][0]) { |
|
248 | + if (T_STRING !== $strTokens[$strPtr][0]) |
|
249 | + { |
|
225 | 250 | throw new Exception ('Expected an object attribute.'); |
226 | 251 | } |
227 | 252 | $attr = $strTokens[$strPtr][1]; |
228 | 253 | $strPtr++; |
229 | 254 | $startStrPtr = $strPtr; |
230 | - try { |
|
255 | + try |
|
256 | + { |
|
231 | 257 | $sub_attr = $this->_parseObjectAttribute($strTokens, $strPtr); |
232 | 258 | return array ('obj' => $attr, 'attr' => $sub_attr); |
233 | - } catch (Exception $err) { |
|
234 | - if ($strPtr !== $startStrPtr) { |
|
259 | + } |
|
260 | + catch (Exception $err) |
|
261 | + { |
|
262 | + if ($strPtr !== $startStrPtr) |
|
263 | + { |
|
235 | 264 | throw $err; |
236 | 265 | } |
237 | 266 | } |
238 | - try { |
|
267 | + try |
|
268 | + { |
|
239 | 269 | $idx = $this->_parseArrayIndexes($strTokens, $strPtr); |
240 | 270 | return array ('arr' => $attr, 'idx' => $idx); |
241 | - } catch (Exception $err) { |
|
242 | - if ($strPtr !== $startStrPtr) { |
|
271 | + } |
|
272 | + catch (Exception $err) |
|
273 | + { |
|
274 | + if ($strPtr !== $startStrPtr) |
|
275 | + { |
|
243 | 276 | throw $err; |
244 | 277 | } |
245 | 278 | } |
@@ -263,13 +296,18 @@ discard block |
||
263 | 296 | private function _parseArrayIndexes ($strTokens, &$strPtr) |
264 | 297 | { |
265 | 298 | $indexes = array($this->_parseArrayIndex($strTokens, $strPtr)); |
266 | - try { |
|
267 | - while (1) { |
|
299 | + try |
|
300 | + { |
|
301 | + while (1) |
|
302 | + { |
|
268 | 303 | $startStrPtr = $strPtr; |
269 | 304 | $indexes [] = $this->_parseArrayIndex($strTokens, $strPtr); |
270 | 305 | } |
271 | - } catch (Exception $err) { |
|
272 | - if (0 !== ($strPtr - $startStrPtr)) { |
|
306 | + } |
|
307 | + catch (Exception $err) |
|
308 | + { |
|
309 | + if (0 !== ($strPtr - $startStrPtr)) |
|
310 | + { |
|
273 | 311 | throw $err; |
274 | 312 | } |
275 | 313 | return $indexes; |
@@ -292,16 +330,19 @@ discard block |
||
292 | 330 | */ |
293 | 331 | private function _parseArrayIndex ($strTokens, &$strPtr) |
294 | 332 | { |
295 | - if ('[' !== $strTokens[$strPtr]) { |
|
333 | + if ('[' !== $strTokens[$strPtr]) |
|
334 | + { |
|
296 | 335 | throw new Exception ('Expected [.'); |
297 | 336 | } |
298 | 337 | $strPtr++; |
299 | - if (! in_array($strTokens[$strPtr][0], array(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER))) { |
|
338 | + if (! in_array($strTokens[$strPtr][0], array(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER))) |
|
339 | + { |
|
300 | 340 | throw new Exception ('Expected an array index.'); |
301 | 341 | } |
302 | 342 | $index = $strTokens[$strPtr][1]; |
303 | 343 | $strPtr++; |
304 | - if (']' !== $strTokens[$strPtr]) { |
|
344 | + if (']' !== $strTokens[$strPtr]) |
|
345 | + { |
|
305 | 346 | throw new Exception ('Expected ].'); |
306 | 347 | } |
307 | 348 | $strPtr++; |
@@ -325,8 +366,7 @@ discard block |
||
325 | 366 | * @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
326 | 367 | * @link http://pear.php.net/package/PHP_CodeSniffer |
327 | 368 | */ |
328 | -class DoubleQuoteUsageSniff extends VariableUsageSniff |
|
329 | -{ |
|
369 | +class DoubleQuoteUsageSniff extends VariableUsageSniff { |
|
330 | 370 | /** |
331 | 371 | * Returns an array of tokens this test wants to listen for. |
332 | 372 | * |
@@ -361,9 +401,12 @@ discard block |
||
361 | 401 | // clean the enclosing quotes |
362 | 402 | $qtString = substr($qtString, 1, strlen($qtString) - 1 - 1); |
363 | 403 | |
364 | - if (0 === strcmp($open_qt_str, '"')) { |
|
404 | + if (0 === strcmp($open_qt_str, '"')) |
|
405 | + { |
|
365 | 406 | $this->processDoubleQuotedString($phpcsFile, $stackPtr, $qtString); |
366 | - } else if (0 === strcmp($open_qt_str, "'")) { |
|
407 | + } |
|
408 | + else if (0 === strcmp($open_qt_str, "'")) |
|
409 | + { |
|
367 | 410 | $this->processSingleQuotedString($phpcsFile, $stackPtr, $qtString); |
368 | 411 | } |
369 | 412 | }//end process() |
@@ -393,7 +436,8 @@ discard block |
||
393 | 436 | ) { |
394 | 437 | $error = 'Single-quoted strings should be used unless it contains variables, special chars like \n or single quotes.'; |
395 | 438 | $phpcsFile->addError($error, $stackPtr); |
396 | - } else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
439 | + } |
|
440 | + else if (false !== $smpl_qt_at && false !== $dbl_qt_at |
|
397 | 441 | && false === $has_variable && false === $has_specific_sequence |
398 | 442 | ) { |
399 | 443 | $warning = 'It is encouraged to use a single-quoted string, since it doesn\'t contain any variable nor special char though it mixes single and double quotes.'; |
@@ -423,7 +467,8 @@ discard block |
||
423 | 467 | $has_variable = parent::processSingleQuotedString($phpcsFile, $stackPtr, "'".$qtString."'"); |
424 | 468 | $dbl_qt_at = strpos($qtString, '"'); |
425 | 469 | $smpl_qt_at = strpos($qtString, "'"); |
426 | - if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) { |
|
470 | + if (false === $has_variable && false !== $smpl_qt_at && false === $dbl_qt_at) |
|
471 | + { |
|
427 | 472 | $warning = 'You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.'; |
428 | 473 | $phpcsFile->addWarning($warning, $stackPtr); |
429 | 474 | } |
@@ -445,14 +490,18 @@ discard block |
||
445 | 490 | { |
446 | 491 | $hasSpecificSequence = FALSE; |
447 | 492 | $specialMeaningStrs = array('\n', '\r', '\t', '\v', '\f'); |
448 | - foreach ($specialMeaningStrs as $splStr) { |
|
449 | - if (FALSE !== strpos($string, $splStr)) { |
|
493 | + foreach ($specialMeaningStrs as $splStr) |
|
494 | + { |
|
495 | + if (FALSE !== strpos($string, $splStr)) |
|
496 | + { |
|
450 | 497 | $hasSpecificSequence = TRUE; |
451 | 498 | } |
452 | 499 | } |
453 | 500 | $specialMeaningPtrns = array('\[0-7]{1,3}', '\x[0-9A-Fa-f]{1,2}'); |
454 | - foreach ($specialMeaningPtrns as $splPtrn) { |
|
455 | - if (1 === preg_match("/{$splPtrn}/", $string)) { |
|
501 | + foreach ($specialMeaningPtrns as $splPtrn) |
|
502 | + { |
|
503 | + if (1 === preg_match("/{$splPtrn}/", $string)) |
|
504 | + { |
|
456 | 505 | $hasSpecificSequence = TRUE; |
457 | 506 | } |
458 | 507 | } |