How to run Rector and PHPStan on Huge Project in 3 Seconds with PHPStorm
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:
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:
- copy directory path or type it
- open command line
- type whole command we usually use
- remove argument we typically use
- add our directory argument
- click to run it
Instant flow killer :(
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?
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.
You run the tool only on a selected directory in the left tree.
- Without the command line.
- Without changing paths.
- Without copy-pasting.
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.
In PHPStorm open:
- Settings →
- Tools →
- External Tools
Click on +:
Name the tool and configure parameters
- Working directory:
Pick the directory you want to process in the file tree.
Type "Rector" or "PHPStan" and hit enter:
Kaboom :) That's it!
Happy flow coding!