-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow CodeSniffer.conf to be stored in a user's home directory #2062
Comments
I'm not sure why the file is being removed. I guess it must be specific to how composer updates packages because I don't track CodeSniffer.conf in the repo and so composer would see it as a local file only. I don't know any way around that. It seems like you'd need to either stop using that file and move to a phpcs.xml file instead (per-project defaults) or I'd need to add an option to allow you to specify a path to a config file on the command line. But given I can't store this path anywhere, you'd have to specify it every time you run PHPCS. Or any ideas you have? |
So after looking into it a bit further, using composer's
It looks like composer uses zip archives to install the packages, which means the entire package directory is being completely overwritten when an update happens, this led me to think i could use the
This in fact resolves the issue when updating (so the config file remains using this option), but I think you'd agree that this is not a very practical or long term solution, and the clone actually took quite a long time. I think a better solution would be to just change where this config file is stored in the first place; is this possible? i.e. so when we run:
instead of generating the file here:
create it here instead:
Obviously you could play with the name a little. |
Using the user's home directory is probably a good option, although that does mean that shared configs are basically gone. So maybe the way to do this is to look in the standard config file location first, then look in the user's home directory if nothing is found there. The tricky bit is setting configs. PHPCS wouldn't know if you want to set a global config or a user config, so it would need a CLI switch (sort of like git uses) to switch between those two locations I guess. What I really would like is a place to store a config file in composer that isn't destroyed during upgrades, like the PEAR installer has always had. But I can't find a place to put anything like that. The suggestion is to include the config file inside your project itself, which is what the phpcs.xml (or xml.dist) file is for. I suggest changing to the phpcs.xml file format and committing it to your projects directly, if possible, as it means that you can have different settings per project if necessary, and any other developers who ever work on that project already have the PHPCS settings required for it. Or if you happen to clone the repo on another machine, the settings are there already. |
@gsherwood Thanks for this, I will place the config in the xml file for now, its a shame we can't use the main config, I have dug through the composer documentation myself and like you said there is nowhere you can put this file, perhaps a feature request is in order, but i shall leave that decision up to you. I was wondering however, with the xml option, can I put a phpcs.xml file in the root of my home directory and set the installed_paths in it, and then set other xml files in child directories without having to set the installed_paths in each each one of those child directories, i.e. they will inherit the installed_paths from the one in my home directory creating a combined config of sorts, or will they completely overwrite the one in my home directory? |
@digitalsounds You can include the ruleset from your project root in the rulesets in child directories. That will "import" all rules and config from the root ruleset. <rule ref="/path/to/project/root/phpcs.xml.dist"/> or <rule ref="./../phpcs.xml.dist"/> |
I have actually found a solution to global phpcs config file overwriting itself, although some may feel it lacks in elegance, but I have taken a somewhat pragmatic approcach with this. To your global composer.json file, usually located in $HOME/.composer or $HOME/.config/composer, add the following to the scripts key: "post-install-cmd": [
"phpcs --config-set installed_paths <installed_paths>",
"phpcs --config-set default_standard <coding_standard>"
],
"post-update-cmd": [
"phpcs --config-set installed_paths <installed_paths>",
"phpcs --config-set default_standard <coding_standard>"
] so the complete composer.json file would look something like this: {
"require": {
"squizlabs/php_codesniffer": "3.*",
"friendsofphp/php-cs-fixer": "^2.12"
},
"scripts": {
"post-install-cmd": [
"phpcs --config-set installed_paths <installed_paths>",
"phpcs --config-set default_standard <coding_standard>"
],
"post-update-cmd": [
"phpcs --config-set installed_paths <installed_paths>",
"phpcs --config-set default_standard <coding_standard>"
]
}
} Here we are just setting the global |
Hi there! Thank you for your previous comments. I have some thoughts:
Yes please!! I always miss that (optional) option. We then could specify any location. If the file doesn't exist or is bad formatted just raise an error. If the option is not used then look up for the config file in the same way you're doing it now.
Thank you for your time ;) |
The code sniffer doesnt appear to use the local php version or the compsoer.json php version when issuing waning for certain standards and I need it to remember what version of php to run as. (this migth be another issue) Just seems very difficult to get the information I need. Can you point me to some clear documentation on how to write this phpcs.xml file? |
@b-hayes https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset Note: if you save a ruleset file in the project root and name it |
Sorry for reviving this from the dead, but since the issue is still open and since my question is closely related, I thought I'd start by leaving a comment here. If this should be a separate issue, I can do that no problem. I would like to use My exact use case I'm having issues with is that I am using phpcodesniffer-composer-installer to set the What I would look to be able to do is tell the sym-linked If the config file path could be user-defined, as currently being worked on in #3191 👏, I would create a PR for the What could work as well is making PHP_CodeSniffer look for the configuration file in the current directory, pretty much in line with how/where it looks for a ruleset file...? 🤔 Does this make sense, or is there anything worth clarifying? |
For Mac users using Homebrew, every time we upgrade php-code-sniffer, it creates a new
Having PHPCS have a "search path" for |
@dossy that sounds like something that could be added to https://github.com/Homebrew/homebrew-core/blob/master/Formula/p/php-code-sniffer.rb. I recommend you open an issue over there to see if your particular case can be solved within the Brew toolset. |
@fredden Unfortunately, Homebrew formulas do not have a method of executing code on upgrade, and by default Homebrew uninstalls the old version before installing the new version during an upgrade, so even trying to add code to the install hook to try and copy forward the |
When running
composer global update
, if there is even a minor version update, e.g. from 3.2.3 to 3.3.0 then the CodeSniffer.conf gets removed (for me its here: ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer.conf) I have it installed globally.This file as you know stores the global config settings for default_standard and installed_paths, etc; which means i lose these settings after every time code sniffer updates and i have to set them again, any way we can avoid this?
The text was updated successfully, but these errors were encountered: