Skip to content

phpcs ignores configuration files #248

Closed as not planned
Closed as not planned

Description

@jefhar

Describe the bug

PHP_CodeSniffer ignores .phpcs.xml, phpcs.xml, .phpcs.xml.dist, phpcs.xml.dist

To reproduce

Steps to reproduce the behavior:

$ composer require --dev "squizlabs/php_codesniffer=*"
<<composer stuff>>
$ vendor/bin/phpcs --config-set default_standard PSR2
$ vendor/bin/phpcs --config-show
Using config file: /Users/me/newestProject/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

default_standard: PSR2
report_format:    summary
report_width:     120
show_progress:    1
show_warnings:    0

$ cp vendor/squizlabs/php_codesniffer/src/Standards/PSR12/ruleset.xml .phpcs.xml
$ cp vendor/squizlabs/php_codesniffer/src/Standards/PSR12/ruleset.xml phpcs.xml
$ cp vendor/squizlabs/php_codesniffer/src/Standards/PSR12/ruleset.xml .phpcs.xml.dist
$ cp vendor/squizlabs/php_codesniffer/src/Standards/PSR12/ruleset.xml phpcs.xml.dist
$ vendor/bin/phpcs --config-show
Using config file: /Users/me/newestProject/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

default_standard: PSR2
report_format:    summary
report_width:     120
show_progress:    1
show_warnings:    0

Expected behavior

I expect the my default configuration file to be used.

Versions (please complete the following information)

Operating System MacOS 14.2.1
PHP version PHP 8.3.1 (cli)
PHP_CodeSniffer version PHP_CodeSniffer version 3.8.0 (stable) by Squiz and PHPCSStandards
Standard N/A
Install type local composer

Additional context

CLI output mentions a file in the vendor folder, which in no way is conducive to having a configuration shareable.

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Activity

  1. jrfnl commented on Jan 11, 2024

    @jrfnl
    Member

    @jefhar Your reproduction case is unclear. Based on reading it through though, I have a feeling there is no actual bug, just some confusion over how things work.

    • Whatever you set via config-set defines the default behaviour of PHPCS.
      config-show will show you those default settings. These are not necessarily the same settings which will be used when running PHPCS.
    • The default behaviour can be overruled via a custom ruleset, like [.]phpcs.xml[.dist]. These files will be picked up automatically when using these names.
      For anything you did not overrule via the custom ruleset, PHPCS will fall through to the default configuration.
    • On top of that, both of these can be overruled via arguments passed on the command-line.
      Again: anything not set on the command line, will fall through to the custom ruleset and if not set there, fall through to the default configuration.

    You can test and confirm this for yourself via the following steps:

    $ composer require --dev "squizlabs/php_codesniffer=*"
    $ vendor/bin/phpcs --config-set default_standard PSR2
    
    # Now check what happens if you run PHPCS without any CLI args and without custom ruleset. PSR2 will be used.
    # You can also confirm this like so:
    $ vendor/bin/phpcs -e # This will now show you PSR2 as that has been set as your default standard.
    $ vendor/bin/phpcs --standard=PSR2 -e

    The output of those last two commands will be the same.

    Next, create a custom ruleset.
    You could copy the PSR12 ruleset like you did, but that's bad practice as that means you now have to maintain a duplicate PSR12 ruleset yourself, so better to use the predefined PSR12 ruleset instead.

    To do so, create a file called phpcs.xml.dist in your project root with the following contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <ruleset name="YourProjectName">
        <rule ref="PSR12"/>
    </ruleset>

    If you would now run those last two commands again, you would see that the output is different:

    $ vendor/bin/phpcs -e  # This will now show you PSR 12 as it will defer to the custom ruleset which overrules the configuration.
    $ vendor/bin/phpcs --standard=PSR2 -e # This will no longer show the same output as the first command.
    
    # And you can confirm that PSR12 is used by comparing the output of the first command with the output of this one.
    $ vendor/bin/phpcs --standard=PSR12 -e

    The configuration file (and using config-set) is mostly useful when using a system-wide PHPCS install and you want to define some defaults for all projects.

    However, if you use a per-project install - like you did with the composer require --dev -, it is a much better idea to use a custom ruleset.

    Reasoning:

    • The configuration file is saved in the Composer vendor directory and will be overwritten on every update of PHPCS. It is also typically excluded from being committed to your project via the .gitignore file containing the vendor directory as a directory to be ignored.
    • The [.]phpcs.xml[.dist] file is saved to your project itself and therefore can be committed and doing so will set clear expectations for the codestyle to be used by all contributors to the codebase.

    Even if you would be using a system-wide PHPCS install, it is generally speaking still a good idea to use a per project [.]phpcs.xml[.dist] file and commit it.

    In the system wide config, I would typically only set things like the report_width or color setting as those are defaults I want set a certain way based on the capabilities of my computer and the preferences for those may depend on each individual developer's own setup.

    Does that clarify things ?

  2. jrfnl commented on Jan 17, 2024

    @jrfnl
    Member

    Closing for lack of response. Presumably the answer provided was sufficient.

  3. jefhar commented on Jan 19, 2024

    @jefhar
    Author

    Closing for lack of response. Presumably the answer provided was sufficient.

    Actually, I was busy.

    Let me clarify: The [.]phpcs.xml[.dist] file, whether it exists or not is not read, and it's only "Using config file: /Users/me/newestProject/vendor/squizlabs/php_codesniffer/CodeSniffer.conf"

    As you can tell, /Users/me/newestProject/vendor/squizlabs/php_codesniffer/CodeSniffer.conf is not [.]phpcs.xml[.dist].

  4. jrfnl commented on Jan 19, 2024

    @jrfnl
    Member

    @jefhar Please read my extensive explanation above. The --config-show command is ONLY about what's in the CodeSniffer.conf file and is unrelated to any [.]phpcs.xml[.dist] file.

  5. jefhar commented on Jan 20, 2024

    @jefhar
    Author

    @jrfnl Then how do i know what's in the config of [.]phpcs.xml[.dist]?

  6. jrfnl commented on Jan 20, 2024

    @jrfnl
    Member

    @jefhar Did you try running the commands I gave you ? -e = "explain" ...

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

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