Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.