| 1 |
<?php |
| 2 |
/** |
| 3 |
* Basic util functions. |
| 4 |
* |
| 5 |
* @author Greg Sherwood <[email protected]> |
| 6 |
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 |
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace PHP_CodeSniffer\Util; |
| 11 |
|
| 12 |
class Common { |
| 13 |
|
| 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 |
]; |
| 30 |
|
| 31 |
|
| 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 |
} |
| 43 |
|
| 44 |
return false; |
| 45 |
|
| 46 |
}//end isPharFile() |
| 47 |
|
| 48 |
|
| 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 |
} |
| 66 |
|
| 67 |
// Check for process substitution. |
| 68 |
if ( strpos( $path, '/dev/fd' ) === 0 ) { |
| 69 |
return str_replace( '/dev/fd', 'php://fd', $path ); |
| 70 |
} |
| 71 |
|
| 72 |
// No extra work needed if this is not a phar file. |
| 73 |
if ( self::isPharFile( $path ) === false ) { |
| 74 |
return realpath( $path ); |
| 75 |
} |
| 76 |
|
| 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 |
} |
| 83 |
|
| 84 |
$phar = \Phar::running( false ); |
| 85 |
$extra = str_replace( 'phar://' . $phar, '', $path ); |
| 86 |
$path = realpath( $phar ); |
| 87 |
if ( $path === false ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
$path = 'phar://' . $path . $extra; |
| 92 |
if ( file_exists( $path ) === true ) { |
| 93 |
return $path; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
|
| 98 |
}//end realpath() |
| 99 |
|
| 100 |
|
| 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 |
} |
| 114 |
|
| 115 |
$basepathLen = strlen( $basepath ); |
| 116 |
if ( substr( $path, 0, $basepathLen ) === $basepath ) { |
| 117 |
$path = substr( $path, $basepathLen ); |
| 118 |
} |
| 119 |
|
| 120 |
$path = ltrim( $path, DIRECTORY_SEPARATOR ); |
| 121 |
if ( $path === '' ) { |
| 122 |
$path = '.'; |
| 123 |
} |
| 124 |
|
| 125 |
return $path; |
| 126 |
|
| 127 |
}//end stripBasepath() |
| 128 |
|
| 129 |
|
| 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 |
} |
| 144 |
|
| 145 |
return $eolChar; |
| 146 |
|
| 147 |
}//end detectLineEndings() |
| 148 |
|
| 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 |
} |
| 166 |
|
| 167 |
if ( in_array( "\n", $exclude, true ) === false ) { |
| 168 |
$content = str_replace( "\n", '\n', $content ); |
| 169 |
} |
| 170 |
|
| 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 |
} |
| 178 |
|
| 179 |
if ( in_array( "\n", $exclude, true ) === false ) { |
| 180 |
$content = str_replace( "\n", "\033[30;1m\\n\033[0m", $content ); |
| 181 |
} |
| 182 |
|
| 183 |
if ( in_array( "\t", $exclude, true ) === false ) { |
| 184 |
$content = str_replace( "\t", "\033[30;1m\\t\033[0m", $content ); |
| 185 |
} |
| 186 |
|
| 187 |
if ( in_array( ' ', $exclude, true ) === false ) { |
| 188 |
$content = str_replace( ' ', "\033[30;1m·\033[0m", $content ); |
| 189 |
} |
| 190 |
}//end if |
| 191 |
|
| 192 |
return $content; |
| 193 |
|
| 194 |
}//end prepareForOutput() |
| 195 |
|
| 196 |
|
| 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 |
} |
| 229 |
|
| 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 |
} |
| 240 |
|
| 241 |
if ( preg_match( "/^$legalFirstChar/", $string ) === 0 ) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 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 |
} |
| 250 |
|
| 251 |
if ( $strict === true ) { |
| 252 |
// Check that there are not two capital letters next to each other. |
| 253 |
$length = strlen( $string ); |
| 254 |
$lastCharWasCaps = $classFormat; |
| 255 |
|
| 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 |
} |
| 268 |
|
| 269 |
if ( $isCaps === true && $lastCharWasCaps === true ) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
$lastCharWasCaps = $isCaps; |
| 274 |
} |
| 275 |
}//end if |
| 276 |
|
| 277 |
return true; |
| 278 |
|
| 279 |
}//end isCamelCaps() |
| 280 |
|
| 281 |
|
| 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 |
} |
| 294 |
|
| 295 |
$validName = true; |
| 296 |
$nameBits = explode( '_', $string ); |
| 297 |
|
| 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 |
} |
| 306 |
|
| 307 |
if ( $bit[0] !== strtoupper( $bit[0] ) ) { |
| 308 |
$validName = false; |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return $validName; |
| 315 |
|
| 316 |
}//end isUnderscoreName() |
| 317 |
|
| 318 |
|
| 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 |
} |
| 333 |
|
| 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 |
| 353 |
|
| 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 |
} |
| 364 |
|
| 365 |
$type2 = ''; |
| 366 |
if ( isset( $matches[3] ) === true ) { |
| 367 |
$type2 = $matches[3]; |
| 368 |
} |
| 369 |
|
| 370 |
$type1 = self::suggestType( $type1 ); |
| 371 |
$type2 = self::suggestType( $type2 ); |
| 372 |
if ( $type2 !== '' ) { |
| 373 |
$type2 = ' => ' . $type2; |
| 374 |
} |
| 375 |
|
| 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 |
| 388 |
|
| 389 |
}//end suggestType() |
| 390 |
|
| 391 |
|
| 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 ); |
| 402 |
|
| 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 |
} |
| 410 |
|
| 411 |
$category = array_pop( $parts ); |
| 412 |
$sniffDir = array_pop( $parts ); |
| 413 |
$standard = array_pop( $parts ); |
| 414 |
$code = $standard . '.' . $category . '.' . $sniff; |
| 415 |
|
| 416 |
return $code; |
| 417 |
|
| 418 |
}//end getSniffCode() |
| 419 |
|
| 420 |
|
| 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 ); |
| 430 |
|
| 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 |
} |
| 436 |
|
| 437 |
$end = ( strlen( $newName ) - $sniffPos + 1 ); |
| 438 |
$start = strrpos( $newName, '\\', ( $end * - 1 ) ); |
| 439 |
|
| 440 |
if ( $start === false ) { |
| 441 |
// Nothing needs to be cleaned. |
| 442 |
return $newName; |
| 443 |
} |
| 444 |
|
| 445 |
$newName = substr( $newName, ( $start + 1 ) ); |
| 446 |
|
| 447 |
return $newName; |
| 448 |
|
| 449 |
}//end cleanSniffClass() |
| 450 |
|
| 451 |
|
| 452 |
}//end class |
| 453 |
|