How to run Rector and PHPStan on Huge Project in 3 Seconds with PHPStorm

Today we'll look at the command line and run performance heavy dev tools like Rector or PHPStan on our projects. I'm guessing that it takes more than 10 seconds to run whatever project you use.

How would you like it be less than 3 seconds with just one click in PHPStorm?

Let's say we have a reasonably big codebase, like Rector, with over 70 000 lines of code. Projects I consult lately usually fit into 200-600 000 lines of code, though. But let's take Rector as the baseline, as that's the project we have data for.

We add a new feature, change test, fix a bug, 300 added lines, 200 removed.

Now, we want to run Rector and PHPStan to make sure the code is clean and working. So we run PHPStan:

And wait...




...40 seconds!


How do we Hack It?

It takes so long. We might adapt and push it into the remote repository and let the CI server handle it. After all, it's often much faster than our local laptop. I used this approach at big codebases so far.

Another way is to copy the directory path and run tool only on it:

vendor/bin/phpstan analyse src/SomeDirectory
vendor/bin/rector process src/SomeDirectory

But we have to:

Instant flow killer :(

Too Slow? Go Home!

When I talk with developers, this is one of the main reasons they don't use such tools. It takes too long to get feedback from them. It destroys their focus on the features.

Frustration is always the sign we should ask this question:
How can we do better?

Narrow Scope → Increase Focus

Last week Honza showed me PHPStorm tip that fits into instant feedback loop flow I discovered recently.

It might trim off some more frustration in your daily work and turn you into a flow developer as a result. The bigger your codebase, the more it might help you get under 3 seconds.


How does it Work?

You run the tool only on a selected directory in the left tree.

Domain Directory Separation

This approach requires us to keep a decoupled structure, e.g., with to local packages or domain-driven design.

What does it mean? That our code that handles security is not all over the place in /src directory but in standalone packages/Security directory. So if we work with security, we only mostly work in that directory.

How to Configure External Tool in PHPStorm?

In PHPStorm open:


Click on +:


Name the tool and configure parameters


Save.

How to Use It?

Pick the directory you want to process in the file tree.

Run action.

Type "Rector" or "PHPStan" and hit enter:


Kaboom :) That's it!


Happy flow coding!




Do you learn from my contents or use open-source packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!