abkrim's avatar
Level 13

Exclude CamelCase validation in phpcs

My phpcs is worng because when running phpcs this show error about CamelCase on name of tests

 16 | ERROR | Method name "RegistrationTest::can_register" is not in camel caps format
 16 | ERROR | Visibility must be declared on method "can_register"

I don't like exclude tests/* from phpcs. I've tried some aporach on phpcs.xml but not work I don't understan well how I can do it.

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <description>The coding standard for our project.</description>
    <rule ref="PSR2" />

    <file>app</file>
    <file>bootstrap</file>
    <file>config</file>
    <file>database</file>
    <file>resources</file>
    <file>routes</file>
    <file>tests</file>

    <exclude-pattern>bootstrap/cache/*</exclude-pattern>
    <exclude-pattern>bootstrap/autoload.php</exclude-pattern>
    <exclude-pattern>*/migrations/*</exclude-pattern>
    <exclude-pattern>*/seeds/*</exclude-pattern>
    <exclude-pattern>*.blade.php</exclude-pattern>
    <exclude-pattern>*.js</exclude-pattern>
    <exclude-pattern>tests/*</exclude-pattern>
    <!-- Show progression -->
    <arg value="p"/>
</ruleset>
0 likes
11 replies
martinbean's avatar

@abkrim It’s not “wrong”. It’s just doing exactly what it’s been instructed to. If you want to disable that rule for tests, then do so.

I don’t know what the rule name is off-hand, but for me in Visual Studio Code it’s displayed in the tooltip when I hover over some code that fails my phpcs configuration. You would then add it to your phpcs.xml (or whatever you’ve called your file) like this:

<rule ref="Name.Of.Rule">
  <exclude-pattern>./tests/*</exclude-pattern>
</rule>
1 like
atunje's avatar

This works. Thanks

<rule ref="PSR1.Methods.CamelCapsMethodName">
  <exclude-pattern>./tests/*</exclude-pattern>
</rule>
abkrim's avatar
Level 13

Hi @martinbean

I'm sorry for my English.

I know that rule works, but I don't want to exclude all path tests /

What I want is to exclude the use of camel case in the name of the methods in the tests of the path tests / * the error CamelCase in the name of methods (PSR1.Methods.CamelCapsMethodName)

martinbean's avatar

@abkrim Yes. So use the rule name (PSR1.Methods.CamelCapsMethodName) and tell it to be excluded for classes in your tests directory.

abkrim's avatar
Level 13

Hu @martinbean

I just tried some some approximations

Below not work

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <description>The coding standard for our project.</description>
    <rule ref="PSR2" />

    <file>app</file>
    <file>bootstrap</file>
    <file>config</file>
    <file>database</file>
    <file>resources</file>
    <file>routes</file>
    <file>tests</file>

    <exclude-pattern>bootstrap/cache/*</exclude-pattern>
    <exclude-pattern>bootstrap/autoload.php</exclude-pattern>
    <exclude-pattern>*/migrations/*</exclude-pattern>
    <exclude-pattern>*/seeds/*</exclude-pattern>
    <exclude-pattern>*.blade.php</exclude-pattern>
    <exclude-pattern>*.js</exclude-pattern>
    <rule ref="PSR1.Methods.CamelCapsMethodName">
        <file>tests</file>
        <exclude-pattern>./tests/*</exclude-pattern>
    </rule>
    <!-- Show progression -->
    <arg value="p"/>
</ruleset>
-----------------------------------------------------------------------------------------------------------------------------
  16 | ERROR   | Visibility must be declared on method "registration_page_contains_livewire_component"
  22 | ERROR   | Visibility must be declared on method "can_register"
  25 | WARNING | Line exceeds 120 characters; contains 154 characters
  34 | ERROR   | Visibility must be declared on method "email_is_required"
  46 | ERROR   | Visibility must be declared on method "email_is_valid_email"
  58 | ERROR   | Visibility must be declared on method "email_hasnt_been_taken_already"
  79 | ERROR   | Visibility must be declared on method "see_email_hasnt_already_been_taken_validation_message_as_user_types"
  98 | ERROR   | Visibility must be declared on method "password_is_required"
 110 | ERROR   | Visibility must be declared on method "password_is_minimum_of_six_characters"
 122 | ERROR   | Visibility must be declared on method "password_matches_password_confirmation"
 134 | ERROR   | Visibility must be declared on method "name_is_minimum_of_six_characters"
 146 | ERROR   | Visibility must be declared on method "name_is_required"
-----------------------------------------------------------------------------------------------------------------------------

if use work with out errors, but not check other rules. (excluded)

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <description>The coding standard for our project.</description>
    <rule ref="PSR2" />

    <file>app</file>
    <file>bootstrap</file>
    <file>config</file>
    <file>database</file>
    <file>resources</file>
    <file>routes</file>
    <file>tests</file>

    <exclude-pattern>bootstrap/cache/*</exclude-pattern>
    <exclude-pattern>bootstrap/autoload.php</exclude-pattern>
    <exclude-pattern>*/migrations/*</exclude-pattern>
    <exclude-pattern>*/seeds/*</exclude-pattern>
    <exclude-pattern>*.blade.php</exclude-pattern>
    <exclude-pattern>*.js</exclude-pattern>
    <exclude-pattern>./tests/*</exclude-pattern>
    <!-- Show progression -->
    <arg value="p"/>
</ruleset>
martinbean's avatar

@abkrim Right, so what’s the problem? It’s not complaining about camel case method names any more; it’s now complaining about missing visibility statements on your methods. It’s a completely different error.

abkrim's avatar
Level 13

The question is that with the normal configuration phpcs detects as incorrect the rule that defines the use of CamelCase in the method names. Of the two options, one what it does is disable phpcs for ALL the test directory. What you offer me, what it does is eliminate the CamelCase error but the others appear.

The reality is that I want to have the same rules as the normal code, except for the name format of the test methods.

I feel that the problem is one of communication because of the language. Thanks @martinbean

martinbean's avatar

What you offer me, what it does is eliminate the CamelCase error but the others appear.

Yes. You’ve disabled the rule for camel case names, now you have a different rule kicking in, which sounds like your test methods look like this:

function test_something()
{
    //
}

It’s missing a visibility modifier (i.e. public).

I think you might need to read up on PHP CodeSniffer a bit more and understand just what it is it’s actually doing.

If you try and write non-PSR-compliant code against a PSR style check then yes, you’re going to get lots of failures unless you configure phpcs to do the checks you want.

abkrim's avatar
Level 13

Hi, @martinbean My test I think are correct write on standard PSR-2 for test. I add /** @test */ in top of method.

/** @test */
    function name_is_minimum_of_six_characters()
    {
        Livewire::test('auth.register')
            ->set('email', '[email protected]')
            ->set('name', 'Abde')
            ->set('password', 'secret')
            ->set('passwordConfirmation', 'secret')
            ->call('register')
            ->assertHasErrors(['name' => 'min']);
    }
martinbean's avatar

@abkrim It’s not, though. Read what phpcs is telling you, and what I’ve written.

Your method is missing a visibility declaration such as public.

abkrim's avatar
Level 13

@martinbean thanks for your time.

I start with:

  • the premise that in tests the use of / ** @test * / is valid and extended to obviate the need to declare the scope of the method
  • That the method names in tests are underscore. Perhaps I do not know in depth if it is correct but if I know that it is commonly used by many programmers.
  • What I want is for phpcs to identify it as such.

After our talk, I found the solution and for that I am very grateful

<!--<exclude-pattern>./tests/*</exclude-pattern>-->
<rule ref="PSR1.Methods.CamelCapsMethodName">
    <file>tests</file>
    <exclude-pattern>./tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Scope.MethodScope">
    <file>tests</file>
    <exclude-pattern>./tests/*</exclude-pattern>
</rule>

Please or to participate in this conversation.