What it took for Rector to be born?
Paradigm shift, ecosystem maturity, need for speed to solve common problems community has. And a great team you share your work with that feedbacks and reflects.
Read also:
It's not that PHP projects didn't need to be updated until 2017. I surely could delegate hundreds of upgrade-hours for my whole career. So why Now?
Codemod is a tool that modifies your code. And you're ok with it.
Many years ago PHP_CodeSniffer was born by Greg Sherwood from Australia. Guess how long ago? In 2006! Tool that checks your coding standard, tabs and spaces, brackets and quotes. First of it's kind to be mainstream in PHP community.
It was followed by PHP CS Fixer with it's first release in 2014 by Fabien Potencier. Did you know the first script had only 106 lines?
I use daily both of these tools, they're both awesome and work best together. Both of them fix the code for you, so you can sleep or have a coffee instead.
It took 12 years and 2 tools with over 4000 stars on Github to get here.
A few years ago Nikita Popov started an ambitious project nikic/PHP-Parser. PHP-Parser parses PHP code to AST. If you're new to Abstract Syntax Tree (AST), check this post that describes 2 big changes in PHP ecosystem thanks to AST.
PHP_CodeSniffer was read only, which is great for letting you know what is wrong, but not much for getting a coffee instead. So fixing part was added.
Same was for PHP-Parser. It can read a code and allow it analysis. That's what Ondřej Mirtes uses in PHPStan - context aware PHP analysis ("this variable is of this type").
Again useful, but what about that coffee? It won't make itself.
I must say, this is breaking point for Rector. Without this, Rector would be just annoying tool telling you what is wrong and what you should do (and we had enough control already, right?). Super fortunately, write feature was added and released in 2018 with php-parser 4.
Did you know? That Fabien wanted to use PHP-Parser for PHP CS Fixer in 2012, but could not, because the writing part was missing? Patience makes perfect - 6 years later it's there.
PHP AST can be saved, but it still needed a bit polishing:
namespace App;
use Symfony\Component\HttpFoundation\DeprecatedRequest;
class Controller
{
- public function actionIndex(): DeprecatedRequest
+ public function actionIndex(): \Symfony\Component\HttpFoundation\NewRequest
{
}
}
There was no tool that could do this for you, until EasyCodingStandard + Symplify\CodingStandard
.
With that combination, just run vendor/bin/ecs
with proper setup to fix that:
namespace App;
+use Symfony\Component\HttpFoundation\NewRequest;
-use Symfony\Component\HttpFoundation\DeprecatedRequest;
class Controller
{
- public function actionIndex(): \Symfony\Component\HttpFoundation\NewRequest
+ public function actionIndex(): NewRequest
{
}
}
That's how Rector was born one part after another. At least the ideas.
It's not only about writing code. It's about discussing the idea, finding the right API that not only me but others would understand right away, it's about using other tools. It's about talking with people who already made AST tools, ask for advices and find possible pitfalls. It's about reporting issues and talking with people who maintain projects you depend on.
I don't work for at any company so development of Rector doesn't solve my personal issues. That's how most project is born, like PHPStan to check Slevomat's code. That means I needed other motivation - when my frustration of wasted thousands human-hours was not enough.
Here I'd like to thank Petr Vácha for cowork weekend in Brno with in summer 2017, where it all started - in those times named as Refactor. You've been great friend for years and courage in times, when I needed it the most.
And David Grudl, who gave me the motivation to dig deep and "just try it" when I felt desperate and useless always with lightness of Zen master.
And also Nikita Popov, who patiently answered, taught me and fixed all my issues on nikic/php-parser
.
Without you, I would not make it here today.
Happy 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!