Writing custom PHPStan rules (Tutorial)


If you're using tools like PHPStan on your projects you'll already be reaping the benefits. However are you using them to their full potential?

Do you have conventions on your project that you have to remember, either when writing or during code review? (E.g. a certain object should only be built by a factory. Or a method should only be used for testing.) Do you wish you could automate the checking of these conventions so they'd never be forgotten?

Or do you wish PHP had features that other languages had, e.g. Java's package visibility modifier?

The great news is PHPStan allows you to write your own custom rules to enforce all kinds of conventions or new language features.

This tutorial will guide you through the basics of writing your own rules for PHPStan. We'll create a couple of simpler rules together and by the end of the session you'll be in a position to start writing your own rules.

NOTE: This tutorial assumes you already use PHPStan on a regular basis.

See more talks.

PHPCon Poland Nov 2023

[slides] [website]

PHPers Summit May 2023

[slides] [website]

Longhorn PHP Nov 2022

[slides] [website]