Code standards

Company wide policies for writing code.

PHP coding standards

We use phpcs and phpcbf along with WordPress and CiviCRM rulesets.

Ensure that your global composer bin path (typically $HOME/.config/composer/vendor/bin) is in your $PATH environment variable.

For CiviCRM code (including extensions and supporting tools) we use CiviCRM's coding standards.

You can configure VS Code to use CiviCRM's coding standards as follows:

  1. Add the CiviCRM fork of Drupal's coding standards to the global composer.json repositories attribute.

      "repositories": [
        {
          "type": "vcs",
          "url": "https://github.com/civicrm/coder"
        }
      ]
    
  2. Require the CiviCRM and WordPress coding standards.

    composer global require drupal/coder "dev-8.x-2.x-civi"
    composer global require wp-coding-standards/wpcs
    
  3. Download the PHP Sniffer & Beautifier for VS Code extension

  4. Add the following configuration to your VS Code settings (replacing {user} with your username)

    {
      "[php]": {
        "editor.defaultFormatter": "valeryanm.vscode-phpsab"
      },
      "phpsab.executablePathCS": "/home/{user}/.config/composer/vendor/bin/phpcs",
      "phpsab.executablePathCBF": "/home/{user}/.config/composer/vendor/bin/phpcbf",
      "phpsab.fixerArguments": ["--extensions=php"],
      "phpsab.snifferArguments": ["--extensions=php"]
    }
    

Configuring project repositories

Add the phpcs.xml files as follows. Only the first one is needed for Drupal sites. It should be placed at the root of the repo.

/phpcs.xml

<?xml version="1.0"?>
<ruleset name="Third Sector Design CiviCRM on WordPress" namespace="ThirdSectorDesign">

    <rule ref="Drupal" />
    <arg name="extensions" value="php" />

</ruleset>

For WordPress sites also add the following three files.

/src/wordpress/phpcs.xml

<?xml version="1.0"?>
<ruleset name="Third Sector Design CiviCRM on WordPress" namespace="ThirdSectorDesign">

    <rule ref="WordPress" />
    <arg name="extensions" value="php" />

</ruleset>

/src/wordpress/wp-content/plugins/civicrm/civicrm/phpcs.xml

<?xml version="1.0"?>
<ruleset name="Third Sector Design CiviCRM on WordPress" namespace="ThirdSectorDesign">

    <rule ref="Drupal" />
    <arg name="extensions" value="php" />

</ruleset>

src/wordpress/wp-content/civicrm-extensions/phpcs.xml

<?xml version="1.0"?>
<ruleset name="Third Sector Design CiviCRM on WordPress" namespace="ThirdSectorDesign">

    <rule ref="Drupal" />
    <arg name="extensions" value="php" />

</ruleset>