Installation#
The latest version of Psalm requires PHP >= 8.2 and Composer.
composer require --dev vimeo/psalm
Generate a config file:
./vendor/bin/psalm --init
Psalm will scan your project and figure out an appropriate error level for your codebase.
Then run Psalm:
./vendor/bin/psalm --no-cache
Psalm will probably find a number of issues - find out how to deal with them in Dealing with code issues.
Docker image#
It is recommended to run Psalm in the official docker image: it uses a custom build of PHP built from scratch, running Psalm +30% faster on average than normal PHP (+50% faster if comparing to PHP without opcache installed).
docker run -v $PWD:/app --rm -it ghcr.io/psalm/psalm:latest /composer/vendor/bin/psalm --no-cache
More specific tags are also available (i.e. ghcr.io/psalm/psalm:6.20.5, as well as branch tags).
Issues due to missing extensions can be fixed by enabling them in psalm.xml and/or requiring them in composer.json see here » for more info.
Extensions not stubbed by Psalm itself (and thus not available as a psalm config option) may be stubbed using traditional PHP stubs.
GitHub Actions#
The Psalm GitHub Action runs Psalm in the docker image and shows its issues on pull requests:
- name: Psalm
uses: docker://ghcr.io/psalm/psalm-github-actions # or :6.20.5, :7.0.0-rc27, ...
The Psalm Security Scan action sends the security analysis results to GitHub code scanning:
- name: Psalm Security Scan
uses: docker://ghcr.io/psalm/psalm-security-scan
- name: Upload Security Analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
Installing plugins#
While Psalm can figure out the types used by various libraries based on their source code and docblocks, it works even better with custom-tailored types provided by Psalm plugins.
Check out the list of existing plugins on Packagist.
Install them with composer require --dev <plugin/package> && vendor/bin/psalm-plugin enable <plugin/package>
Read more about plugins in Using Plugins chapter.
Using the Phar#
Sometimes your project can conflict with one or more of Psalm’s dependencies. In that case you may find the Phar (a self-contained PHP executable) useful.
The Phar can be downloaded from GitHub:
wget https://github.com/vimeo/psalm/releases/latest/download/psalm.phar
chmod +x psalm.phar
./psalm.phar --version
Alternatively, you can use Composer to install the Phar:
composer require --dev psalm/phar