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.66k stars 1.48k forks source link

Feature request: ability to define config options in phpcs.xml (or elsewhere) #1358

Closed MickeyKay closed 7 years ago

MickeyKay commented 7 years ago

Hi there,

First of all, fantastic project - I love it! Feature request (or perhaps it exists already and I just can't figure it out). . .

I would love to be able to specify config options like show_colors inside our phpcs.xml/ruleset.xml files, or elsewhere if that makes more sense. Reason being is that I'd like to be able to package up and share our rulesets within our org, however requiring config options to be specified via the command line at runtime feels like an unnecessary step. Is there currently a way to save these config options as actual config?

Thanks!

gsherwood commented 7 years ago

Yes, this feature exists. You can set config options and CLI arguments inside a ruleset/phpcs.xml file.

Some examples are here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

The phpcs.xml.dist file in the 3.0 branch (the standard PHPCS itself uses) also has some examples, including settings colours and default files to check (allowing you to just run phpcs inside the root dir): https://github.com/squizlabs/PHP_CodeSniffer/blob/3.0/phpcs.xml.dist

The line you are probably after is: <arg name="colors" />

Let me know if you need any help.

gsherwood commented 7 years ago

Forgot to mention that as show_colors is both a config option and a CLI arg, it is better to specify the CLI arg (using the arg tag) in the ruleset. But if you want to, you can also use the config tag and it should do the same thing.

MickeyKay commented 7 years ago

@gsherwood I tried this, and the arg method works, however when I add the following to my phpcs.xml file, it has no effect:

<config name="colors" value="true" />

What am I missing?

gsherwood commented 7 years ago

What am I missing?

Sorry, I gave you the wrong info. You can't set config values to override CLI arguments. You only set those config values for sniff configs. The <arg> tag is the way to go.

MickeyKay commented 7 years ago

So, there's no way, for example, to update the installed_paths in my ruleset.xml with something like the following?

<arg name="standard" value="vendor/wp-coding-standards/wpcs" />
gsherwood commented 7 years ago

Installed paths isn't something you set on the CLI, so you'd use:

<config name="installed_paths" value="/path/to/standard,/path/to/otherstandard" />

You're not overriding CLI values with that, so the value will be set and used during execution.

But that's also something you should typically set in your actual PHPCS config file, and not in a ruleset. Inside a ruleset, you would typically reference sniffs using relative paths. But it all depends on how much control you have over the standards you are importing.

MickeyKay commented 7 years ago

Ahhh, fantastic. Thanks!

Pton commented 5 years ago

For anyone else who got here by looking for a way to enable colors in PHPCS; you can enable them using the --colors option via the CLI

Pi-George commented 4 years ago

Installed paths isn't something you set on the CLI, so you'd use:

<config name="installed_paths" value="/path/to/standard,/path/to/otherstandard" />

You're not overriding CLI values with that, so the value will be set and used during execution.

But that's also something you should typically set in your actual PHPCS config file, and not in a ruleset. Inside a ruleset, you would typically reference sniffs using relative paths. But it all depends on how much control you have over the standards you are importing.

What if your path is relative not absolute? Where is it relative from?

jrfnl commented 4 years ago

What if your path is relative not absolute? Where is it relative from?

@Pi-George The ruleset.