Skip to content
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

Squiz.NamingConventions.ValidVariableName how to only report for declaration, not usage #3714

Open
momala454 opened this issue Nov 18, 2022 · 3 comments

Comments

@momala454
Copy link

momala454 commented Nov 18, 2022

Describe the bug
I want to report variable or members not in camelCase, but with some exceptions. So I would like to report only on variable/member declaration, not on usage

Code sample

class Hello
{
    // phpcs:disable Squiz.NamingConventions.ValidVariableName
// it is ignored
    public string $hello_world;

    public function __construct(
// it is ignored
        public int $number_of_results,
    ) {
    }

    // phpcs:enable Squiz.NamingConventions.ValidVariableName

    public function test(): void
    {
        // shouldn't be reported
        $this->number_of_results = 1;

        // should be reported for the creation of $number_of_pages 
        $number_of_pages = $this->number_of_results;
    }
}

Custom ruleset

<?xml version="1.0"?>
<rule ref="Squiz.NamingConventions.ValidVariableName"/>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
 55 | ERROR | Member variable "number_of_results" is not in valid camel caps format
 58 | ERROR | Variable "number_of_pages" is not in valid camel caps format
 58 | ERROR | Member variable "number_of_results" is not in valid camel caps format

Expected behavior
An option to disable on usage (or disable if it has been disabled on the variable/member declaration). I don't want to need to write //phpcs::disable ... every time I use $this->number_of_results

Versions (please complete the following information):

  • OS: windows 10
  • PHP: 8.1
  • PHPCS: 3.7.1
  • Standard: psr12

Additional context
Add any other context about the problem here.

@momala454 momala454 changed the title Squiz.NamingConventions.ValidVariableName only report for declaration, not usage Squiz.NamingConventions.ValidVariableName how to only report for declaration, not usage Nov 18, 2022
@gsherwood
Copy link
Member

The sniff cannot be configured to only report on the first definition of a variable. It will always report everywhere the variable is used.

This isn't something I would add to the sniff as it is very hard to track this sort of variable declaration in PHP_CodeSniffer, so the results would not be accurate.

@momala454
Copy link
Author

is it possible to detect if the variable is in left side of an equal (=) to differenciate assignment and usage ?

@runthis
Copy link

runthis commented Jun 24, 2024

This would be nice. A better example would be from receiving an object from a vendor and there isn't a way.

$someVendor = new SomeVendor();
echo $someVendor->variable_isnt_mine;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants