coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

Is there a sniff to disallow "variable variable"?

Open ghnp5 opened this issue 3 years ago • 5 comments

Looking to disallow things like:

$$variable

With two dollar signs.

Thank you!

ghnp5 avatar Dec 18 '21 21:12 ghnp5

This should be super simple with custom phpstan rule:


use PhpParser\Node\Expr\Variable;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
 * @implements Rule<Variable>
 */
class CamelCaseFieldNamingRule implements Rule
{

    public function getNodeType(): string
    {
        return Variable::class;
    }

    /**
     * @param Variable $node
     * @return string[]
     */
    public function processNode(Node $node, Scope $scope): array
    {
        if (!is_string($node->name)) {
            return ['Expression for variable name is not allowed!'];
        }

        return [];
    }

}

janedbal avatar Dec 20 '21 11:12 janedbal

Thank you very much. Haven't got the chance to implement phpstan in my project yet! But I'm taking the note for when I do :)

Do you think this would be hard to implement as a PHPCS rule?

ghnp5 avatar Dec 20 '21 16:12 ghnp5

Do you think this would be hard to implement as a PHPCS rule?

No, it should be really simple sniff :)

kukulich avatar Dec 20 '21 16:12 kukulich

I leave the feature request here, so! Thank you!

ghnp5 avatar Dec 20 '21 17:12 ghnp5

Btw, it's already implemented by phpstan strict rules

https://phpstan.org/r/2966deda-b4e9-4d76-ada9-2c9b6467c857

mabar avatar Jul 17 '22 15:07 mabar

Implemented in https://github.com/slevomat/coding-standard/commit/43904bdc6c8f6d424cc75a582f47a52d68643081

kukulich avatar Feb 01 '23 15:02 kukulich

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Mar 05 '23 00:03 github-actions[bot]