-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Introduce variable_case
fixer
#5097
base: master
Are you sure you want to change the base?
Conversation
9ec1090
to
d9f3728
Compare
This fixer should be marked as risky. It will break compact/require/include. |
Also, we are missing test cases with string variable interpolation? |
@GrahamCampbell do you mean a test like this:
|
Yes, and |
Sadly no. PHP CS Fixer works on one file at a time and isn't able to detect that a change in that file will have any impact on other files. Users have to check for breaking changes after running risky rules. You may even have cases where a break is introduced inside the same file: $some_variable = 'foo';
$name = 'some_variable';
echo $$name; // prints "foo" would become: $someVariable = 'foo';
$name = 'some_variable';
echo $$name; // Undefined variable $some_variable PHP CS Fixer does not really have advanced analysis capabilities and detecting this would be too much complicated to implement here IMO. Maybe tools like PHPStan or Psalm can detect it anyway. |
Even then, this kind of fixer is inherently risky, like I said earlier. It cannot fix the following correctly: function f($bar_baz, $file)
{
require $file;
} |
8d17658
to
c9807e1
Compare
I updated this, marked it as risky, added some more tests. |
Thanks @jmkoni for your work on this PR. We are trying to add this as a custom rule via mll-lab/php-cs-fixer-config#3 and stumbled upon some edge cases: $_ = 2;
var_dump($_ENV); Thus, i modified the camel case fixer to preserve leading underscores and exclude globals. private function camelCase(string $string): string
{
$leadingDollar = '';
if ('$' === $string[0]) {
$string = substr($string, 1);
$leadingDollar = '$';
}
// Preserve leading underscores
$leadingUnderscoreMatches = [];
Preg::match('/^_*/', $string, $leadingUnderscoreMatches);
$leadingUnderscores = $leadingUnderscoreMatches[0] ?? '';
if (array_key_exists($string, $GLOBALS)) {
return $leadingDollar . $string;
}
$string = Preg::replace('/[$_]/i', ' ', $string);
$string = trim($string);
// uppercase the first character of each word
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return $leadingDollar . $leadingUnderscores . lcfirst($string);
} |
This also converts property names, which may not be desired. To avoid that I changed the /**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if ((T_VARIABLE === $token->getId()) || (T_STRING_VARNAME === $token->getId())) {
$prev_index = $tokens->getPrevMeaningfulToken($index);
$prev_token = $tokens[$prev_index];
if ($prev_token->getId() === T_STATIC) {
$prev_index = $tokens->getPrevMeaningfulToken($prev_index);
$prev_token = $tokens[$prev_index];
}
if ($prev_token->getId() === T_PROTECTED
|| $prev_token->getId() === T_PUBLIC
|| $prev_token->getId() === T_PRIVATE
|| $prev_token->getId() === T_VAR
|| $prev_token->getId() === T_DOUBLE_COLON
) {
continue;
}
$tokens[$index] = new Token([$token->getId(), $this->updateVariableCasing($token->getContent())]);
}
}
} |
@jmkoni could you rerun
For update README.rst and resolve the conflict, so it would look RTM for maintainers? Thanks |
c9807e1
to
9a7894c
Compare
variable_case
fixer
9a7894c
to
13f23c7
Compare
I've added failing test cases for class' properties to address @muglug's comment - will fix it after initial agreement that we want this fixer. |
4836779
to
d30c4ef
Compare
- mark fixer as risky - implement proper interface for configurability - add type definitions - provide description and riskiness information
d30c4ef
to
02bdf44
Compare
variable_case
fixervariable_case
fixer
@keradus we need decision whether we want to continue to work on this (see this comment). |
The project that I am working in has a rule that variables should all be in camelCase. However, we don't have any way to currently lint or automatically fix. Since we are using PHPCSFixer, I wanted to add a fixer that would automatically make that change.