Phplint is a Phpstorm plugin for phpLint! Protocol Buffer Linter. Compatible with all IntelliJ-based IDEs. Protolint plugin for all products based on IntelliJ Platform (IntelliJ IDEA, GoLand, RubyMine, WebStorm, PhpStorm, PyCharm, AppCode, etc.) The plugin does not require configuration by default, for the majority of projects it should work. In PhpStorm, we can configure code style through Project Settings Code Style and use one of the many available schemes. There are also various inspections that check our code against the code style. We can add additional code style inspections to PhpStorm using tools like PHP Code Sniffer. By inspecting PHP, JavaScript and CSS files PHP Code.
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
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
exec
mode 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
require
orrequire-dev
section. 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.
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 Fontexplorer mac free download. 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.
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:
Download sims 3 generations mac. 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 --version
command. 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
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
scripts
section of composer.json, add thephpcs
PHP_CodeSniffer launch command into one of the leaf elements.Provide the
--standard
argument 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.
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
phpcs
to 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 .
PHP Code Standards in 2020.
Following coding standards in PHP is now easier than ever. Let’s go over the benefits of following a coding standard, how to enforce it, and look at how modern tooling and new features in IDE’s like PHPStorm makes it nicer than ever before.
Why Follow a Coding Standard?
- Stop Wasting Time
If you’ve ever worked with another developer or worked on a team, no doubt you’ve debated coding styles. Focus on what’s important (getting things done) and stop debating:
– Tabs vs spaces
– camelCase vs snake_case
– Opening brackets {} on new line or same line
– Control structure format
– TRUE/FALSE/NULL vs true/false/null
– Spacing that could go anywhere and everywhere
– … the list is never ending. - More Readable Code
Easier to read code comes from using a consistent code style. Your brain isn’t distracted by the personal preferences of the previous developer. In Jason McCreary’s book Basecode, he does an excellent job of explaining a concept which Kevlin Henney has coined “visual honesty”. It’s the concept that you should be able to generally tell what’s happening in a function just by the shape of the code blocks. He illustrates this by showing that if all the characters were replaced with X’s, when code is formatted properly, you can still recognize what’s happening. I had noticed the benefits of this before, but had a hard time putting it into words; what an excellent way to demonstrate it! - Save Money
Time is money, whether it’s your time or the company’s. Save time in both new developer on-boarding,as well as on code maintenance.New hire ramp-up time can be reduced if your codebase is in a popular format. It increases the chances the new developer has previous experience developing, reading and writing code in that format. For maintenance, code reviews are simpler, as there are less stylistic changes from developer to developer. As code reviews and new hires are a constant, this savings in developer time translates to real cost savings for the business over time.
EditorConfig
At the very least, a project in any language should add an EditorConfig. EditorConfig is a widely adopted open standard that helps maintain general coding styles. It’s a file which sits in the root of your project that many IDE’s will automatically (or with a plugin) read settings to set tabs vs spaces, tab length to 4 spaces, etc. PHPStorm now has EditorConfig support built-in since PHPStorm 2019.2, VS Code has a plugin, and so does almost all code editors.
EditorConfig can only specify generic coding style settings, for the rest of the article we’ll look at standards and tools that go a lot further, specific to PHP.
PSR-12
In 2009, all the popular frameworks came together (PHP-FIG) and agreed on common coding standards PSR-1 and PSR-2. Since then, PSR-2 (which builds upon PSR-1) had become the defacto coding standard in PHP, which is nice because you know any framework or 3rd-party php package you come across will follow the same coding style.
In August 2019, PSR-12 was approved, adding rules for new language features introduced in PHP 7. PSR-12 is now the recommended standard to follow.
But We Can’t Follow PSR-12 Because Of ________
Here’s some of the popular reasons holding team’s back from following PSR-12:
“We have so much code already, it’d be too much work to convert it all.”
“We have certain folders we can’t (or don’t want to) touch.”
“There’s certain rules in PSR-12 that would break our code”
“There’s rules in PSR-12 my team strongly disagrees with.”
Historically, it’s been difficult to perform large stylistic changes on an existing code base. Luckily there are tools which can convert all your files for you! We’ll walk through these tools in “Linters”.
There might be disagreement around the merit of specific rules within a coding standard. The tooling today easily addresses these concerns. It’s super easy to extendthe PSR-12 coding standard and exclude certain folders or rules. We’ll walk through how to do this in Creating a Configuration File.
Linters
PHP_CodeSniffer
PHP_CodeSniffer is a linter command line tool for following PHP coding standards. You can install it globally so that from any project you can run the phpcs
command to analyze files and phpcbf
to fix them. Starting in version 3.5 has support for PSR-12.
Install PHP_CodeSniffer (Globally)
Install PHP_CodeSniffer (Project Dependency)
Having it installed globally is handy, the command is short so no matter what project you jump into it’s easy to remember. But I recommend also installing it as a composer dev dependency in your project. This way, no matter what, it’s installed for all your teammates and you all have the same version.
Then you can run it the same way as demonstrated globally above, you just need to specify the vendor bin path where composer installs it to.
We can avoid potentially forgetting to pass --standard=PSR12
by creating a phpcs.xml configuration file like we’ll demonstrate below.
If you find it annoying to type out the entire vendor path or have a hard time remembering the vendor path, maybe create a composer run-script which we’ll also go over below.
Even so, I’ll be honest, I usually just use the globally installed version; it’s easier to just type phpcs
, and PHP_CodeSniffer doesn’t change too much from version to version. But I install it as a dev dependency anyway so that it’s there if I need it to run it on a server or another system. Plus there’s an added benefit that by setting it as a dev dependency and creating the composer run-script, our teammates IDE’s can set the coding standard automatically for them.
Create a phpcs.xml Configuration File
Like I said above, you may Extend PSR-12 if you want to exclude files or folders, or exclude certain rules. By placing a phpcs.xml
file at the root of your project, anyone who runs phpcs
doesn’t have to worry about passing any arguments to set the coding standard to PSR-12 or any custom settings for your project. PHP_CodeSniffer will automatically read them from this config file instead.
As a simple example, here’s how you create a custom standard that extends PSR-12 but excludes the 120 character line length limit, adds a rule to disallow long array syntax, and ignores a /views/ folder.
phpcs.xml
Here’s a more complex example: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xml.dist
And full documentation can be found here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset
Create a Composer Script
Remembering the path to the local installation of phpcs
and typing it out every time can get tedious, not to mention remembering to pass the flag for what standard to use if you don’t need a phpcs.xml
, so you can create a composer script to make it easier:
Then you only have to run:
This has 2 additional benefits:
- PHPStorm 2020.1 will provide play buttons to run composer run-scripts with just one click.
- PHPStorm can automagically set your standard in PHPStorm.
PHP CS Fixer
PHP CS Fixer is an alternative linter to PHP_CodeSniffer. It doesn’t have PSR-12 yet, but they are working to add PSR-12 support. It does, however, include all the other same popular standards that PHP_CodeSniffer supports, like PSR-12’s predecessor PSR-2 and others.
As well, PHP CS Fixer also includes support for the Symfony coding standard. It takes the PSR-2 coding standard (PSR-12 soon) and builds upon it with coding opinions common in their framework’s community. I personally really like it, except I do tweak a couple settings. I like to turn off yoda_style, and turn on a couple handy fixers that PHP_CodeSniffer doesn’t have, like sorting import statements alphabetically, and removing unnecessary fully qualified class names.
Unlike PHP_CodeSniffer where you set the standard/rules in an xml file, for PHP CS Fixer you set them in a PHP file (either .php_cs or .php_cs.dist). Here’s an example for PHP CS Fixer on how to set the standard as the Symfony coding standard and make the other tweaks I just mentioned:
.php_cs.dist
Documentation for these and other settings can be here: https://github.com/FriendsOfPHP/PHP-CS-Fixer#config-file
IDE Support
PHPStorm
PHPStorm + PHP Linters = 🔥. IntelliJ has made linters a first class citizen in their PHPStorm IDE. There’s no plugin needed. You can utilize PHP_CodeSniffer or PHP CS Fixer right inside the IDE so you don’t have to run commands on the command line. What’s nice about this integration is that it will help you follow the PSR-12 coding standard in real-time as you write code.
Here’s the 3 steps you’ll need to do in order to get PHPStorm setup with either PHP_CodeSniffer or PHP CS Fixer:
- Set the “Code Style”
By setting the code style we make sure that when you reformat entire classes, generate code, or refactor code, PHPStorm will follow PSR-12 when it rewrites your code for you. Starting in PHPStorm 2019.3, PSR-12 is available as one of the built-in code styles to pick from.
Preferences > Editor > Code Style > PHP > “Set from…” > Predefined Style > PSR12
- Turn on “Code Quality Tools”
We need to validate that PHPStorm knows where the linter is installed for when it needs to use it to lint your code.
Preferences > Languages & Frameworks > PHP > Quality Tools > Code Sniffer
Click the ellipsis (…) and use the validate button to make sure it can detect where PHP_CodeSniffer is installed.
- Turn on “Inspections”
By turning on Inspections for the linter, PHPStorm will put a squiggly line below any code for any issues the linter detects. Opening the intention actions menu on an inspection will even provide an action to fix the issue for you starting in PHPStorm 2019.1 for PHP_CodeSniffer (and PHPStorm 2018.3 for PHP-CS-Fixer). You can see this in action in the gif at the top of this article!
Preferences > Editor > Inspections > PHP > Quality Tools > PHP_CodeSniffer Validation ✅
Set the Coding Standard Automatically for Teammates
PHPStorm recently made it super easy to have the coding standard set automatically for all your team members by simply adding a line to your composer.json:
Note: You’re teammates must use Composer through PHPStorm (eg. Tools > Composer > Install/Update) for it to automatically set the path and turn on the inspections for them.
Visual Studio Code
PHPStorm is probably the most popular IDE for PHP, but Visual Studio Code (VS Code) is becoming a very popular (and free) IDE for PHP developers. You can install the phpcs plugin for PHP_CodeSniffer integration or the php cs fixer plugin for PHP CS Fixer integration.
Sublime Text, Vim, Atom & Others
Sublime Text has sublime-phpcs. Vim has Syntastic or Ale. Atom has a linter-phpcs package. As for PHP CS Fixer, they have a list of plugins for various editors right on their Github readme.
Taking It a Step Further
So now you’ve got linting working on the command-line, you’ve reformatted your codebase to PSR-12, and your IDE of choice is setup to lint in real-time! Now how do you keep your codebase PSR-12 compliant? Ever opened up a class to find others had committed a bunch of code that doesn’t follow PSR-12? Do you find yourself having to run phpcs
across the whole codebase every couple months?
In my next blog post we’ll go over how to make sure PSR-12 errors never makes it into master again. I’ll go a step further and show how you can catch PSR-12 violations before they even make it into a commit! Subscribe via RSS, or follow me on twitter to find out about the next blog post comes out!
Linter Phpstorm
Also published on Medium.
