Last active
September 18, 2020 17:34
-
-
Save jmkoni/0afb692e93c57ca39e2efa43a1c4a600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
{ | |
$predefinedVariables = [ | |
'$GLOBALS', | |
'$_SERVER', | |
'$_GET', | |
'$_POST', | |
'$_FILES', | |
'$_REQUEST', | |
'$_SESSION', | |
'$_ENV', | |
'$_COOKIE', | |
'$php_errormsg', | |
'$HTTP_RAW_POST_DATA', | |
'$http_response_header', | |
'$argc', | |
'$argv' | |
]; | |
foreach ($tokens as $index => $token) { | |
if ((T_VARIABLE === $token->getId()) || (T_STRING_VARNAME === $token->getId())) { | |
if (in_array($token->getContent(), $predefinedVariables)) { | |
continue; | |
} | |
$tokens[$index] = new Token([$token->getId(), $this->updateVariableCasing($token->getContent())]); | |
} | |
} | |
} | |
private function updateVariableCasing($variableName) | |
{ | |
if (self::CAMEL_CASE === $this->configuration['case']) { | |
return $this->camelCase($variableName); | |
} | |
return $this->snakeCase($variableName); | |
} | |
private function camelCase($string) | |
{ | |
if(strtoupper($string) == $string){ | |
return $string; | |
} | |
$string = Preg::replace('/_/i', ' ', $string); | |
$string = trim($string); | |
// uppercase the first character of each word | |
$string = ucwords($string); | |
$string = str_replace(' ', '', $string); | |
return lcfirst($string); | |
} | |
private function snakeCase($string, $separator = '_') | |
{ | |
if(strtoupper($string) == $string){ | |
return $string; | |
} | |
if(strtolower($string) == $string){ | |
return $string; | |
} | |
// insert separator between any letter and the beginning of a numeric chain | |
$string = Preg::replace('/([a-z]+)([0-9]+)/i', '$1'.$separator.'$2', $string); | |
// insert separator between any lower-to-upper-case letter chain | |
$string = Preg::replace('/([a-z]+)([A-Z]+)/', '$1'.$separator.'$2', $string); | |
// insert separator between the end of a numeric chain and the beginning of an alpha chain | |
$string = Preg::replace('/([0-9]+)([a-z]+)/i', '$1'.$separator.'$2', $string); | |
// Lowercase | |
return strtolower($string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment