Announcing Psalm support for Laravel

Matt Brown
Mar 5 · 2 min read

Today I’m releasing a Psalm plugin for Laravel!

Background

Two months ago I announced the latest version of Psalm, and discussed how its new plugin framework would make authoring integrations for third-party packages much easier. A bunch of great plugins have sprung up since, but none for Laravel.

Authoring a such a plugin from scratch requires an in-depth knowledge of that framework. Nobody came forward to volunteer, and my own experience with Laravel is essentially zero.

Luckily there’s a popular tool that’s done most of the work already: the Laravel IDE Helper. It generates static files that help PHPStorm understand Laravel’s functions and methods.

There are two types of files the IDE Helper generates:

  • regular stubs (PHP code without function/method bodies)
  • a .phpstorm.meta.php file that allows PHPStorm to interpret different return types for a set of known functions depending on those functions’ arguments.

Psalm already understood regular stubs, and adding support for the IDE Helper’s generated .phpstorm.meta.php was relatively simple. The plugin generates those files in a separate directory, and tells Psalm to use them in its analysis.

The advantage of this approach is threefold:

  • Psalm can benefit from future improvements to the Laravel IDE Helper without me having to do any extra work.
  • Anyone who uses Psalm in a project with a .phpstorm.meta.php stub can now benefit from Psalm’s understanding of them, regardless of whether they use Laravel.
  • Users of other IDEs can benefit from these stubs too, if they use Psalm’s Language Server functionality.

Installation

If you haven’t already, install Psalm. Then run

composer require --dev psalm/plugin-laravel
vendor/bin/psalm-plugin enable psalm/plugin-laravel

Head to the plugin’s Github repo for all the usual open source stuff.

Thanks to Barry vd. Heuvel for creating and maintaining the Laravel IDE Plugin, and to a number of contributors for improving Psalm’s plugin architecture so this was a reasonably simple integration.

Matt Brown

Written by

I work @vimeo. I am cool.