PhpStorm 2023.3 Help

Psalm

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

To use Psalm 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 Psalm on the fly are highlighted in the editor in the same way as errors and warnings from PhpStorm's internal code inspections. When the tool is run in batch mode, the errors and warnings are displayed in the Problems tool window. Each message has the psalm prefix to distinguish it from PhpStorm internal inspections.

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

Prerequisites

Prior to integrating Psalm 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 to execute calls to the system-wide PHP engine.

  • For Docker Compose-based remote interpreters, make sure to use docker-compose exec mode to avoid spawning additional containers.

    1. In the Settings dialog (Ctrl+Alt+S) , go to PHP.

    2. On the PHP page that opens, click the Browse button 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').

      docker-compose exec for PHP interpreter

Install and configure Psalm

Install Psalm with Composer

When you install Psalm 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 vimeo/psalm dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

  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.

Clicking the Settings button next to the package record in the composer.json editor gutter will take you to the corresponding Settings page where you can configure Psalm manually.

Gutter icon for psalm settings in composer.json

Reset Psalm configuration

After Psalm is initially configured, further modifications in composer.json will not affect the inspection configuration. To apply newer changes, reset the Psalm configuration.

  1. In the Settings dialog (Ctrl+Alt+S) , navigate to PHP | Quality Tools | Psalm.

  2. Click the Browse button next to the Configuration list.

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

    Empty Psalm path field
  4. Update the project Composer dependencies by clicking Update on top of the composer.json editor panel. For more information, refer to Update dependencies.

PhpStorm will perform the Psalm configuration anew and thus apply the changes in composer.json.

Configure Psalm in PhpStorm

When you install Psalm with Composer, PhpStorm automatically detects Psalm's executable file in the vendor/bin folder and sets the PHP interpreter configured in the system path to run it.

In Settings (Ctrl+Alt+S) | PHP | Quality Tools | Psalm, you can change the default PHP interpreter, set the path to a manually downloaded and installed Psalm executable file, or add some options to be passed to Psalm when running it in PhpStorm.

Psalm settings
  • Configuration: in this field, you can change the default PHP interpreter and path to the Psalm executable file.

    1. To only change the interpreter, select the required item from the list of local and remote PHP interpreters configured in your project.

    2. To change the path to the Psalm executable file, or both the interpreter and the path, click the Browse button next to the Configuration list to open the Psalm dialog and edit the fields there as described on the Psalm Dialog reference page.

  • Show ignored files: use this setting to exclude files from Psalm validation inspection. For more information, refer to the Quality Tools reference page.

  • Options: in this area, add the options to run Psalm as a PhpStorm inspection with. Edit the fields there as described on the Psalm reference page.

Enable Psalm as a PhpStorm inspection

There are two ways to set up Psalm as a PhpStorm inspection: automatically during Psalm installation with Composer, or manually in PhpStorm's inspections settings.

Enable the Psalm inspection with Composer

You can include the information on the Psalm 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 Psalm 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 psalm.xml or psalm.xml.dist default name.

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

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

    "scripts": { "psalm": "vendor/bin/psalm --config=psalm.xml" }

Enable Psalm validation in Inspections settings

  1. In the Settings 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 Psalm validation.

  3. On the right-hand pane of the page, configure how PhpStorm handles the Psalm inspection output:

    1. Scope: choose the scope to limit the inspection application to.

    2. Severity: choose the severity degree for the inspection. The selected value determines how seriously the detected discrepancies will be treated by PhpStorm and presented in the inspection results.

    3. Highlighting in editor: choose how the issues detected by the inspection are highlighted in the editor.

Initialize Psalm caches

While the Psalm validation inspection can be run on a single file, Psalm will still make a pass on the entire project. On large projects, waiting for Psalm response when running it for the first time can exceed the limit specified in the Tool process timeout field in the Psalm dialog. In such cases, PhpStorm displays the error message:

Psalm validation inspection timeout error
  • Click the Init cache link to have Psalm make a pass on the entire project and generate caches in the configured cache directory. If you are using a remote interpreter, make sure to set the cache directory to the location, which is accessible from within the remote environment.

  • Click the Exclude from Psalm analysis link to add the current file to the ignored files list.

Run Psalm in batch mode

  1. In the main menu, go to Code | Inspect code.

  2. In the Specify Inspection Scope dialog that opens, select the inspection profile from the list, or click Configure to open the Inspections dialog and configure a new profile.

    You can also click Configure to check which fixes will be applied within the scope of the selected inspection profile, and make sure that the Psalm validation inspection is enabled.

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

Exclude files from Psalm Validation inspection

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

  1. In the Settings dialog (Ctrl+Alt+S) , navigate to PHP | Quality Tools | Psalm.

  2. Click the Show ignored files link.

    • To add a file, click the Add button and locate the desired file in the dialog that opens.

    • To delete a file from the list and have Psalm process it again, select the file and click the Remove button.

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

Use extended @psalm annotations

Besides regular PHPDoc comments, PhpStorm supports Psalm-specific annotations, which are used by Psalm for performing code analysis. When specifying an annotation, in most cases you can omit the @psalm- part. To use code completion, press Ctrl+Space.

Providing a Psalm annotation

For more information, see PHP type checking.

Last modified: 25 March 2024