Skip to content
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

Update Guideline for SublimeText and WPCS #2001

Closed
smileBeda opened this issue Jun 25, 2021 · 4 comments
Closed

Update Guideline for SublimeText and WPCS #2001

smileBeda opened this issue Jun 25, 2021 · 4 comments

Comments

@smileBeda
Copy link

smileBeda commented Jun 25, 2021

According to Slack Conversation, opening issue to update this guideline.

Instead of a ready to copy paste text I will simply share here each and every step I needed to take and the findings I had, please pick what you think is useful to update the guide.


This setup assumes global usage, not project limited, and uses the first suggested package of the DOC (https://github.com/WordPress/WordPress-Coding-Standards/wiki/Setting-up-WPCS-to-work-in-Sublime-Text#sublime-phpcs-package)

  1. Follow this to install WPCS and PHPCS since you likely will have Composer 2 and not the outdated Composer 1 on your machine. Thus, start with this command:
composer global require dealerdirect/phpcodesniffer-composer-installer --update-no-dev
composer global require wp-coding-standards/wpcs --update-no-dev
  1. Put the bin in your path, with following command:
    nano ~/.zshrc, add export PATH="$HOME/.composer/vendor/bin:$PATH" to the file and write it out, close the file, then source ~/.zshrc and confirm it worked with where phpcs (you should see something like /Users/your_user_name/.composer/vendor/bin/phpcs)
  2. Point PHPCS to WPCS config:
    phpcs --config-set installed_paths /Users/your_user_name/.composer/vendor/wp-coding-standards/wpcs
  3. In Sublime, open Preferences > Package Control and choose Install Package, then select Phpcs package (https://benmatselby.dev/sublime-phpcs/)
  4. In Sublime, open Preferences > Package Settings > PHP Code Sniffer > Settings Default and copy the entire content, then open Preferences > Package Settings > PHP Code Sniffer > Settings User and paste the entire content. Now alter these points:
  • item phpcs_executable_path should be (full line statement):
    "phpcs_executable_path": "/Users/your_user_name/.composer/vendor/bin/phpcs",
  • item phpcs_additional_args should be (full line statement):
"phpcs_additional_args": {
        "--standard": "WordPress",
        "-n": ""
    },
  • item phpcbf_executable_path should be (full line statement):
    "phpcbf_executable_path": "/Users/your_user_name/.composer/vendor/bin/phpcbf",
  • item phpcbf_additional_args should be (full line statement):
"phpcbf_additional_args": {
        "--standard": "WordPress",
        "-n": ""
    },

That should be it, you have now a context menu item "PHP Code Sniffer" in Sublime text where you can sniff, change default, and Beautify.

Note, Sublime package additionally adds "php cs fixer" as well, which will not work with WPCS. Ignore and do not use it.

Note 2, The SublimeLinter-phpcs (second method described on above doc) did not work for me at all. While it is available in the package control for install, it will fail with ImportError: No module named 'SublimeLinter' on install.
Why, not sure, and did not dig, and removed it again.

@smileBeda
Copy link
Author

I am adding here a note that MacOS users of Monterey might run into an error env: php: No such file or directory

This would be the error you see in the terminal after running phpcs --standard=WordPress your-file.php
However in an IDE like Sublime Text you won't see any error, things just won't work anymore, at all but the commands will be there.

If that is the case for you too, and you use Monterey - then be aware that they removed PHP. You will need to install it.
A possible approach is (terminal) brew install php, which will take a while and install PHP, and things will be back working.

@smileBeda
Copy link
Author

Updating this 2 years later, if you still get the Sublime text doing "nothing" when saving, open the console of Sublime text and check the errors.
You might still see env: php: No such file or directory in which case, if the command phpcs --standard=WordPress your-file.php works in the terminal but not in the sublime, you might need to run brew link [email protected]

7.4 because WPCS lives in 2020, and somehow forgot that we only have one maintained PHP version nowadays.
Development is in process about that so, if WPCS 3 ever gets released, you would not need php 7.4 anymore.

I do hope this helps anyone. It certainly helped me now the 5th time, and this doc is still not working...

@smileBeda
Copy link
Author

Well. The original page seems to be deleted meanwhile.
Not going to hunt his anymore therefore.

@jrfnl
Copy link
Member

jrfnl commented Oct 30, 2023

@smileBeda It has not been deleted, though might have been renamed: https://github.com/WordPress/WordPress-Coding-Standards/wiki/Setting-up-WordPressCS-to-work-in-Sublime-Text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants