← All changes
|
src/extra/php-codesniffer-php-token/Util/Common.php
+368
-376
3.0.1
→
6.3.0
View file →
| @@ -8,453 +8,445 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace PHP_CodeSniffer\Util; |
| 11 | 11 | |
| 12 | -class Common | |
| 13 | -{ | |
| 12 | +class Common { | |
| 14 | 13 | |
| 15 | - /** | |
| 16 | - * An array of variable types for param/var we will check. | |
| 17 | - * | |
| 18 | - * @var string[] | |
| 19 | - */ | |
| 20 | - public static $allowedTypes = [ | |
| 21 | - 'array', | |
| 22 | - 'boolean', | |
| 23 | - 'float', | |
| 24 | - 'integer', | |
| 25 | - 'mixed', | |
| 26 | - 'object', | |
| 27 | - 'string', | |
| 28 | - 'resource', | |
| 29 | - 'callable', | |
| 30 | - ]; | |
| 14 | + /** | |
| 15 | + * An array of variable types for param/var we will check. | |
| 16 | + * | |
| 17 | + * @var string[] | |
| 18 | + */ | |
| 19 | + public static $allowedTypes = [ | |
| 20 | + 'array', | |
| 21 | + 'boolean', | |
| 22 | + 'float', | |
| 23 | + 'integer', | |
| 24 | + 'mixed', | |
| 25 | + 'object', | |
| 26 | + 'string', | |
| 27 | + 'resource', | |
| 28 | + 'callable', | |
| 29 | + ]; | |
| 31 | 30 | |
| 32 | 31 | |
| 33 | - /** | |
| 34 | - * Return TRUE if the path is a PHAR file. | |
| 35 | - * | |
| 36 | - * @param string $path The path to use. | |
| 37 | - * | |
| 38 | - * @return mixed | |
| 39 | - */ | |
| 40 | - public static function isPharFile($path) | |
| 41 | - { | |
| 42 | - if (strpos($path, 'phar://') === 0) { | |
| 43 | - return true; | |
| 44 | - } | |
| 32 | + /** | |
| 33 | + * Return TRUE if the path is a PHAR file. | |
| 34 | + * | |
| 35 | + * @param string $path The path to use. | |
| 36 | + * | |
| 37 | + * @return mixed | |
| 38 | + */ | |
| 39 | + public static function isPharFile( $path ) { | |
| 40 | + if ( strpos( $path, 'phar://' ) === 0 ) { | |
| 41 | + return true; | |
| 42 | + } | |
| 45 | 43 | |
| 46 | - return false; | |
| 44 | + return false; | |
| 47 | 45 | |
| 48 | - }//end isPharFile() | |
| 46 | + }//end isPharFile() | |
| 49 | 47 | |
| 50 | 48 | |
| 51 | - /** | |
| 52 | - * CodeSniffer alternative for realpath. | |
| 53 | - * | |
| 54 | - * Allows for PHAR support. | |
| 55 | - * | |
| 56 | - * @param string $path The path to use. | |
| 57 | - * | |
| 58 | - * @return mixed | |
| 59 | - */ | |
| 60 | - public static function realpath($path) | |
| 61 | - { | |
| 62 | - // Support the path replacement of ~ with the user's home directory. | |
| 63 | - if (substr($path, 0, 2) === '~/') { | |
| 64 | - $homeDir = getenv('HOME'); | |
| 65 | - if ($homeDir !== false) { | |
| 66 | - $path = $homeDir.substr($path, 1); | |
| 67 | - } | |
| 68 | - } | |
| 49 | + /** | |
| 50 | + * CodeSniffer alternative for realpath. | |
| 51 | + * | |
| 52 | + * Allows for PHAR support. | |
| 53 | + * | |
| 54 | + * @param string $path The path to use. | |
| 55 | + * | |
| 56 | + * @return mixed | |
| 57 | + */ | |
| 58 | + public static function realpath( $path ) { | |
| 59 | + // Support the path replacement of ~ with the user's home directory. | |
| 60 | + if ( substr( $path, 0, 2 ) === '~/' ) { | |
| 61 | + $homeDir = getenv( 'HOME' ); | |
| 62 | + if ( $homeDir !== false ) { | |
| 63 | + $path = $homeDir . substr( $path, 1 ); | |
| 64 | + } | |
| 65 | + } | |
| 69 | 66 | |
| 70 | - // Check for process substitution. | |
| 71 | - if (strpos($path, '/dev/fd') === 0) { | |
| 72 | - return str_replace('/dev/fd', 'php://fd', $path); | |
| 73 | - } | |
| 67 | + // Check for process substitution. | |
| 68 | + if ( strpos( $path, '/dev/fd' ) === 0 ) { | |
| 69 | + return str_replace( '/dev/fd', 'php://fd', $path ); | |
| 70 | + } | |
| 74 | 71 | |
| 75 | - // No extra work needed if this is not a phar file. | |
| 76 | - if (self::isPharFile($path) === false) { | |
| 77 | - return realpath($path); | |
| 78 | - } | |
| 72 | + // No extra work needed if this is not a phar file. | |
| 73 | + if ( self::isPharFile( $path ) === false ) { | |
| 74 | + return realpath( $path ); | |
| 75 | + } | |
| 79 | 76 | |
| 80 | - // Before trying to break down the file path, | |
| 81 | - // check if it exists first because it will mostly not | |
| 82 | - // change after running the below code. | |
| 83 | - if (file_exists($path) === true) { | |
| 84 | - return $path; | |
| 85 | - } | |
| 77 | + // Before trying to break down the file path, | |
| 78 | + // check if it exists first because it will mostly not | |
| 79 | + // change after running the below code. | |
| 80 | + if ( file_exists( $path ) === true ) { | |
| 81 | + return $path; | |
| 82 | + } | |
| 86 | 83 | |
| 87 | - $phar = \Phar::running(false); | |
| 88 | - $extra = str_replace('phar://'.$phar, '', $path); | |
| 89 | - $path = realpath($phar); | |
| 90 | - if ($path === false) { | |
| 91 | - return false; | |
| 92 | - } | |
| 84 | + $phar = \Phar::running( false ); | |
| 85 | + $extra = str_replace( 'phar://' . $phar, '', $path ); | |
| 86 | + $path = realpath( $phar ); | |
| 87 | + if ( $path === false ) { | |
| 88 | + return false; | |
| 89 | + } | |
| 93 | 90 | |
| 94 | - $path = 'phar://'.$path.$extra; | |
| 95 | - if (file_exists($path) === true) { | |
| 96 | - return $path; | |
| 97 | - } | |
| 91 | + $path = 'phar://' . $path . $extra; | |
| 92 | + if ( file_exists( $path ) === true ) { | |
| 93 | + return $path; | |
| 94 | + } | |
| 98 | 95 | |
| 99 | - return false; | |
| 96 | + return false; | |
| 100 | 97 | |
| 101 | - }//end realpath() | |
| 98 | + }//end realpath() | |
| 102 | 99 | |
| 103 | 100 | |
| 104 | - /** | |
| 105 | - * Removes a base path from the front of a file path. | |
| 106 | - * | |
| 107 | - * @param string $path The path of the file. | |
| 108 | - * @param string $basepath The base path to remove. This should not end | |
| 109 | - * with a directory separator. | |
| 110 | - * | |
| 111 | - * @return string | |
| 112 | - */ | |
| 113 | - public static function stripBasepath($path, $basepath) | |
| 114 | - { | |
| 115 | - if (empty($basepath) === true) { | |
| 116 | - return $path; | |
| 117 | - } | |
| 101 | + /** | |
| 102 | + * Removes a base path from the front of a file path. | |
| 103 | + * | |
| 104 | + * @param string $path The path of the file. | |
| 105 | + * @param string $basepath The base path to remove. This should not end | |
| 106 | + * with a directory separator. | |
| 107 | + * | |
| 108 | + * @return string | |
| 109 | + */ | |
| 110 | + public static function stripBasepath( $path, $basepath ) { | |
| 111 | + if ( empty( $basepath ) === true ) { | |
| 112 | + return $path; | |
| 113 | + } | |
| 118 | 114 | |
| 119 | - $basepathLen = strlen($basepath); | |
| 120 | - if (substr($path, 0, $basepathLen) === $basepath) { | |
| 121 | - $path = substr($path, $basepathLen); | |
| 122 | - } | |
| 115 | + $basepathLen = strlen( $basepath ); | |
| 116 | + if ( substr( $path, 0, $basepathLen ) === $basepath ) { | |
| 117 | + $path = substr( $path, $basepathLen ); | |
| 118 | + } | |
| 123 | 119 | |
| 124 | - $path = ltrim($path, DIRECTORY_SEPARATOR); | |
| 125 | - if ($path === '') { | |
| 126 | - $path = '.'; | |
| 127 | - } | |
| 120 | + $path = ltrim( $path, DIRECTORY_SEPARATOR ); | |
| 121 | + if ( $path === '' ) { | |
| 122 | + $path = '.'; | |
| 123 | + } | |
| 128 | 124 | |
| 129 | - return $path; | |
| 125 | + return $path; | |
| 130 | 126 | |
| 131 | - }//end stripBasepath() | |
| 127 | + }//end stripBasepath() | |
| 132 | 128 | |
| 133 | 129 | |
| 134 | - /** | |
| 135 | - * Detects the EOL character being used in a string. | |
| 136 | - * | |
| 137 | - * @param string $contents The contents to check. | |
| 138 | - * | |
| 139 | - * @return string | |
| 140 | - */ | |
| 141 | - public static function detectLineEndings($contents) | |
| 142 | - { | |
| 143 | - if (preg_match("/\r\n?|\n/", $contents, $matches) !== 1) { | |
| 144 | - // Assume there are no newlines. | |
| 145 | - $eolChar = "\n"; | |
| 146 | - } else { | |
| 147 | - $eolChar = $matches[0]; | |
| 148 | - } | |
| 130 | + /** | |
| 131 | + * Detects the EOL character being used in a string. | |
| 132 | + * | |
| 133 | + * @param string $contents The contents to check. | |
| 134 | + * | |
| 135 | + * @return string | |
| 136 | + */ | |
| 137 | + public static function detectLineEndings( $contents ) { | |
| 138 | + if ( preg_match( "/\r\n?|\n/", $contents, $matches ) !== 1 ) { | |
| 139 | + // Assume there are no newlines. | |
| 140 | + $eolChar = "\n"; | |
| 141 | + } else { | |
| 142 | + $eolChar = $matches[0]; | |
| 143 | + } | |
| 149 | 144 | |
| 150 | - return $eolChar; | |
| 145 | + return $eolChar; | |
| 151 | 146 | |
| 152 | - }//end detectLineEndings() | |
| 147 | + }//end detectLineEndings() | |
| 153 | 148 | |
| 154 | - /** | |
| 155 | - * Prepares token content for output to screen. | |
| 156 | - * | |
| 157 | - * Replaces invisible characters so they are visible. On non-Windows | |
| 158 | - * operating systems it will also colour the invisible characters. | |
| 159 | - * | |
| 160 | - * @param string $content The content to prepare. | |
| 161 | - * @param string[] $exclude A list of characters to leave invisible. | |
| 162 | - * Can contain \r, \n, \t and a space. | |
| 163 | - * | |
| 164 | - * @return string | |
| 165 | - */ | |
| 166 | - public static function prepareForOutput($content, $exclude=[]) | |
| 167 | - { | |
| 168 | - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
| 169 | - if (in_array("\r", $exclude, true) === false) { | |
| 170 | - $content = str_replace("\r", '\r', $content); | |
| 171 | - } | |
| 149 | + /** | |
| 150 | + * Prepares token content for output to screen. | |
| 151 | + * | |
| 152 | + * Replaces invisible characters so they are visible. On non-Windows | |
| 153 | + * operating systems it will also colour the invisible characters. | |
| 154 | + * | |
| 155 | + * @param string $content The content to prepare. | |
| 156 | + * @param string[] $exclude A list of characters to leave invisible. | |
| 157 | + * Can contain \r, \n, \t and a space. | |
| 158 | + * | |
| 159 | + * @return string | |
| 160 | + */ | |
| 161 | + public static function prepareForOutput( $content, $exclude = [] ) { | |
| 162 | + if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) { | |
| 163 | + if ( in_array( "\r", $exclude, true ) === false ) { | |
| 164 | + $content = str_replace( "\r", '\r', $content ); | |
| 165 | + } | |
| 172 | 166 | |
| 173 | - if (in_array("\n", $exclude, true) === false) { | |
| 174 | - $content = str_replace("\n", '\n', $content); | |
| 175 | - } | |
| 167 | + if ( in_array( "\n", $exclude, true ) === false ) { | |
| 168 | + $content = str_replace( "\n", '\n', $content ); | |
| 169 | + } | |
| 176 | 170 | |
| 177 | - if (in_array("\t", $exclude, true) === false) { | |
| 178 | - $content = str_replace("\t", '\t', $content); | |
| 179 | - } | |
| 180 | - } else { | |
| 181 | - if (in_array("\r", $exclude, true) === false) { | |
| 182 | - $content = str_replace("\r", "\033[30;1m\\r\033[0m", $content); | |
| 183 | - } | |
| 171 | + if ( in_array( "\t", $exclude, true ) === false ) { | |
| 172 | + $content = str_replace( "\t", '\t', $content ); | |
| 173 | + } | |
| 174 | + } else { | |
| 175 | + if ( in_array( "\r", $exclude, true ) === false ) { | |
| 176 | + $content = str_replace( "\r", "\033[30;1m\\r\033[0m", $content ); | |
| 177 | + } | |
| 184 | 178 | |
| 185 | - if (in_array("\n", $exclude, true) === false) { | |
| 186 | - $content = str_replace("\n", "\033[30;1m\\n\033[0m", $content); | |
| 187 | - } | |
| 179 | + if ( in_array( "\n", $exclude, true ) === false ) { | |
| 180 | + $content = str_replace( "\n", "\033[30;1m\\n\033[0m", $content ); | |
| 181 | + } | |
| 188 | 182 | |
| 189 | - if (in_array("\t", $exclude, true) === false) { | |
| 190 | - $content = str_replace("\t", "\033[30;1m\\t\033[0m", $content); | |
| 191 | - } | |
| 183 | + if ( in_array( "\t", $exclude, true ) === false ) { | |
| 184 | + $content = str_replace( "\t", "\033[30;1m\\t\033[0m", $content ); | |
| 185 | + } | |
| 192 | 186 | |
| 193 | - if (in_array(' ', $exclude, true) === false) { | |
| 194 | - $content = str_replace(' ', "\033[30;1m·\033[0m", $content); | |
| 195 | - } | |
| 196 | - }//end if | |
| 187 | + if ( in_array( ' ', $exclude, true ) === false ) { | |
| 188 | + $content = str_replace( ' ', "\033[30;1m·\033[0m", $content ); | |
| 189 | + } | |
| 190 | + }//end if | |
| 197 | 191 | |
| 198 | - return $content; | |
| 192 | + return $content; | |
| 199 | 193 | |
| 200 | - }//end prepareForOutput() | |
| 194 | + }//end prepareForOutput() | |
| 201 | 195 | |
| 202 | 196 | |
| 203 | - /** | |
| 204 | - * Returns true if the specified string is in the camel caps format. | |
| 205 | - * | |
| 206 | - * @param string $string The string the verify. | |
| 207 | - * @param boolean $classFormat If true, check to see if the string is in the | |
| 208 | - * class format. Class format strings must start | |
| 209 | - * with a capital letter and contain no | |
| 210 | - * underscores. | |
| 211 | - * @param boolean $public If true, the first character in the string | |
| 212 | - * must be an a-z character. If false, the | |
| 213 | - * character must be an underscore. This | |
| 214 | - * argument is only applicable if $classFormat | |
| 215 | - * is false. | |
| 216 | - * @param boolean $strict If true, the string must not have two capital | |
| 217 | - * letters next to each other. If false, a | |
| 218 | - * relaxed camel caps policy is used to allow | |
| 219 | - * for acronyms. | |
| 220 | - * | |
| 221 | - * @return boolean | |
| 222 | - */ | |
| 223 | - public static function isCamelCaps( | |
| 224 | - $string, | |
| 225 | - $classFormat=false, | |
| 226 | - $public=true, | |
| 227 | - $strict=true | |
| 228 | - ) { | |
| 229 | - // Check the first character first. | |
| 230 | - if ($classFormat === false) { | |
| 231 | - $legalFirstChar = ''; | |
| 232 | - if ($public === false) { | |
| 233 | - $legalFirstChar = '[_]'; | |
| 234 | - } | |
| 197 | + /** | |
| 198 | + * Returns true if the specified string is in the camel caps format. | |
| 199 | + * | |
| 200 | + * @param string $string The string the verify. | |
| 201 | + * @param boolean $classFormat If true, check to see if the string is in the | |
| 202 | + * class format. Class format strings must start | |
| 203 | + * with a capital letter and contain no | |
| 204 | + * underscores. | |
| 205 | + * @param boolean $public If true, the first character in the string | |
| 206 | + * must be an a-z character. If false, the | |
| 207 | + * character must be an underscore. This | |
| 208 | + * argument is only applicable if $classFormat | |
| 209 | + * is false. | |
| 210 | + * @param boolean $strict If true, the string must not have two capital | |
| 211 | + * letters next to each other. If false, a | |
| 212 | + * relaxed camel caps policy is used to allow | |
| 213 | + * for acronyms. | |
| 214 | + * | |
| 215 | + * @return boolean | |
| 216 | + */ | |
| 217 | + public static function isCamelCaps( | |
| 218 | + $string, | |
| 219 | + $classFormat = false, | |
| 220 | + $public = true, | |
| 221 | + $strict = true | |
| 222 | + ) { | |
| 223 | + // Check the first character first. | |
| 224 | + if ( $classFormat === false ) { | |
| 225 | + $legalFirstChar = ''; | |
| 226 | + if ( $public === false ) { | |
| 227 | + $legalFirstChar = '[_]'; | |
| 228 | + } | |
| 235 | 229 | |
| 236 | - if ($strict === false) { | |
| 237 | - // Can either start with a lowercase letter, or multiple uppercase | |
| 238 | - // in a row, representing an acronym. | |
| 239 | - $legalFirstChar .= '([A-Z]{2,}|[a-z])'; | |
| 240 | - } else { | |
| 241 | - $legalFirstChar .= '[a-z]'; | |
| 242 | - } | |
| 243 | - } else { | |
| 244 | - $legalFirstChar = '[A-Z]'; | |
| 245 | - } | |
| 230 | + if ( $strict === false ) { | |
| 231 | + // Can either start with a lowercase letter, or multiple uppercase | |
| 232 | + // in a row, representing an acronym. | |
| 233 | + $legalFirstChar .= '([A-Z]{2,}|[a-z])'; | |
| 234 | + } else { | |
| 235 | + $legalFirstChar .= '[a-z]'; | |
| 236 | + } | |
| 237 | + } else { | |
| 238 | + $legalFirstChar = '[A-Z]'; | |
| 239 | + } | |
| 246 | 240 | |
| 247 | - if (preg_match("/^$legalFirstChar/", $string) === 0) { | |
| 248 | - return false; | |
| 249 | - } | |
| 241 | + if ( preg_match( "/^$legalFirstChar/", $string ) === 0 ) { | |
| 242 | + return false; | |
| 243 | + } | |
| 250 | 244 | |
| 251 | - // Check that the name only contains legal characters. | |
| 252 | - $legalChars = 'a-zA-Z0-9'; | |
| 253 | - if (preg_match("|[^$legalChars]|", substr($string, 1)) > 0) { | |
| 254 | - return false; | |
| 255 | - } | |
| 245 | + // Check that the name only contains legal characters. | |
| 246 | + $legalChars = 'a-zA-Z0-9'; | |
| 247 | + if ( preg_match( "|[^$legalChars]|", substr( $string, 1 ) ) > 0 ) { | |
| 248 | + return false; | |
| 249 | + } | |
| 256 | 250 | |
| 257 | - if ($strict === true) { | |
| 258 | - // Check that there are not two capital letters next to each other. | |
| 259 | - $length = strlen($string); | |
| 260 | - $lastCharWasCaps = $classFormat; | |
| 251 | + if ( $strict === true ) { | |
| 252 | + // Check that there are not two capital letters next to each other. | |
| 253 | + $length = strlen( $string ); | |
| 254 | + $lastCharWasCaps = $classFormat; | |
| 261 | 255 | |
| 262 | - for ($i = 1; $i < $length; $i++) { | |
| 263 | - $ascii = ord($string[$i]); | |
| 264 | - if ($ascii >= 48 && $ascii <= 57) { | |
| 265 | - // The character is a number, so it cant be a capital. | |
| 266 | - $isCaps = false; | |
| 267 | - } else { | |
| 268 | - if (strtoupper($string[$i]) === $string[$i]) { | |
| 269 | - $isCaps = true; | |
| 270 | - } else { | |
| 271 | - $isCaps = false; | |
| 272 | - } | |
| 273 | - } | |
| 256 | + for ( $i = 1; $i < $length; $i ++ ) { | |
| 257 | + $ascii = ord( $string[ $i ] ); | |
| 258 | + if ( $ascii >= 48 && $ascii <= 57 ) { | |
| 259 | + // The character is a number, so it cant be a capital. | |
| 260 | + $isCaps = false; | |
| 261 | + } else { | |
| 262 | + if ( strtoupper( $string[ $i ] ) === $string[ $i ] ) { | |
| 263 | + $isCaps = true; | |
| 264 | + } else { | |
| 265 | + $isCaps = false; | |
| 266 | + } | |
| 267 | + } | |
| 274 | 268 | |
| 275 | - if ($isCaps === true && $lastCharWasCaps === true) { | |
| 276 | - return false; | |
| 277 | - } | |
| 269 | + if ( $isCaps === true && $lastCharWasCaps === true ) { | |
| 270 | + return false; | |
| 271 | + } | |
| 278 | 272 | |
| 279 | - $lastCharWasCaps = $isCaps; | |
| 280 | - } | |
| 281 | - }//end if | |
| 273 | + $lastCharWasCaps = $isCaps; | |
| 274 | + } | |
| 275 | + }//end if | |
| 282 | 276 | |
| 283 | - return true; | |
| 277 | + return true; | |
| 284 | 278 | |
| 285 | - }//end isCamelCaps() | |
| 279 | + }//end isCamelCaps() | |
| 286 | 280 | |
| 287 | 281 | |
| 288 | - /** | |
| 289 | - * Returns true if the specified string is in the underscore caps format. | |
| 290 | - * | |
| 291 | - * @param string $string The string to verify. | |
| 292 | - * | |
| 293 | - * @return boolean | |
| 294 | - */ | |
| 295 | - public static function isUnderscoreName($string) | |
| 296 | - { | |
| 297 | - // If there are space in the name, it can't be valid. | |
| 298 | - if (strpos($string, ' ') !== false) { | |
| 299 | - return false; | |
| 300 | - } | |
| 282 | + /** | |
| 283 | + * Returns true if the specified string is in the underscore caps format. | |
| 284 | + * | |
| 285 | + * @param string $string The string to verify. | |
| 286 | + * | |
| 287 | + * @return boolean | |
| 288 | + */ | |
| 289 | + public static function isUnderscoreName( $string ) { | |
| 290 | + // If there are space in the name, it can't be valid. | |
| 291 | + if ( strpos( $string, ' ' ) !== false ) { | |
| 292 | + return false; | |
| 293 | + } | |
| 301 | 294 | |
| 302 | - $validName = true; | |
| 303 | - $nameBits = explode('_', $string); | |
| 295 | + $validName = true; | |
| 296 | + $nameBits = explode( '_', $string ); | |
| 304 | 297 | |
| 305 | - if (preg_match('|^[A-Z]|', $string) === 0) { | |
| 306 | - // Name does not begin with a capital letter. | |
| 307 | - $validName = false; | |
| 308 | - } else { | |
| 309 | - foreach ($nameBits as $bit) { | |
| 310 | - if ($bit === '') { | |
| 311 | - continue; | |
| 312 | - } | |
| 298 | + if ( preg_match( '|^[A-Z]|', $string ) === 0 ) { | |
| 299 | + // Name does not begin with a capital letter. | |
| 300 | + $validName = false; | |
| 301 | + } else { | |
| 302 | + foreach ( $nameBits as $bit ) { | |
| 303 | + if ( $bit === '' ) { | |
| 304 | + continue; | |
| 305 | + } | |
| 313 | 306 | |
| 314 | - if ($bit[0] !== strtoupper($bit[0])) { | |
| 315 | - $validName = false; | |
| 316 | - break; | |
| 317 | - } | |
| 318 | - } | |
| 319 | - } | |
| 307 | + if ( $bit[0] !== strtoupper( $bit[0] ) ) { | |
| 308 | + $validName = false; | |
| 309 | + break; | |
| 310 | + } | |
| 311 | + } | |
| 312 | + } | |
| 320 | 313 | |
| 321 | - return $validName; | |
| 314 | + return $validName; | |
| 322 | 315 | |
| 323 | - }//end isUnderscoreName() | |
| 316 | + }//end isUnderscoreName() | |
| 324 | 317 | |
| 325 | 318 | |
| 326 | - /** | |
| 327 | - * Returns a valid variable type for param/var tags. | |
| 328 | - * | |
| 329 | - * If type is not one of the standard types, it must be a custom type. | |
| 330 | - * Returns the correct type name suggestion if type name is invalid. | |
| 331 | - * | |
| 332 | - * @param string $varType The variable type to process. | |
| 333 | - * | |
| 334 | - * @return string | |
| 335 | - */ | |
| 336 | - public static function suggestType($varType) | |
| 337 | - { | |
| 338 | - if ($varType === '') { | |
| 339 | - return ''; | |
| 340 | - } | |
| 319 | + /** | |
| 320 | + * Returns a valid variable type for param/var tags. | |
| 321 | + * | |
| 322 | + * If type is not one of the standard types, it must be a custom type. | |
| 323 | + * Returns the correct type name suggestion if type name is invalid. | |
| 324 | + * | |
| 325 | + * @param string $varType The variable type to process. | |
| 326 | + * | |
| 327 | + * @return string | |
| 328 | + */ | |
| 329 | + public static function suggestType( $varType ) { | |
| 330 | + if ( $varType === '' ) { | |
| 331 | + return ''; | |
| 332 | + } | |
| 341 | 333 | |
| 342 | - if (in_array($varType, self::$allowedTypes, true) === true) { | |
| 343 | - return $varType; | |
| 344 | - } else { | |
| 345 | - $lowerVarType = strtolower($varType); | |
| 346 | - switch ($lowerVarType) { | |
| 347 | - case 'bool': | |
| 348 | - case 'boolean': | |
| 349 | - return 'boolean'; | |
| 350 | - case 'double': | |
| 351 | - case 'real': | |
| 352 | - case 'float': | |
| 353 | - return 'float'; | |
| 354 | - case 'int': | |
| 355 | - case 'integer': | |
| 356 | - return 'integer'; | |
| 357 | - case 'array()': | |
| 358 | - case 'array': | |
| 359 | - return 'array'; | |
| 360 | - }//end switch | |
| 334 | + if ( in_array( $varType, self::$allowedTypes, true ) === true ) { | |
| 335 | + return $varType; | |
| 336 | + } else { | |
| 337 | + $lowerVarType = strtolower( $varType ); | |
| 338 | + switch ( $lowerVarType ) { | |
| 339 | + case 'bool': | |
| 340 | + case 'boolean': | |
| 341 | + return 'boolean'; | |
| 342 | + case 'double': | |
| 343 | + case 'real': | |
| 344 | + case 'float': | |
| 345 | + return 'float'; | |
| 346 | + case 'int': | |
| 347 | + case 'integer': | |
| 348 | + return 'integer'; | |
| 349 | + case 'array()': | |
| 350 | + case 'array': | |
| 351 | + return 'array'; | |
| 352 | + }//end switch | |
| 361 | 353 | |
| 362 | - if (strpos($lowerVarType, 'array(') !== false) { | |
| 363 | - // Valid array declaration: | |
| 364 | - // array, array(type), array(type1 => type2). | |
| 365 | - $matches = []; | |
| 366 | - $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; | |
| 367 | - if (preg_match($pattern, $varType, $matches) !== 0) { | |
| 368 | - $type1 = ''; | |
| 369 | - if (isset($matches[1]) === true) { | |
| 370 | - $type1 = $matches[1]; | |
| 371 | - } | |
| 354 | + if ( strpos( $lowerVarType, 'array(' ) !== false ) { | |
| 355 | + // Valid array declaration: | |
| 356 | + // array, array(type), array(type1 => type2). | |
| 357 | + $matches = []; | |
| 358 | + $pattern = '/^array\(\s*([^\s^=^>]*)(\s*=>\s*(.*))?\s*\)/i'; | |
| 359 | + if ( preg_match( $pattern, $varType, $matches ) !== 0 ) { | |
| 360 | + $type1 = ''; | |
| 361 | + if ( isset( $matches[1] ) === true ) { | |
| 362 | + $type1 = $matches[1]; | |
| 363 | + } | |
| 372 | 364 | |
| 373 | - $type2 = ''; | |
| 374 | - if (isset($matches[3]) === true) { | |
| 375 | - $type2 = $matches[3]; | |
| 376 | - } | |
| 365 | + $type2 = ''; | |
| 366 | + if ( isset( $matches[3] ) === true ) { | |
| 367 | + $type2 = $matches[3]; | |
| 368 | + } | |
| 377 | 369 | |
| 378 | - $type1 = self::suggestType($type1); | |
| 379 | - $type2 = self::suggestType($type2); | |
| 380 | - if ($type2 !== '') { | |
| 381 | - $type2 = ' => '.$type2; | |
| 382 | - } | |
| 370 | + $type1 = self::suggestType( $type1 ); | |
| 371 | + $type2 = self::suggestType( $type2 ); | |
| 372 | + if ( $type2 !== '' ) { | |
| 373 | + $type2 = ' => ' . $type2; | |
| 374 | + } | |
| 383 | 375 | |
| 384 | - return "array($type1$type2)"; | |
| 385 | - } else { | |
| 386 | - return 'array'; | |
| 387 | - }//end if | |
| 388 | - } else if (in_array($lowerVarType, self::$allowedTypes, true) === true) { | |
| 389 | - // A valid type, but not lower cased. | |
| 390 | - return $lowerVarType; | |
| 391 | - } else { | |
| 392 | - // Must be a custom type name. | |
| 393 | - return $varType; | |
| 394 | - }//end if | |
| 395 | - }//end if | |
| 376 | + return "array($type1$type2)"; | |
| 377 | + } else { | |
| 378 | + return 'array'; | |
| 379 | + }//end if | |
| 380 | + } else if ( in_array( $lowerVarType, self::$allowedTypes, true ) === true ) { | |
| 381 | + // A valid type, but not lower cased. | |
| 382 | + return $lowerVarType; | |
| 383 | + } else { | |
| 384 | + // Must be a custom type name. | |
| 385 | + return $varType; | |
| 386 | + }//end if | |
| 387 | + }//end if | |
| 396 | 388 | |
| 397 | - }//end suggestType() | |
| 389 | + }//end suggestType() | |
| 398 | 390 | |
| 399 | 391 | |
| 400 | - /** | |
| 401 | - * Given a sniff class name, returns the code for the sniff. | |
| 402 | - * | |
| 403 | - * @param string $sniffClass The fully qualified sniff class name. | |
| 404 | - * | |
| 405 | - * @return string | |
| 406 | - */ | |
| 407 | - public static function getSniffCode($sniffClass) | |
| 408 | - { | |
| 409 | - $parts = explode('\\', $sniffClass); | |
| 410 | - $sniff = array_pop($parts); | |
| 392 | + /** | |
| 393 | + * Given a sniff class name, returns the code for the sniff. | |
| 394 | + * | |
| 395 | + * @param string $sniffClass The fully qualified sniff class name. | |
| 396 | + * | |
| 397 | + * @return string | |
| 398 | + */ | |
| 399 | + public static function getSniffCode( $sniffClass ) { | |
| 400 | + $parts = explode( '\\', $sniffClass ); | |
| 401 | + $sniff = array_pop( $parts ); | |
| 411 | 402 | |
| 412 | - if (substr($sniff, -5) === 'Sniff') { | |
| 413 | - // Sniff class name. | |
| 414 | - $sniff = substr($sniff, 0, -5); | |
| 415 | - } else { | |
| 416 | - // Unit test class name. | |
| 417 | - $sniff = substr($sniff, 0, -8); | |
| 418 | - } | |
| 403 | + if ( substr( $sniff, - 5 ) === 'Sniff' ) { | |
| 404 | + // Sniff class name. | |
| 405 | + $sniff = substr( $sniff, 0, - 5 ); | |
| 406 | + } else { | |
| 407 | + // Unit test class name. | |
| 408 | + $sniff = substr( $sniff, 0, - 8 ); | |
| 409 | + } | |
| 419 | 410 | |
| 420 | - $category = array_pop($parts); | |
| 421 | - $sniffDir = array_pop($parts); | |
| 422 | - $standard = array_pop($parts); | |
| 423 | - $code = $standard.'.'.$category.'.'.$sniff; | |
| 424 | - return $code; | |
| 411 | + $category = array_pop( $parts ); | |
| 412 | + $sniffDir = array_pop( $parts ); | |
| 413 | + $standard = array_pop( $parts ); | |
| 414 | + $code = $standard . '.' . $category . '.' . $sniff; | |
| 425 | 415 | |
| 426 | - }//end getSniffCode() | |
| 416 | + return $code; | |
| 427 | 417 | |
| 418 | + }//end getSniffCode() | |
| 428 | 419 | |
| 429 | - /** | |
| 430 | - * Removes project-specific information from a sniff class name. | |
| 431 | - * | |
| 432 | - * @param string $sniffClass The fully qualified sniff class name. | |
| 433 | - * | |
| 434 | - * @return string | |
| 435 | - */ | |
| 436 | - public static function cleanSniffClass($sniffClass) | |
| 437 | - { | |
| 438 | - $newName = strtolower($sniffClass); | |
| 439 | 420 | |
| 440 | - $sniffPos = strrpos($newName, '\sniffs\\'); | |
| 441 | - if ($sniffPos === false) { | |
| 442 | - // Nothing we can do as it isn't in a known format. | |
| 443 | - return $newName; | |
| 444 | - } | |
| 421 | + /** | |
| 422 | + * Removes project-specific information from a sniff class name. | |
| 423 | + * | |
| 424 | + * @param string $sniffClass The fully qualified sniff class name. | |
| 425 | + * | |
| 426 | + * @return string | |
| 427 | + */ | |
| 428 | + public static function cleanSniffClass( $sniffClass ) { | |
| 429 | + $newName = strtolower( $sniffClass ); | |
| 445 | 430 | |
| 446 | - $end = (strlen($newName) - $sniffPos + 1); | |
| 447 | - $start = strrpos($newName, '\\', ($end * -1)); | |
| 431 | + $sniffPos = strrpos( $newName, '\sniffs\\' ); | |
| 432 | + if ( $sniffPos === false ) { | |
| 433 | + // Nothing we can do as it isn't in a known format. | |
| 434 | + return $newName; | |
| 435 | + } | |
| 448 | 436 | |
| 449 | - if ($start === false) { | |
| 450 | - // Nothing needs to be cleaned. | |
| 451 | - return $newName; | |
| 452 | - } | |
| 437 | + $end = ( strlen( $newName ) - $sniffPos + 1 ); | |
| 438 | + $start = strrpos( $newName, '\\', ( $end * - 1 ) ); | |
| 453 | 439 | |
| 454 | - $newName = substr($newName, ($start + 1)); | |
| 455 | - return $newName; | |
| 440 | + if ( $start === false ) { | |
| 441 | + // Nothing needs to be cleaned. | |
| 442 | + return $newName; | |
| 443 | + } | |
| 456 | 444 | |
| 457 | - }//end cleanSniffClass() | |
| 445 | + $newName = substr( $newName, ( $start + 1 ) ); | |
| 446 | + | |
| 447 | + return $newName; | |
| 448 | + | |
| 449 | + }//end cleanSniffClass() | |
| 458 | 450 | |
| 459 | 451 | |
| 460 | 452 | }//end class |