The current style in Piwik's codebase is still not consistent with PSR-2, and the PhpStorm config we distribute doesn't fix that. Using PSR-2 all the way would be much simpler, and less surprising for contributors and new team members. To sum up: we should stop distributing a custom PhpStorm config and encourage using the PSR-2 default config instead. Coding style Support (PSR1/PSR2, Drupal,10 Symfony, Zend). The IDE provides code completion suggestions based on @property, @method and @var annotations. Coding Style Guide. Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated.PSR-12 is now recommended as an alternative. This guide extends and expands on PSR-1, the basic coding standard. The intent of this guide is to reduce cognitive friction when scanning code from different authors.
PhpStorm already ships with a lot of predefined language schemes that are based on common code style guidelines and standards like PSR-2. There is kind of a hidden feature in the code style settings pages where you can import these standards and set them as your current configuration. To do so simply choose your coding language in the left panel. PHPStorm comes out of the box with all the tools you need for writing PHP applications. Here are some tips and tricks about my PHPStorm configurations that I use as a Magento Developer on a daily basis. Apply PSR1/PSR2 Standard to your code ⌘ +, Code Style - PHP - Set from - PSR1/PSR2. Configure PHP Codesniffer (phpcs), PHP Mess.
Tabs or Spaces? Ok spaces, but then 2 or 4 spaces? Some of you probably will answer “doesn’t matter, it will still work”. Let me guess, you probably work alone, not in a team of developers? Cause as soon as there are multiple people pushing the code, there needs to be a coding standard. This article will show you how to adopt it.
Let’s start with a simple example.
or
Notice the difference above? { brackets are on a new line, for both class name and function edit(). No big deal, you would say?
But if we follow currently most popular PHP standards PSR-2/PSR-12, the second code is the correct one – it should be new lines.
What is the Actual Problem if Code is not Formatted?
As I said above, code styling is not a problem if you work alone, and no one else would edit your code in the future.

But for serious projects, if they grow, then the team will likely expand. Not to mention open-source projects that may have hundreds of contributors.
Now, imagine you work in a team and push the code formatted in any way you want, without looking at any standard.
And your teammate pulls down that code to make some changes. But he’s coding to the standard and his IDE is formatting code automatically (later I will show how to do it).
Then what happens – he pushes the code with the changes, and that commit has two things:
– a lot of re-formatting the code
– and then the main code of commit
See the screenshot from SourceTree – this could be a commit with a small fix:
Only three lines of code related to the actual fix, everything else is a “noise”. It’s very difficult to read for anyone who wants to review the code or to find in history what was changed exactly.
In other words, if different teammates use different coding standards (or none at all), then code will be full of re-formatting or, even worse, differently formatted code in different places.
It may sound like a non-issue, but imagine having different fonts in a big Word document. It would be still readable, but not pleasant.
What is Code Style/Standard and Why PSR-2/PSR-12?
Code standard describes the rules like:
- Tabs or Spaces? 2 Spaces or 4 Spaces? Correct answer is 4 spaces (by the way, hilarious video from “Silicon Valley” TV show)
- “{” symbol for methods – on the same line or separate line? (answer is separate line)
- Do we have to declare public/private for all class properties? (yes)
- Method names: “snake_case” or “camelCase”? (camelCase)
In PHP language, for a long time, the most popular standard has been PSR-2. In August 2019, it was replaced by PSR-12 that became a new standard.
But both are pretty similar, PSR-12 is kind of an extended PSR-2, adapted to modern PHP syntax. So you may find PSR-2 in older articles, and PSR-12 in newer ones.
Also, PSR-12 is expanding on a Basic Coding Standard called PSR-1.
In general, both PSR-12 and PSR-1 standards are very long documents and you don’t have to know all the rules. But I encourage you to read both standards at least once and note down some of the most widely used questions.
How to Reformat Code in Seconds with IDE?
I told you don’t need to learn the rules, partly because there are tools that reformate the code for us.
See quick screencast what can be done by PhpStorm by using menu Code -> Reformat Code, or just shortcut Cmd + Shift + L:

You can read more about Code Reformatting in PhpStorm here and here.
How to tell PhpStorm, what code style to adopt? It can be done in the Preferences modal window, in Editor -> Code Style -> PHP, then choose “Set from” and pick your style, I recommend PSR-12.
Notice: screenshots and shortcuts here are for Mac users, look into PhpStorm docs for Windows.
Also, I’m sure such formatters exist in other IDEs like Sublime Text, VS Code and others. So please search in their documentation.
Code Formatting Tools
Also, there are tools to automatically format many files in all your project, you can also attach to your process of deployment – so format all files either locally, or on the server.
Please Please, Push Formatting Changes in a Separate Commit
Final message. Whatever code formatting method you use, if you need to reformat the existing file and push it to the repository, please do it in a separate commit.
If done manually, you can do it in such a sequence:
- Make the code changes and commit the code
- Click “Code Reformat” in PhpStorm and if there are changes – commit with message “Code formatting”
It can also be automized with the tools mentioned above.
Conclusion
Code styling and formatting is a part of effective work in a team. So if you want to make life easier for your teammates, please agree on code standard and adapt it together.
PhpStorm provides code style check through integration with the PHP_CodeSniffer tool, which validates your code for consistency with a coding standard of your choice. You can appoint one of the predefined coding standards or use your own previously defined coding standard with the root directory outside the default PHP_CodeSniffer’s Standards directory. Moreover, you can share your custom coding style with your team.
To use PHP_CodeSniffer from PhpStorm instead of command line, you need to register it in PhpStorm and configure it as a PhpStorm code inspection. Once installed and enabled in PhpStorm, the tool is available in any opened PHP file, and no additional steps are required to launch it. The on-the-fly code check is activated upon every update in the file thus making it easy to get rid of discovered problems.
Errors and warnings reported by PHP_CodeSniffer on-the-fly are displayed as popup messages. When the tool is run in the batch mode, the errors and warnings are displayed in the Inspection Results tool window. Each message has the phpcs prefix to distinguish it from PhpStorm internal inspections. PhpStorm also integrates with the PHP Code Beautifier and Fixer tool, which lets you fix many of the detected issues.
Prerequisites
Phpstorm Property Accessed Via Magic Method
Prior to integrating PHP_CodeSniffer in PhpStorm, make sure the following prerequisites are met:
You are working with PHP_CodeSniffer version 1.5.0 and later.
The directory containing the PHP engine executable must be added to the system
path. This allows code quality tool scripts execute calls to the system-wide PHP engine.For Docker Compose-based remote interpreters, make sure to use
execmode to avoid spawning additional containers.In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP.
On the PHP page that opens, click next to the CLI Interpreter list.
In the CLI Interpreters dialog that opens, set the Lifecycle mode for the selected interpreter to Connect to existing container ('docker-compose exec').
Install and configure PHP_CodeSniffer
PHP_CodeSniffer scripts can be used as local scripts, the scripts associated with PHP interpreters, or scripts declared as project dependencies and installed via Composer, which is the preferable and recommended way.
Install PHP_CodeSniffer with Composer
Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.
When you install PHP_CodeSniffer with Composer, PhpStorm automatically downloads the necessary scripts, registers them in the IDE, and, optionally, enables and configures the corresponding code inspection.
Inside composer.json, add the squizlabs/php_codesniffer dependency record to the
requireorrequire-devsection. Press Ctrl+Space to get code completion for the package name and version.Do one of the following:
Click the Install shortcut link on top of the editor panel.
If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.
Click next to the package record in the composer.json editor gutter to jump to the corresponding Settings/Preferences page and configure PHP_CodeSniffer manually.
Reset PHP_CodeSniffer configuration
After PHP_CodeSniffer is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHP_CodeSniffer configuration.
Phpstorm Project Folder
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.
On the Quality Tools page that opens, expand the PHP_CodeSniffer area and click next to the Configuration list.
In the PHP_CodeSniffer dialog that opens, empty the PHP_CodeSniffer path field.
Update the project Composer dependencies by clicking Update on top of the composer.json editor panel. See Update dependencies for details.
PhpStorm will perform the PHP_CodeSniffer configuration anew and thus apply the changes in composer.json.
Configure PHP_CodeSniffer manually
You can use the manually downloaded local PHP code quality tool scripts or scripts associated with PHP interpreters. There can be a number of local and remote PHP interpreters, the one specified on the PHP page of the Settings/Preferences dialog is considered Project Default. Learn more about configuring PHP interpreters in Configure remote PHP interpreters or in Configure local PHP interpreters.
Choose a PHP_CodeSniffer script to use
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.
On the Quality Tools page that opens, expand the PHP_CodeSniffer area. From the Configuration list, choose the PHP_CodeSniffer script:
To use the script associated with a specific remote PHP interpreter, choose the name of this interpreter.
To use a local script, choose Local. In this case the local PHP_CodeSniffer will be executed no matter which PHP interpreter - local or remote - is used in the project. Note that there can be only one Local configuration for PHP_CodeSniffer because PhpStorm runs a script (phpcs.bat for Windows or phpcs for Linux and macOS) that contains a path to a PHP engine.
To use the script associated with the default project interpreter, that is, the one chosen on the PHP page of the Settings/Preferences dialog, choose By default project interpreter.
Phpstorm Profiling
Configure a local PHP_CodeSniffer script

Download and install the PHP_CodeSniffer scripts.
To check the PHP_CodeSniffer installation, switch to the installation directory and run the following command:
If the tool is available, you will get a message in the following format:
PHP_CodeSniffer version <version> (stable) by Squiz. (http://www.squiz.net)To have code checked against your own custom coding standard, create it. Store the rules and the ruleset.xml file that points to them in the coding standard root directory.
Register the local PHP_CodeSniffer script in PhpStorm:
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.
On the Quality Tools page that opens, expand the PHP_CodeSniffer area and click next to the Configuration list.
In the PHP_CodeSniffer dialog that opens, specify the location of the phpcs.bat or phpcs PHP_CodeSniffer executable in the PHP_CodeSniffer path field. Type the path manually or click and select the relevant folder in the dialog that opens.
To check that the specified path to phpcs.bat or phpcs ensures interaction between PhpStorm and PHP_CodeSniffer, that is, the tool can be launched from PhpStorm and PhpStorm will receive problem reports from it, click the Validate button. This validation is equal to running the
phpcs --versioncommand. If validation passes successfully, PhpStorm displays the information on the detected PHP_CodeSniffer version.
If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHP_CodeSniffer, whereupon the process is terminated to prevent excessive CPU and memory usage.
If necessary, in the PHP Code Beautifier and Fixer Settings area provide the path to the PHP Code Beautifier and Fixer tool in the Path to phpcbf field. This will let you automatically fix many of the errors detected by PHP_CodeSniffer. If you install PHP_CodeSniffer with Composer, PHP Code Beautifier and Fixer is detected and set up automatically.
Configure a PHP_CodeSniffer script associated with a PHP interpreter
In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP | Quality Tools.
On the Quality Tools page that opens, expand the PHP_CodeSniffer area and click next to the Configuration list. The PHP_CodeSniffer dialog opens showing the list of all the configured PHP_CodeSniffer scripts in the left-hand pane, one of them is of the type Local and others are named after the PHP interpreters with which the scripts are associated.
Click on the toolbar. In the PHP_CodeSniffer by Remote Interpreter dialog that opens, choose the remote PHP interpreter to use the associated script from. If the list does not contain a relevant interpreter, click and configure a remote interpreter in the CLI Interpreters dialog as described in Configure remote PHP interpreters.
When you click OK, PhpStorm brings you back to the PHP_CodeSniffer dialog where the new PHP_CodeSniffer configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHP_CodeSniffer associated with it, and the advanced PHP_CodeSniffer options.
If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHP_CodeSniffer, whereupon the process is terminated to prevent excessive CPU and memory usage.
If necessary, in the PHP Code Beautifier and Fixer Settings area provide the path to the PHP Code Beautifier and Fixer tool in the Path to phpcbf field. This will let you automatically fix many of the errors detected by PHP_CodeSniffer. If you install PHP_CodeSniffer with Composer, PHP Code Beautifier and Fixer is detected and set up automatically.
Configure PHP_CodeSniffer as a PhpStorm inspection
Configure the PHP_CodeSniffer inspection with Composer
Phpstorm Prettier On Save
You can include the information on the default and custom PHP_CodeSniffer rulesets inside the scripts section of composer.json. When you install or update project dependencies, the specified rulesets will be detected and the PHP_CodeSniffer validation inspection will be enabled automatically.
If no ruleset is specified in the scripts section of composer.json, PhpStorm will additionally check the project root to locate the ruleset with the phpcs.xml default name. If the file is present, it will be automatically selected as the inspection's Custom ruleset.
In the
scriptssection of composer.json, add thephpcsPHP_CodeSniffer launch command into one of the leaf elements.Provide the
--standardargument to denote the coding standard used. For example, adding the following record will set the coding standard to PSR2:Adding the following record will set the coding standard to Custom and the path to the ruleset to <project root>/phpcs.xml:
'scripts': { 'phpcs': 'phpcs --standard=phpcs.xml' }
Additionally, you can provide a custom non-PSR standard dependency inside the require-dev section of composer.json to have it detected automatically. Currently, the following standards are supported:
After PHP_CodeSniffer is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHP_CodeSniffer configuration on the PHP | Quality Tools page of the Settings/Preferences dialog Ctrl+Alt+S and update project dependencies.
Configure the PHP_CodeSniffer inspection manually
In the Settings/Preferences dialog Ctrl+Alt+S, click Inspections under Editor.
On the Inspections page that opens, expand the PHP | Quality Tools node and select the checkbox next to PHP_CodeSniffer validation.
If you have installed PHP_CodeSniffer with Composer but the corresponding inspection is currently disabled, PhpStorm highlights its record in composer.json. Press Alt+Enter and use the provided Enable inspection quick-fix to enable the inspection and open the Inspections page.
On the right-hand pane of the page, configure the PHP_CodeSniffer tool using the controls in the Options area:
From the Severity list, choose the severity degree for the PHP_CodeSniffer inspection. The selected value determines how serious the detected discrepancies will be treated by PhpStorm and presented in the inspection results.
From the Scope list, choose the scope to limit the inspection application to.
In the Check files with extensions field, provide the comma-separated list of file extensions that should be checked by PHP_CodeSniffer.
To have PHP_CodeSniffer report warnings in addition to errors, select the Show warnings as... checkbox and choose the severity degree from the list. To have only errors reported and suppress reporting warnings, clear Show warnings as... checkbox.
If you are relying on a custom third-party coding standard (for example, Coder or Joomla Coding Standards ), you need to integrate it with PHP_CodeSniffer prior to using it. To do this, select the Installed standard paths checkbox, click and select the custom standard installation directory in the dialog that opens.
Select the Show sniff name checkbox to have the corresponding sniff's name displayed in the editor or the inspection results in addition to the inspection summary.
Appoint the coding standard to apply.
To use one of the predefined coding standards, select it the Coding standard list, appoint the coding style to check your code against. The list contains all the coding standards installed inside the main php_codesniffer directory structure.
To have your code checked against your own previously defined coding standard, choose Custom. Click and in the Custom Coding Standard dialog that opens, specify the path to the ruleset.xml file for your own coding standard in the Path to ruleset field. Type the path manually or click and choose the relevant folder in the dialog that opens.
Phpstorm Psr2 Checker
Fix issues detected by PHP_CodeSniffer

PhpStorm integrates with the PHP Code Beautifier and Fixer tool, which lets you fix many of the detected issues. If you install PHP_CodeSniffer with Composer, PHP Code Beautifier and Fixer will be detected and set up automatically. Otherwise, you need to set it up manually.
Fix issues by using a quick-fix
Upon detecting an issue, PhpStorm highlights it in accordance with the PHP_CodeSniffer inspection settings.
Place the caret at the detected issue and click , or press Alt+Enter.
Select PHP Code Beautifier and Fixer: fix the whole file from the popup menu. Note that this will fix all issues detected in the current file.
You can also detect issues in the batch mode, or fix them automatically with code cleanup.
Fix issues automatically with Code Cleanup
From the main menu, select Code | Code Cleanup.
In the Specify Code Cleanup Scope dialog that opens, select ths scope to which you want the inspection profile to be applied.
Select the inspection profile from the list, or click to configure a new profile in the Code Cleanup Inspections dialog that opens. You can also click to check, which fixes will be applied and make sure that the PHP_CodeSniffer validation inspection is enabled.
Click OK to launch code cleanup.
Run PHP_CodeSniffer in the batch mode
From the main menu, select Code | Inspect code.
Select the inspection profile from the list, or click to configure a new profile in the Inspections dialog that opens. You can also click to check, which fixes will be applied and make sure that the PHP_CodeSniffer validation inspection is enabled.
View the inspection results in the Inspection results tool window. Errors and warnings reported by PHP_CodeSniffer are prefixed with
phpcsto distinguish them from PhpStorm internal inspections.
Exclude files from PHP_CodeSniffer Validation inspection
When waiting for PHP_CodeSniffer response exceeds the limit specified in the Tool process timeout field in the PHP_CodeSniffer dialog, PhpStorm suggests adding the file to the ignore list.
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.
On the Quality Tools page that opens, expand the PHP_CodeSniffer area and click the Show ignored files link.
To add a file, click and locate the desired file in the dialog that opens.
To delete a file from the list and have PHP_CodeSniffer process it again, select the file and click .
To remove all the files from the list, click .




