Skip to content

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

@momala454

Description

@momala454

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions