This ruleset for PHPCS and PHPCBF adheres to PSR 12 with three changes:
-
Stroustrup brackets (a K&R variant).
- The opening bracket is always on the same line
- No else-cuddling or other compression of control structures, i.e.
else
andcatch
keywords are on their own line (followed by the opening bracket).
-
Spaces in Parentheses
- One space before and after statement inside parentheses in control structures
- One space after ! in control structures
-
Space around functions
- One blank line before and after functions
Inspired by this question on Stackoverflow.
<?php
namespace Bar;
class Foo {
public function baz() {
if ( ! false ) {
echo('Hello World!');
}
else {
die();
}
}
}
-
Install Composer.
-
Add the following section to your
composer.json
(perhaps preferrably in the global context). This makes this repository visible for composer (replace the github address with your own if you fork the project). You can locate yourcomposer.json
withcomposer config home
orcomposer global config home
.
{
"repositories": [
{
"type": "vcs",
"url": "[email protected]:nicolajknudsen/psr12-stroustrup-and-space"
}
]
}
- Run the following to install the project and dependencies, including dev dependencies. Add a
global
if you are installing globally. Allow the plugin from "dealerdirect/phpcodesniffer-composer-installer" when prompted.
composer require nicolajknudsen/psr12-stroustrup-and-space:dev-master
That's it. Check that the coding standard is available by running phpcs -i
.
To make use of the linting and fixing ability af PHPCS and PHPCBF in VS Code, complete the following steps:
- Install the PHPSAB extension in VS Code.
- Point the config to
/path/to/composer/vendor/bin
. Follow the guide in the extension documentation, or just opensettings.json
in VS Code withctrl+shift+P
and add the following config:
{
"phpsab.executablePathCS": "/path/to/composer/vendor/bin/phpcs",
"phpsab.executablePathCBF": "/path/to/composer/vendor/bin/phpcbf",
"phpsab.standard": "psr12-stroustrup-and-space",
"phpsab.debug": true,
"phpsab.fixerEnable": true,
"phpsab.snifferEnable": true,
"phpsab.snifferShowSources": true
}