PHP Code Sniffer through VS Code

What issue or error are you experiencing?

I am attempting to run PHP Code Sniffer, but am open to other solutions in order to have an automated way to stick to WP coding standards through VS Code and Local. I have tried several VS Code extensions, but am currently sticking to PHP Sniffer (wongjn).

I’m going to start by saying this is all bumping up against my knowledge of the command line, terminals, shells, and how Local works. I think the trouble I’m having is an issue that the VSC plugins are using the global/Mac shell and not the Local shell. When I open a terminal window using the “Site Shell” link in Local, I see Composer and the PHP Code Sniffer that I installed. When I open a new terminal window and try to see Composer, it’s cannot be found.

The VSC extensions ask for a path to the executables folder, which I’ve tried ‘/Users/NAME/.composer/vendor/bin/’ but that pops up errors that it’s not a file or directory. (I can also see that php_codensniffer has a directory in that /vendor directory.)

So, how can I get VSC code to see the site shell? Is there some other path to the executables folder within Local that I haven’t found?


What steps can be taken to replicate the issue? Feel free to include screenshots, videos, etc


System Details

  • Local Version:
    Version 9.2.3+6776

  • Operating System (OS) and OS version:
    MacOS Sequoia 15.4


Local Logs

Attach your Local Logs here (Help Doc - Retrieving Local’s Log)


Security Reminder
Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there’s always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn’t private info like passwords being displayed.

Hi, @lagom-jim!

To get Local working with PHPCS, composer, VS Code, and a WordPress plugin you are developing on macOS you can:

  1. Set up your plugin to use PHPCS and other dev dependencies in its composer.json, as suggested under “composer project-based installation” in the official WPCS docs.

    For example, you could run the lines below one-by-one in Local’s site shell (you will need to have already created a composer.json in your plugin root, for example by doing composer init from the plugin root):

    cd wp-content/plugins/your-plugin-name # very important to run subsequent lines from the plugin root!
    composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
    composer require --dev wp-coding-standards/wpcs:"^3.0"
    composer require --dev dealerdirect/phpcodesniffer-composer-installer:"^1.0" # to automatically install WP coding standards rulesets
    
  2. Add a phpcs.xml.dist to your plugin folder root (same place as the composer.json), for example:

    <?xml version="1.0"?>
    <ruleset name="My Plugin Coding Standards">
        <description>PHP_CodeSniffer configuration for My Plugin</description>
    
        <!-- Set the default standard -->
        <rule ref="WordPress-Core" />
        <rule ref="WordPress-Extra" />
        <rule ref="WordPress-Docs" />
    
        <!-- Include your plugin source files, wherever they are -->
        <file>includes</file>
        <file>src</file>
        <file>your-plugin.php</file>
    
        <!-- Exclude vendor or other irrelevant directories -->
        <exclude-pattern>*/vendor/*</exclude-pattern>
        <exclude-pattern>*/node_modules/*</exclude-pattern>
    
        <!-- Optional customizations -->
        <arg name="extensions" value="php"/>
    </ruleset>   
    
  3. In VS Code, install the PHP Sniffer extension that you mentioned: PHP Sniffer - Visual Studio Marketplace

  4. In VS Code, Cmd + , to view preferences and search for @ext:wongjn.php-sniffer to see only that extension’s settings.

  5. Click the “Workspace” tab to edit the PHP Sniffer extension settings for this project only — you will want to adjust the phpcs location and rules on a per-project basis.

  6. Set wp-content/plugins/your-plugin-name/vendor/bin/ as the PHP Sniffer: Executables Folder, being sure to change your-plugin-name to your plugin’s folder name.

  7. Set wp-content/plugins/your-plugin-name/phpcs.xml.dist as the PHP Sniffer: Standard (at the bottom of the settings), being sure to change the your-plugin-name part again.

  8. Open any PHP file in your plugin, choose “format document…” from VS Code’s command palette, and choose PHP Sniffer (setting it as the default if you have other formatters available). Or you can try “format document with…” if you already set a default formatter for PHP in the past.

You should now get auto formatting when you use alt+shift+f (after a short delay) and red squiggles when you do something that breaks WP coding standards in your plugin code, as well as popovers for the squiggles and entries in the “problems” pane, see screenshots below.

For the above, you’ll need to run composer in Local’s site shell. If you want to be able to run composer in VS Code’s terminal instead, you can look into these options (but neither is required for the above to work):

  • Consider installing composer globally (it will then run in VS Code’s terminal, and also override Local’s composer).
  • Alternatively, try the direnv addon for Local by installing the .tgz file from the releases page. (You will also need to install direnv via homebrew: direnv — Homebrew Formulae)

But, honestly, using Local’s composer as you’re doing is fine unless you want to be able to frequently run composer from VS Code’s terminal.

CleanShot 2025-04-16 at 18.17.22

1 Like

Thank you @nickc for the very thorough walk through!

Alas, I followed your steps but am getting the same errors that there’s no file or directory.

I have Composer installed in the project root. – I did not have it installed there before.

I see the vendor/bin/phpcs file.

Within the Workspace Settings for PHP Sniffer, I have a relative path to the vendor/bin directory.

And one to the standard.

Maybe I missed a step somewhere, but I ran through everything two separate times (on two separate projects) and both ended up not working.

Thanks for trying that, @lagom-jim.

env: php: No such file or directory

Ah, it looks like the phpcs VS Code extension expects PHP to be available system-wide. I don’t see a way in its config to specify a path to a specific version of PHP like the one Local ships with.

The quickest fix would be to install PHP globally on your system, for example by installing homebrew and then running brew install php in your terminal. (That’s what I have on my system, which is why the extension works for me.)

After that you should be able to restart VS Code and run the phpcs extension commands.