PHP_CodeSniffer
PHP_CodeSniffer copied to clipboard
Add an option for Squiz.NamingConventions.ValidVariableName
Could support an option to specify only validate general variable like $foo_bar
Do not validate object variable like $object->foo_bar
because object variable may be a orm attribute, it assigned by database table, and in some case db table field name use snake_case, so if using both at the same time will be conflict
Is there any workaround to allow this?
Seems like this PR would allow it https://github.com/squizlabs/PHP_CodeSniffer/pull/2380
Removing this for 3.5.0 as PR was not accepted.
Any update?
Any update?
It's not scheduled for development. It's unlikely to be looked at unless someone else is able to put some thought into a PR. See my comments on the PR as to why this isn't just a quick change.
Hm, a shame this isn't picked up yet. I'm trying to get my team to work towards camelCaps local variables, which this sniff seems to enforce. Unfortunately this sniff is unusable in its current form as properties called on objects are checked within processVariable
part.
For now I'll probably subclass the sniff and remove any property/member checks and only keep local variable enforcement.
I'm using this.
Ah, cool.
Looks similar to what I just created:
class ValidVariableNameSniff extends BaseValidVariableNameSniff
{
protected function processVariable(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
// If it's a php reserved var, then its ok.
if (isset($this->phpReservedVars[$varName]) === true) {
return;
}
// If variable is called statically, ignore it (Class::$is_okay || $object::is_okay;)
if (isset($tokens[$stackPtr - 1]) && $tokens[$stackPtr - 1]['code'] === T_DOUBLE_COLON) {
return;
}
if (Common::isCamelCaps($varName, false, true, false)) {
return;
}
$error = 'Variable "%s" is not in valid camel caps format';
$data = [$varName];
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
}
protected function processMemberVar(File $phpcsFile, $stackPtr): void
{
// Not checking anything for member vars atm
}
Only functional difference seems to be the if (isset($tokens[$stackPtr - 1]) && $tokens[$stackPtr - 1]['code'] === T_DOUBLE_COLON)
check
I just using @Levivb 's code create a package, hope it help someone util a new PR here.
We have created ValidVariableNameSniff
sniff that takes regex pattern to check for variables names, camelCase is default regex value.
https://github.com/cdn77/coding-standard/releases/tag/6.0.0
The name of this issue is incomplete and should be clarified to include what kind of option. Had to dig around awhile before I found this to be what I was looking for.
This would be a great addition to have as to @Levivb's point, this sniff is unusable in its current form.
EDIT: After some digging, this rule ultimately did what I was looking for: https://stackoverflow.com/a/45539698/6064135