Phpstan Phpstorm

broken image


PHPStan is a static code analyzer for PHP which can reduce the amount of bugs in a project and make it more maintainable in the long run. With the latest release of PHPStorm 2020.3 EAP, support for PHPStan is provided directly by PHPStorm - no plugins necessary.

  1. Phpstan For Phpstorm
  2. Phpstorm Phpstan No Rules Detected
  3. Phpstan Phpstorm Docker

Let's take a look at how that can be configured and used.

Installing PHPStan

We need to install and configure PHPStan - thankfully, there already is a handy blog post we can follow.

The popular static analyzers PHPStan and Psalm will soon be supported in PhpStorm. Even better, the code behind this support will be open source. It's always been possible to run PHPStan and Psalm as external tools. In PhpStorm, we've already annotated some internal PHP functions like parseurl with # ArrayShape, so you can benefit from the attributes right away. Fortunately, the syntax of one-line attributes is backward-compatible.

Basically, require PHPStan with Composer and get started. In my case I added the extension installer and TYPO3 extension, too, as I want to use it for my TYPO3 extension development:

PHPStan doesn't check anything on the mixed type - any property or method can be called on it and it can be passed to any type in a function/method call. PHPStan has a concept of implicit and explicit mixed. Missing typehint is implicit mixed - no type was specified as a parameter type or a return type. Explicit mixed is written in the PHPDoc. PhpStorm 2020.3 EAP #2: PHPStan and Psalm Support Out-of-the-Box.

Phpstan

Next, I created a basic config file:

with this content:

Now we need to tell PHPStorm where to find both PHPStan and the configuration file.

Configure PHPStan in PHPStorm

Configure PHPStan Location in PHPStorm

  • Go to File > Settings
  • Open Languages & Frameworks > PHP > Quality Tools
  • Expand PHPStan
  • Choose your PHP Interpreter at Configuration
  • Click on ..
  • Configure the path to your phpstan executable (in a composer installation most likely /vendor/bin/composer)
  • Click on Validate to ensure PHPStorm finds it
  • Save

Configure PHPStan (Config File, Inspection) in PHPStorm

  • Go to File > Settings
  • Open Editor > Inspections
  • Expand PHP > Quality tools
  • Check the checkbox at PHPStan Global validation
  • Set the severity you want
  • If you have a custom configuration file, add it here
  • Click Apply

After closing the settings, PHPStorm will now automatically inspect your code according to your configuration.

PHPStan Pro

Phpstan Phpstorm

In case you are just getting started with PHPStan or want to use PHPStan in a legacy project, in addition of using the PHPStorm integration I would recommend to take a closer look at PHPStan Pro. Capture one slow.

PHPStan Pro comes with a nice GUI and continuous inspections. You can start it via the command line by running vendor/bin/phpstan analyse -c Build/phpstan.neon --pro - a browser window will open asking you to login. After login, you will get a list of errors / warnings that is updated as you code. I have that window running on a second screen to continuously watch my code getting better.

PHPStan Pro additionally provides interactive fixing of errors through the GUI.

Even if you don't need the PHPStan Pro features, I'd ask you to consider buying a ! license if possible as it supports the creator who is providing the main tool for free.

Links:
https://phpstan.org/user-guide/getting-started
https://blog.jetbrains.com/phpstorm/2020/10/phpstorm-2020-3-eap-2/
https://phpstan.org/blog/introducing-phpstan-pro
https://usetypo3.com/code-analysis.html
https://github.com/sascha-egerer/phpstan-typo3

PhpStorm provides code quality check through integration with the PHPStan tool, which validates your code for consistency against a set of validation rules.

To use PHPStan 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 PHPStan 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 phpstan prefix to distinguish it from PhpStorm internal inspections.

PHPStan integration is implemented via the open-source PHPStan plugin. This plugin is bundled with PhpStorm and is enabled by default.

Prerequisites

Prior to integrating PHPStan in PhpStorm, make sure the following prerequisites are met:

  • 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 PHPStan

PHPStan 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 PHPStan 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 PHPStan 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 phpstan/phpstan 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.

Phpstorm

Click next to the package record in the composer.json editor gutter to jump to the corresponding Settings/Preferences page and configure PHPStan manually.

Reset PHPStan configuration

After PHPStan is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHPStan 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 PHPStan area and click next to the Configuration list.

  3. In the PHPStan dialog that opens, empty the PHPStan 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 PHPStan configuration anew and thus apply the changes in composer.json.

Configure PHPStan 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. Arduino drink maker. Learn more about configuring PHP interpreters in Configure remote PHP interpreters or in Configure local PHP interpreters.

Choose a PHPStan 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 PHPStan area. From the Configuration list, choose the PHPStan 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 PHPStan 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 PHPStan because PhpStorm runs a script (phpstan.bat for Windows or phpstan 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 PHPStan script

  1. Download and install the PHPStan scripts.

    To check the PHPStan 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: Deezer plus ios.

  2. Register the local PHPStan 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 PHPStan area and click next to the Configuration list.

    • In the PHPStan dialog that opens, specify the location of the phpstan.bat or phpstan PHPStan executable in the PHPStan 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 phpstan.bat or phpstan ensures interaction between PhpStorm and PHPStan, 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 phpstan --version command. If validation passes successfully, PhpStorm displays the information on the detected PHPStan version.

  3. If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHPStan, whereupon the process is terminated to prevent excessive CPU and memory usage.

Configure a PHPStan script associated with a PHP interpreter

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

  2. On the Quality Tools page that opens, expand the PHPStan area and click next to the Configuration list. The PHPStan dialog opens showing the list of all the configured PHPStan 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 PHPStan 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 PHPStan dialog where the new PHPStan configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHPStan associated with it, and the advanced PHPStan options.

  4. If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHPStan, whereupon the process is terminated to prevent excessive CPU and memory usage.

Configure PHPStan as a PhpStorm inspection

Configure the PHPStan inspection with Composer

You can include the information on the PHPStan configuration file inside the scripts section of composer.json. When you install or update project dependencies, the specified configuration file will be detected and the PHPStan validation inspection will be enabled automatically.

If no configuration file is specified in the scripts section of composer.json, PhpStorm will additionally check the project root to locate the ruleset with the phpstan.neon or phpstan.neon.dist default name.

  • In the scripts section of composer.json, add the phpstan PHPStan launch command into one of the leaf elements.

    Provide the -c argument and the path to the configuration file:

    'scripts': { 'phpstan': 'vendor/bin/phpstan -c phpstan.neon' }

After PHPStan is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHPStan configuration on the PHP | Quality Tools page of the Settings/Preferences dialog Ctrl+Alt+S and update project dependencies.

Configure the PHPStan 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 PHPStan validation.

    If you have installed PHPStan 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 PHPStan tool using the controls in the Options area:

    1. From the Severity list, choose the severity degree for the PHPStan 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 Level field, provide the desired Rule Level against which the PHPStan checks are performed. Note that if a configuration file is provided, PhpStorm relies on the level value specified in this file, and the Level field is ignored.

    4. In the Configuration file field, provide the path to the PHPStan configuration file.

    5. In the Autoload file, provide the path to the autoloader. Note that if a configuration file is provided, PhpStorm relies on the autoload-file value specified in this file, and the Autoload file field is ignored.

    6. In the Memory limit field, provide the maximum amount of RAM PHPStan is allowed to allocate. You can either provide an explicit integer value in bytes or use a shorthand notation (500M, 4G, and so on).

    The Full project run (batch mode only) option is only applicable when running the inspection in batch mode. If selected, the entire project is included in the inspection run scope; otherwise, only the source roots are included.

Run PHPStan 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 PHPStan validation inspection is enabled.

    Use the Full project run (batch mode only) option to select whether the entire project or only its source roots are included in the inspection run scope.

  3. View the inspection results in the Inspection results tool window. Errors and warnings reported by PHPStan are prefixed with to distinguish them from PhpStorm internal inspections.

Phpstan

Next, I created a basic config file:

with this content:

Now we need to tell PHPStorm where to find both PHPStan and the configuration file.

Configure PHPStan in PHPStorm

Configure PHPStan Location in PHPStorm

  • Go to File > Settings
  • Open Languages & Frameworks > PHP > Quality Tools
  • Expand PHPStan
  • Choose your PHP Interpreter at Configuration
  • Click on ..
  • Configure the path to your phpstan executable (in a composer installation most likely /vendor/bin/composer)
  • Click on Validate to ensure PHPStorm finds it
  • Save

Configure PHPStan (Config File, Inspection) in PHPStorm

  • Go to File > Settings
  • Open Editor > Inspections
  • Expand PHP > Quality tools
  • Check the checkbox at PHPStan Global validation
  • Set the severity you want
  • If you have a custom configuration file, add it here
  • Click Apply

After closing the settings, PHPStorm will now automatically inspect your code according to your configuration.

PHPStan Pro

In case you are just getting started with PHPStan or want to use PHPStan in a legacy project, in addition of using the PHPStorm integration I would recommend to take a closer look at PHPStan Pro. Capture one slow.

PHPStan Pro comes with a nice GUI and continuous inspections. You can start it via the command line by running vendor/bin/phpstan analyse -c Build/phpstan.neon --pro - a browser window will open asking you to login. After login, you will get a list of errors / warnings that is updated as you code. I have that window running on a second screen to continuously watch my code getting better.

PHPStan Pro additionally provides interactive fixing of errors through the GUI.

Even if you don't need the PHPStan Pro features, I'd ask you to consider buying a ! license if possible as it supports the creator who is providing the main tool for free.

Links:
https://phpstan.org/user-guide/getting-started
https://blog.jetbrains.com/phpstorm/2020/10/phpstorm-2020-3-eap-2/
https://phpstan.org/blog/introducing-phpstan-pro
https://usetypo3.com/code-analysis.html
https://github.com/sascha-egerer/phpstan-typo3

PhpStorm provides code quality check through integration with the PHPStan tool, which validates your code for consistency against a set of validation rules.

To use PHPStan 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 PHPStan 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 phpstan prefix to distinguish it from PhpStorm internal inspections.

PHPStan integration is implemented via the open-source PHPStan plugin. This plugin is bundled with PhpStorm and is enabled by default.

Prerequisites

Prior to integrating PHPStan in PhpStorm, make sure the following prerequisites are met:

  • 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 PHPStan

PHPStan 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 PHPStan 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 PHPStan 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 phpstan/phpstan 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 PHPStan manually.

Reset PHPStan configuration

After PHPStan is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHPStan 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 PHPStan area and click next to the Configuration list.

  3. In the PHPStan dialog that opens, empty the PHPStan 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 PHPStan configuration anew and thus apply the changes in composer.json.

Configure PHPStan 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. Arduino drink maker. Learn more about configuring PHP interpreters in Configure remote PHP interpreters or in Configure local PHP interpreters.

Choose a PHPStan 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 PHPStan area. From the Configuration list, choose the PHPStan 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 PHPStan 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 PHPStan because PhpStorm runs a script (phpstan.bat for Windows or phpstan 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 PHPStan script

  1. Download and install the PHPStan scripts.

    To check the PHPStan 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: Deezer plus ios.

  2. Register the local PHPStan 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 PHPStan area and click next to the Configuration list.

    • In the PHPStan dialog that opens, specify the location of the phpstan.bat or phpstan PHPStan executable in the PHPStan 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 phpstan.bat or phpstan ensures interaction between PhpStorm and PHPStan, 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 phpstan --version command. If validation passes successfully, PhpStorm displays the information on the detected PHPStan version.

  3. If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHPStan, whereupon the process is terminated to prevent excessive CPU and memory usage.

Configure a PHPStan script associated with a PHP interpreter

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

  2. On the Quality Tools page that opens, expand the PHPStan area and click next to the Configuration list. The PHPStan dialog opens showing the list of all the configured PHPStan 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 PHPStan 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 PHPStan dialog where the new PHPStan configuration is added to the list and the right-hand pane shows the chosen remote PHP interpreter, the path to the PHPStan associated with it, and the advanced PHPStan options.

  4. If necessary, in the Tool process timeout field, specify how long you want PhpStorm to wait for a result from PHPStan, whereupon the process is terminated to prevent excessive CPU and memory usage.

Configure PHPStan as a PhpStorm inspection

Configure the PHPStan inspection with Composer

You can include the information on the PHPStan configuration file inside the scripts section of composer.json. When you install or update project dependencies, the specified configuration file will be detected and the PHPStan validation inspection will be enabled automatically.

If no configuration file is specified in the scripts section of composer.json, PhpStorm will additionally check the project root to locate the ruleset with the phpstan.neon or phpstan.neon.dist default name.

  • In the scripts section of composer.json, add the phpstan PHPStan launch command into one of the leaf elements.

    Provide the -c argument and the path to the configuration file:

    'scripts': { 'phpstan': 'vendor/bin/phpstan -c phpstan.neon' }

After PHPStan is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the PHPStan configuration on the PHP | Quality Tools page of the Settings/Preferences dialog Ctrl+Alt+S and update project dependencies.

Configure the PHPStan 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 PHPStan validation.

    If you have installed PHPStan 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 PHPStan tool using the controls in the Options area:

    1. From the Severity list, choose the severity degree for the PHPStan 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 Level field, provide the desired Rule Level against which the PHPStan checks are performed. Note that if a configuration file is provided, PhpStorm relies on the level value specified in this file, and the Level field is ignored.

    4. In the Configuration file field, provide the path to the PHPStan configuration file.

    5. In the Autoload file, provide the path to the autoloader. Note that if a configuration file is provided, PhpStorm relies on the autoload-file value specified in this file, and the Autoload file field is ignored.

    6. In the Memory limit field, provide the maximum amount of RAM PHPStan is allowed to allocate. You can either provide an explicit integer value in bytes or use a shorthand notation (500M, 4G, and so on).

    The Full project run (batch mode only) option is only applicable when running the inspection in batch mode. If selected, the entire project is included in the inspection run scope; otherwise, only the source roots are included.

Run PHPStan 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 PHPStan validation inspection is enabled.

    Use the Full project run (batch mode only) option to select whether the entire project or only its source roots are included in the inspection run scope.

  3. View the inspection results in the Inspection results tool window. Errors and warnings reported by PHPStan are prefixed with to distinguish them from PhpStorm internal inspections.

Phpstan For Phpstorm

Exclude files from PHPStan Validation inspection

Phpstorm Phpstan No Rules Detected

When waiting for PHPStan response exceeds the limit specified in the Tool process timeout field in the PHPStan dialog, PhpStorm suggests adding the file to the ignore list.

Phpstan Phpstorm Docker

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

  2. On the Quality Tools page that opens, expand the PHPStan 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 PHPStan process it again, select the file and click .

    • To remove all the files from the list, click .





broken image