Skip to content

Squiz.NamingConventions.ValidVariableName does not use correct error code for all member vars #3197

@josemv92

Description

@josemv92

Describe the bug
While working with a Laravel project, I try to add sniffs to restrict the code to camel case variables but allow member variables not following this rule since model instances can have properties for the columns in the table which can follow snake case.

Using the Squiz standard to setup naming convention, I try to use NotCamelCaps with MemberNotCamelCaps disabled, but the former seems to interfere whenever I use a member property in any method, flagging it as invalid.

This can be reproduced with the following example:

Code sample

class Test {
    public $test_var = null; // Notice how this is not declared as an error.

    public function main() {
        $this->test_var = 1; // However, whenever I use the variable, I get an error of invalid format.
    }
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
    <rule ref="Squiz.NamingConventions.ValidVariableName" />
    <rule ref="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps">
        <severity>0</severity>
    </rule>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Run phpcs test.php -s using the standard ruleset provided above.
  3. See error message displayed
FILE: ./test.php
----------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------
 7 | ERROR | Variable "test_var" is not in valid camel caps format
   |       | (Squiz.NamingConventions.ValidVariableName.NotCamelCaps)
----------------------------------------------------------------------------------------------------------------------------

Time: 16ms; Memory: 4MB

Expected behavior
NotCamelCaps should not show an error for member variables when MemberNotCamelCaps is disabled.

Versions (please complete the following information):

  • OS: Using Ubuntu, but this might happen in any.
  • PHP: 7.4
  • PHPCS: 3.5.8
  • Standard: Squiz

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions