| 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 |
/** |
| 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 |
]; |
| 31 |
|
| 32 |
|
| 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 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
|
| 48 |
}//end isPharFile() |
| 49 |
|
| 50 |
|
| 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 |
} |
| 69 |
|
| 70 |
// Check for process substitution. |
| 71 |
if (strpos($path, '/dev/fd') === 0) { |
| 72 |
return str_replace('/dev/fd', 'php://fd', $path); |
| 73 |
} |
| 74 |
|
| 75 |
// No extra work needed if this is not a phar file. |
| 76 |
if (self::isPharFile($path) === false) { |
| 77 |
return realpath($path); |
| 78 |
} |
| 79 |
|
| 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 |
} |
| 86 |
|
| 87 |
$phar = \Phar::running(false); |
| 88 |
$extra = str_replace('phar://'.$phar, '', $path); |
| 89 |
$path = realpath($phar); |
| 90 |
if ($path === false) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$path = 'phar://'.$path.$extra; |
| 95 |
if (file_exists($path) === true) { |
| 96 |
return $path; |
| 97 |
} |
| 98 |
|
| 99 |
return false; |
| 100 |
|
| 101 |
}//end realpath() |
| 102 |
|
| 103 |
|
| 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 |
} |
| 118 |
|
| 119 |
$basepathLen = strlen($basepath); |
| 120 |
if (substr($path, 0, $basepathLen) === $basepath) { |
| 121 |
$path = substr($path, $basepathLen); |
| 122 |
} |
| 123 |
|
| 124 |
$path = ltrim($path, DIRECTORY_SEPARATOR); |
| 125 |
if ($path === '') { |
| 126 |
$path = '.'; |
| 127 |
} |
| 128 |
|
| 129 |
return $path; |
| 130 |
|
| 131 |
}//end stripBasepath() |
| 132 |
|
| 133 |
|
| 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 |
} |
| 149 |
|
| 150 |
return $eolChar; |
| 151 |
|
| 152 |
}//end detectLineEndings() |
| 153 |
|
| 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 |
} |
| 172 |
|
| 173 |
if (in_array("\n", $exclude, true) === false) { |
| 174 |
$content = str_replace("\n", '\n', $content); |
| 175 |
} |
| 176 |
|
| 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 |
} |
| 184 |
|
| 185 |
if (in_array("\n", $exclude, true) === false) { |
| 186 |
$content = str_replace("\n", "\033[30;1m\\n\033[0m", $content); |
| 187 |
} |
| 188 |
|
| 189 |
if (in_array("\t", $exclude, true) === false) { |
| 190 |
$content = str_replace("\t", "\033[30;1m\\t\033[0m", $content); |
| 191 |
} |
| 192 |
|
| 193 |
if (in_array(' ', $exclude, true) === false) { |
| 194 |
$content = str_replace(' ', "\033[30;1m·\033[0m", $content); |
| 195 |
} |
| 196 |
}//end if |
| 197 |
|
| 198 |
return $content; |
| 199 |
|
| 200 |
}//end prepareForOutput() |
| 201 |
|
| 202 |
|
| 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 |
} |
| 235 |
|
| 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 |
} |
| 246 |
|
| 247 |
if (preg_match("/^$legalFirstChar/", $string) === 0) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 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 |
} |
| 256 |
|
| 257 |
if ($strict === true) { |
| 258 |
// Check that there are not two capital letters next to each other. |
| 259 |
$length = strlen($string); |
| 260 |
$lastCharWasCaps = $classFormat; |
| 261 |
|
| 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 |
} |
| 274 |
|
| 275 |
if ($isCaps === true && $lastCharWasCaps === true) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
$lastCharWasCaps = $isCaps; |
| 280 |
} |
| 281 |
}//end if |
| 282 |
|
| 283 |
return true; |
| 284 |
|
| 285 |
}//end isCamelCaps() |
| 286 |
|
| 287 |
|
| 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 |
} |
| 301 |
|
| 302 |
$validName = true; |
| 303 |
$nameBits = explode('_', $string); |
| 304 |
|
| 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 |
} |
| 313 |
|
| 314 |
if ($bit[0] !== strtoupper($bit[0])) { |
| 315 |
$validName = false; |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return $validName; |
| 322 |
|
| 323 |
}//end isUnderscoreName() |
| 324 |
|
| 325 |
|
| 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 |
} |
| 341 |
|
| 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 |
| 361 |
|
| 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 |
} |
| 372 |
|
| 373 |
$type2 = ''; |
| 374 |
if (isset($matches[3]) === true) { |
| 375 |
$type2 = $matches[3]; |
| 376 |
} |
| 377 |
|
| 378 |
$type1 = self::suggestType($type1); |
| 379 |
$type2 = self::suggestType($type2); |
| 380 |
if ($type2 !== '') { |
| 381 |
$type2 = ' => '.$type2; |
| 382 |
} |
| 383 |
|
| 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 |
| 396 |
|
| 397 |
}//end suggestType() |
| 398 |
|
| 399 |
|
| 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); |
| 411 |
|
| 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 |
} |
| 419 |
|
| 420 |
$category = array_pop($parts); |
| 421 |
$sniffDir = array_pop($parts); |
| 422 |
$standard = array_pop($parts); |
| 423 |
$code = $standard.'.'.$category.'.'.$sniff; |
| 424 |
return $code; |
| 425 |
|
| 426 |
}//end getSniffCode() |
| 427 |
|
| 428 |
|
| 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 |
|
| 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 |
} |
| 445 |
|
| 446 |
$end = (strlen($newName) - $sniffPos + 1); |
| 447 |
$start = strrpos($newName, '\\', ($end * -1)); |
| 448 |
|
| 449 |
if ($start === false) { |
| 450 |
// Nothing needs to be cleaned. |
| 451 |
return $newName; |
| 452 |
} |
| 453 |
|
| 454 |
$newName = substr($newName, ($start + 1)); |
| 455 |
return $newName; |
| 456 |
|
| 457 |
}//end cleanSniffClass() |
| 458 |
|
| 459 |
|
| 460 |
}//end class |
| 461 |
|