Formatting PHP Code in VSCode

PHP Settings

  1. Set the executable path on settings.json: "php.validate.executablePath": "<path>"
    where <path> is the path to the php executable:
         /usr/bin/php on linux
  2. Install the PHP Intellisense plugin
    1. add the following to the settings.json file
      "php.executablePath": "<path>",
      "php.suggest.basic": false,
      "files.eol": "\n"

      where <path> is the path to the php executable:
           /usr/bin/php on linux

Coding Standard

PHP Coding Style

Install PHP-CodeSniffer in your system. Note that this will require you to have installed the php xmlwriter extension. It can be installed with:

sudo apt-get -y install php7.4-xmlwriter;
 composer global require squizlabs/php_codesniffer

To prevent the error “referenced sniff symfony does not exist” make sure you set the coding standard for the sniffer:

cd /home/mydevuser/Sites;
git clone git://github.com/djoos/Symfony2-coding-standard.git;
php /home/mydevuser/.config/composer/vendor/bin/phpcs --config-set installed_paths /home/mydevuser/Sites/Symfony2-coding-standard;

See also:   Symfony Coding Standard for Code Sniffer.

On Linux

Then, on VS Code install the phpsab plugin

ext install ValeryanM.vscode-phpsab

Last, add the following lines to the settings.json file:

"phpsab.composerJsonPath": "<composerPath>",
"phpsab.debug": true,
"phpsab.executablePathCBF": "<phpcbfPath>",
"phpsab.executablePathCS": "<phpcsPath>",
"phpsab.snifferShowSources": true,
"phpsab.standard": null,
"phpsab.snifferMode": "onSave"

Example on typical Linux machine:

    "phpsab.composerJsonPath": "/usr/bin/composer",
    "phpsab.debug": true,
    "phpsab.executablePathCBF": "/home/mydevuser/.config/composer/vendor/bin/phpcbf",
    "phpsab.executablePathCS": "/home/mydevuser/.config/composer/vendor/bin/phpcs",
    "phpsab.snifferShowSources": true,
    "phpsab.standard": null,
    "phpsab.snifferMode": "onSave"

Example on typical Mac machine:

    "phpsab.composerJsonPath": "/usr/local/bin/composer",
    "phpsab.debug": true,
    "phpsab.executablePathCBF": "/Users/mydevuser/.composer/vendor/squizlabs/php_codesniffer/bin/phpcbf",
    "phpsab.executablePathCS": "/Users/mydevuser/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs",
    "phpsab.snifferShowSources": true,
    "phpsab.standard": null,
    "phpsab.snifferMode": "onSave"

Where:

<composerPath> is the path to the composer executable

on Linux, if installed with our guide: /usr/bin/composer

on MacOS, if installed with our guide: /usr/local/bin/composer

<phpcbfPath> is the path to the phpcbf executable

on Linux if installed with our Guide : /home/USERNAME/.config/composer/vendor/bin/phpcbf

on MacOS if installed with our Guide : /Users/USERNAME/.composer/vendor/squizlabs/php_codesniffer/bin/phpcbf

<phpcsPath> is the path to the phpcs executable

on Linux if installed with our Guide/home/USERNAME/.config/composer/vendor/bin/phpcs

on MacOS if installed with our Guide :   /Users/USERNAME/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs

To save you a minute here is the config for a typical dev machine 

    "phpsab.composerJsonPath": "/usr/bin/composer",
    "phpsab.debug": true,
    "phpsab.executablePathCBF": "/home/mydevuser/.config/composer/vendor/bin/phpcbf",
    "phpsab.executablePathCS": "/home/mydevuser/.config/composer/vendor/bin/phpcs",
    "phpsab.snifferShowSources": true,
    "phpsab.standard": null,
    "phpsab.snifferMode": "onSave",
    "phpsab.allowedAutoRulesets": [
        ".phpcs.xml",
        ".phpcs.xml.dist",
        "phpcs.xml",
        "phpcs.xml.dist",
        "phpcs.ruleset.xml",
        "ruleset.xml"
    ]