squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.62k stars 1.48k forks source link

Can't disable rule Underscore #3939

Open alexjonser opened 5 months ago

alexjonser commented 5 months ago

Hello,

we are using in our projects the "Doctrine Coding Standard". For some reasons we want to declarate private properties and methods with a leading underscore.

I don't get it what we are doing wrong to exclude the "PSR2.Classes.PropertyDeclaration.Underscore" and "PSR2.Classes.MethodDeclaration.Underscore" rules.

Here my ruleset

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="DYStandard" xsi:noNamespaceSchemaLocation="./Vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <description>The DY coding standard.</description>
    <ini name="memory_limit" value="512M"/>

    <arg name="basepath" value="."/>
    <arg name="extensions" value="php"/>
    <arg name="parallel" value="80"/>
    <arg name="cache" value=".phpcs-cache"/>
    <arg name="colors"/>

    <arg value="ps"/>
    <file>./Vendor/frameworks/lifework/src/Lifemeter/Helper/Tableobject.php</file>

    <!-- Include full Doctrine Coding Standard -->
    <rule ref="Doctrine">
        <exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName"/>
        <exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException"/>
        <exclude name="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidCommentType"/>
        <exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing"/>
        <exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore" />
        <exclude name="PSR2.Classes.PropertyDeclaration.Underscore" />
        <exclude name="PSR2.Classes.MethodDeclaration.Underscore" />
    </rule>

    <rule ref="Squiz.NamingConventions.ValidVariableName">
        <exclude name="Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore" />
    </rule>

    <rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
        <properties>
            <property name="allowFallbackGlobalConstants" value="true"/>
        </properties>
    </rule>

PHPCS Output

54 | ERROR   | [ ] Private member variable "pDate" must contain a leading underscore (Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore)
   55 | WARNING | [ ] Property name "$_startDate" should not be prefixed with an underscore to indicate visibility (PSR2.Classes.PropertyDeclaration.Underscore)

Versions (please complete the following information)

Operating System Mac OS Ventura 13.2.1
PHP version 8.2.7
PHP_CodeSniffer version PHP_CodeSniffer version 3.8.1 (stable) by Squiz and PHPCSStandards
Standard Doctrine / Custom
Install type composer
alexjonser commented 5 months ago

in addition there is following error in my phpcs output

1 | ERROR   | [ ] An error occurred during processing; checking has been aborted. The error message was: "else" without curly braces is not supported.
      |         |     The error originated in the SlevomatCodingStandard.ControlStructures.EarlyExit sniff on line 401. (Internal.Exception)
alexjonser commented 5 months ago

example out of my composer.json

"config" : {
    "vendor-dir" : "Vendor",
    "allow-plugins": {
      "dealerdirect/phpcodesniffer-composer-installer": true
    }
  },
  "require-dev": {
    "doctrine/coding-standard" : "*"
  }
jrfnl commented 1 month ago

Please don't open issues in this repo anymore. This repo is abandoned and https://github.com/PHPCSStandards/PHP_CodeSniffer is its successor. See: #3932

If the original issue is is still an issue, please open a ticket in the new repo and include a PHP code sample to reproduce the behaviour.

One thing I can already see is a typo:

- <exclude name="PSR2.Classes.MethodDeclaration.Underscore" />
+ <exclude name="PSR2.Methods.MethodDeclaration.Underscore" />

As for the error coming from the Slevomat standard: that's not a PHPCS issue, but an issue with the Slevomat standard in combination with your code and the error spells out what is needed to fix it: ""else" without curly braces is not supported."