Phpstorm Linter

  



  1. Phpstorm Line Count
  2. Phpstorm Js Linter
  3. Phpstorm Lint
  4. Phpstorm Linter Settings

Lots of PHP developers create their applications using Laravel, a free, open source PHP web application framework. It is built on top of several Symfony components, and provides a development framework that makes common tasks such as authentication, routing, sessions and caching much easier to implement. WebStorm/PHPStorm plugin for Stylus Linter idea-plugin idea jetbrains plugin stylus linter stlint 35 commits 2 branches 0 packages 0 releases Fetching contributors MIT Java. Java 100.0%; Branch: master. New pull request Find file. Clone or download Clone with HTTPS. Phplint is a Phpstorm plugin for phpLint!

“We must run as fast as we can, just to stay in place” – Lewis Carroll

Qodana is a new product from JetBrains that brings the “smarts” of JetBrains IDEs directly into your CI Pipeline.

PhpStorm and our other JetBrains IDEs are renowned for their ability to understand your code. They can track declarations, check references and types, and collect possible execution paths. Thanks to all this static analysis, you are able to rename and move things around safely, get appropriate code completion, or be warned with a message saying “hey, this won’t run” from the code inspection as you are editing your code.

Having all this while you are working in the editor is extremely helpful, but modern CI-centric workflows require having a reliable quality gate in your build pipeline. The multitude of tools being actively developed and used clearly support this statement.

It was technically possible to run JetBrains IDE inspections for the whole project in headless batch mode well before the CI era. However, integrating an IDE into the CI pipeline as a typical linter proved difficult. Our users have been requesting help in this area for quite some time and have actively tried to implement solutions themselves, and some have even published custom images in Docker Hub to help the community. It was hard to address many of the problems without full access to the IDE codebase, and the practice also raised some legal concerns because the IDE licenses did not explicitly allow such usage.

Qodana is the solution

Qodana comprises two main parts: a nicely packaged GUI-less IntelliJ IDEA engine tailored for use in a CI pipeline as a typical “linter” tool, and an interactive web-based reporting UI.

It makes it easy to set up workflows to get an overview of the project quality, set quality targets, and track progress on them. You can quickly adjust the list of checks applied for the project and include or remove directories from the analysis.

The example above shows how to exclude a source directory or inspection category from the analysis. Another way to configure this is to use the qodana.yaml file. See the details in our user guide.

Demo of popular projects

We’ve created a playground for you to see Qodana in action for popular projects: Laravel, Doctrine ORM, PHPUnit, and WordPress. Although the code in these projects may run on millions of machines daily, with the help of Qodana you can easily see what can be improved. It doesn’t mean that the code is bad, it just means that the technologies are evolving, the surroundings are changing, and we need to keep up.

In the example report above that was run on WordPress, you can see that there are 99 cases in 34 files where regular expressions can be simplified. You can click Open in IDE and apply quick-fixes to these regular expressions. As a result, the next developer who works with these files will be able to spend less time trying to work out why this > symbol is included and whether it is redundant or not.

To open the sandbox playground use the following link https://qodana.beta.teamcity.com/ and select the Login as guest option to explore the UI integrated into the JetBrains cloud CI, TeamCity. You can use the Qodana tab to see what problems it found, browse the list of inspections that it ran, and try out the different options for yourself. Once you have an idea of how it works and what you can do with it, then it’s time to take the next step and try Qodana on your own project.

Using Qodana on your project

Qodana is packed into a ready-to-use Docker image. You can run it:

  • By manually invoking it on your projects.
  • By integrating it into your CI gateway.
  • Using GitHub Actions.
  • On JetBrains TeamCity standalone and Cloud.

Docker image

Pull the image from Docker Hub:

Run the command below, replacing source-folder with your project’s root path and open the URL printed in the command line output to view the report.

If you use Composer in your project or want to set a certain language level, please check our user guide. You can find more information about the Docker image here.

GitHub Actions

Qodana can run on any CI tool, but for simplicity let’s take a look at how you can use it with GitHub Actions.

The user guide describes a simple, fully functional GitHub Actions workflow that uses the Docker image. Integration into other CIs can be achieved in a similar manner. You can control the execution by providing conditions via qodana.yaml and pass the results in JSON format to further steps in your pipeline.

TeamCity

If you have your own installation of TeamCity, you can install the Qodana plugin from JetBrains Marketplace. After extending your build with Qodana, you’ll get the following capabilities:

  • An interactive report in a separate build tab.
  • Compare problems and checks applied between builds.
  • Report discovered problems as standard TeamCity tests so that you can assign investigations to the responsible team members.
  • Use flexible build failure conditions.
  • View aggregate statistics for static code analysis metrics.

The screenshot above shows a report for failed inspections grouped by type. In this example, 17 problems related to PhpHierarchyChecksInpections were grouped together. You can also choose grouping by file or module, or create a separate assignable failed “test” for every problem that is found.

Read more about the Qodana TeamCity plugin in the user guide.

TeamCity Cloud

If you don’t have TeamCity yet but would like to give it a try, you can quickly and easily create a TeamCity Cloud instance. Once you’ve done that, contact us at qodana-support@jetbrains.com and we will install the latest version of the Qodana plugin in your instance.

What’s next?

We are working on a public roadmap to let you see in advance where things are going. Right now we’re working on polishing all the features described above and developing many more, for example, revision/branch comparison, high-level quality targets, an inspection profile exchange, cross-project analysis for code duplication, third-party license monitoring, and security checks including taint analysis.

Try it now

JetBrains Qodana is now available under an Early Access Program (EAP). During the EAP users will have full access to Qodana Docker, Qodana TeamCity Plugin, and Qodana GitHub Application free of charge. While we try to keep EAP releases stable, they have not undergone the same degree of testing as a full public release. There can be flaws and there will be rapidly evolving changes in the UI and configurations.

Detailed instructions on how to start using Qodana are available in our documentation. We will be grateful for any feedback, and any ideas are welcome! Contact us at qodana-support@jetbrains.com or via our issue tracker.

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.

Phpstorm line endingsPhpstorm

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.

    1. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP.

    2. On the PHP page that opens, click next to the CLI Interpreter list.

    3. 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.

  1. Inside composer.json, add the squizlabs/php_codesniffer dependency record to the require or require-dev section. Press Ctrl+Space to get code completion for the package name and version.

  2. 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.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.

  2. On the Quality Tools page that opens, expand the PHP_CodeSniffer area and click next to the Configuration list.

  3. In the PHP_CodeSniffer dialog that opens, empty the PHP_CodeSniffer path field.

  4. 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

Phpstorm Line Count

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

  1. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.

  2. 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

  1. 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.

  2. 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 --version command. If validation passes successfully, PhpStorm displays the information on the detected PHP_CodeSniffer version.

  3. 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.

  4. 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

Phpstorm Js Linter

Phpstorm
  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP | Quality Tools.

  2. 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.

  3. 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.

  4. 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.

  5. 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 the phpcs 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

  1. In the Settings/Preferences dialog Ctrl+Alt+S, click Inspections under Editor.

  2. 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.

  3. On the right-hand pane of the page, configure the PHP_CodeSniffer tool using the controls in the Options area:

    1. 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.

    2. From the Scope list, choose the scope to limit the inspection application to.

    3. In the Check files with extensions field, provide the comma-separated list of file extensions that should be checked by PHP_CodeSniffer.

    4. 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.

    5. 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.

    6. 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.

    7. 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.

  1. Place the caret at the detected issue and click , or press Alt+Enter.

  2. 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

  1. From the main menu, select Code | Code Cleanup.

  2. In the Specify Code Cleanup Scope dialog that opens, select ths scope to which you want the inspection profile to be applied.

  3. 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.

  4. Click OK to launch code cleanup.

Run PHP_CodeSniffer in the batch mode

  1. From the main menu, select Code | Inspect code.

  2. 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.

  3. 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.

Phpstorm Lint

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.

Phpstorm Linter Settings

  1. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Quality Tools.

  2. 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 .