PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Add an option for Squiz.NamingConventions.ValidVariableName

Open tim0991 opened this issue 7 years ago • 11 comments

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

tim0991 avatar Dec 08 '17 03:12 tim0991

Is there any workaround to allow this?

vedmant avatar May 22 '18 18:05 vedmant

Seems like this PR would allow it https://github.com/squizlabs/PHP_CodeSniffer/pull/2380

neanton avatar Jan 23 '19 13:01 neanton

Removing this for 3.5.0 as PR was not accepted.

gsherwood avatar May 12 '19 23:05 gsherwood

Any update?

anurak-kg avatar Aug 18 '19 16:08 anurak-kg

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.

gsherwood avatar Aug 18 '19 23:08 gsherwood

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.

Levivb avatar Feb 12 '20 07:02 Levivb

I'm using this.

glen-84 avatar Feb 12 '20 07:02 glen-84

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

Levivb avatar Feb 12 '20 08:02 Levivb

I just using @Levivb 's code create a package, hope it help someone util a new PR here.

CloudyCity avatar Mar 25 '21 04:03 CloudyCity

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

simPod avatar Feb 19 '22 19:02 simPod

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

Justintime50 avatar Jul 14 '22 15:07 Justintime50