You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@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:
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.
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].
@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.
Activity
jrfnl commentedon Jan 11, 2024
@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.
config-setdefines the default behaviour of PHPCS.config-showwill show you those default settings. These are not necessarily the same settings which will be used when running PHPCS.[.]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.
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:
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.distin your project root with the following contents:If you would now run those last two commands again, you would see that the output is different:
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:
vendordirectory and will be overwritten on every update of PHPCS. It is also typically excluded from being committed to your project via the.gitignorefile containing thevendordirectory as a directory to be ignored.[.]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_widthorcolorsetting 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 ?
jrfnl commentedon Jan 17, 2024
Closing for lack of response. Presumably the answer provided was sufficient.
jefhar commentedon Jan 19, 2024
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.confis not[.]phpcs.xml[.dist].jrfnl commentedon Jan 19, 2024
@jefhar Please read my extensive explanation above. The
--config-showcommand is ONLY about what's in theCodeSniffer.conffile and is unrelated to any[.]phpcs.xml[.dist]file.jefhar commentedon Jan 20, 2024
@jrfnl Then how do i know what's in the config of
[.]phpcs.xml[.dist]?jrfnl commentedon Jan 20, 2024
@jefhar Did you try running the commands I gave you ?
-e= "explain" ...