Rector - The Power of Automated Refactoring
Rector - The Power of Automated Refactoring
About the Book
Rector is an extremely powerful tool that can instantly improve the code quality of your PHP projects. It can be used to safely migrate projects from PHP 5.6 to PHP 8. It can help you establish a standard of robust programming practices in your project. And it can save you a lot of time spent on reviewing pull requests.
In this book, seasoned developers Matthias Noback and Tomas Votruba give you all the information you need to become a Rector power user. You'll learn:
- How to make Rector part of your daily development workflow
- How to create Rector rules that replace repetitive manual refactoring work
- How to automate even more with Rector by adding it to your project's build process
Tomas, being the founding father and core maintainer of the Rector project, will shine his light on:
- What's the place of Rector in the larger ecosystem of PHP tools for code quality assurance?
- What role will Rector fulfill in the future?
Matthias, who has a strong focus on automated testing, will cover:
- The concepts behind Rector: tokenizing, parsing, and manipulating PHP AST nodes
- Test-driven development techniques for creating automated refactorings
What readers say
"I've been following Rector for a couple of years now. I've been very excited by the claims and demos but try as I might to get an understanding and a usable knowledge I just couldn't... until the book. Now within the space of a few days I have started to integrate Rector into a 20-year-old project (with a couple million lines of code) at work. My commits have increased, I have been able to find way more problems early on in code reviews, and I have started to clean up a lot of legacy spaghetti code.
Many thanks for the book, it has really really.... REALLY helped!"
-- Steve Hyde
"Grabbed my copy. This was an absolute nobrainer.
(one day later)
I already read through the whole thing. Nice work. Next step is to create some rules"
"The book is easy to read in a complex subject to me. Congrats both on your excellent work!!"
"18 years ago, I bought my first technical book ever PHP 4 Bible. To master how to automatically refactor code using the same language on which it is written is something I could've not foreseen back then."
-- Oscar Nevarez
"I read most of the book already. Learned lots about the abstract syntax tree and how to create and test Rector rules. I can recommend any PHP developer to read this book!"
-- Tijmen Wierenga
"Purchased! My mind is already buzzing with ideas on how to use this on a legacy PHP project I just took over."
-- Joel Clermont
- A Trainer’s Journey
- The Other Side
- Who’s Fault is It?
- From Blame to Pain
- From Pain to Idea
- From Idea to First Real Reconstructor
- We’ve Just Started, Now You Get on Board
- What is Rector?
- Who Should Read This Book?
- An Overview of the Contents
- About the Code Samples
About the Authors
- Matthias Noback
- Tomas Votruba
- We Want Your Feedback
Programmatically Modifying PHP Code
- Primitive Ways of Modifying Code
- Tokenizing PHP Code
- Parsing PHP Tokens: the Abstract Syntax Tree
- Converting the AST Back to PHP Code
- Manipulating the AST
- Node Visitors
- Manipulating the AST with a Node Visitor
Built-in Node Visitors
- Connecting Nodes
- Resolving Fully-qualified Names
- Finding Nodes
PHP Tools in the Game
- Working Together with Giants
- 2007 to 2021 Timeline
- The Primary Feature
1. Coding Standard Tools
- PHP CS Fixer
2. Static Analyzers
3. Instant Upgrade Tools
4. Instant Downgrade Tools
- When to Use Which Tool?
- Is Your Project Bare Without Any Tools?
- Run Rector First, Then Polish with Coding Standards
- Recommended Tools
Creating Your First Rector Rule
- What’s a Rector Rule?
- Creating a Custom Rule
- Finding the Right Node Class
- Running a Single Rule
Refactoring the Method Call Node
- What Is the Type of a Variable?
What if We Run Rector Twice?
- Effectivity Beats Perfection
Test-driven Rule Development
- Migrating from DateTime to DateTimeImmutable
- Creating a Test Class
- Adding First Test Fixture
- Making the First Test Pass
- Narrowing the Scope of the Refactoring
- Capturing the Return Value of modify()
- Skipping Calls On Classes That Are Not DateTime
More Testing Techniques
- Testing Multiple Rules Combined
- Rules Should Be Idempotent
- Removing the Clone Step
- Comparing Return Values to Validate Correctness
- Shouldn’t You Start with a Rule Set Test?
- Low-Hanging Fruit First
- Create a Configurable Generic Rule
- Using a Configurable Instead of a Custom Rule
- Making Rule Behavior Configurable
- Configurable Rule Makes Hidden Assumptions Explicit
- Using Supporting Classes in Fixtures
- The Next Member of Your Team
- Turning a Junior Into a Senior on Day 1
- Who’s to Blame?
- What Does it Look Like to Work with Rector in CI?
- How Rector is Rectifying Itself
- Work First, Enjoy Later
- Removing Boring Work Opens Your Creativity
Rector + GitHub Actions = Next Member of Your Team
- What Are The Steps for a Rector Run?
How to Add Rector to GitHub Actions
- 1. Generic PHP Setup
- 2. Rector Setup
- What Feedback Do We Get?
How to Work with Rector’s Output?
- 1. Manual Changes
- 2. Run Rector Locally
- 3. Let Rector Code for Us in Pull Requests
- What is The Work We Want to Automate?
Make Your Rector Workflow Come Alive
- Make Rector Push Changes
The Future of Instant Upgrades
- How Can We Be So Confident?
- No Backward-Compatibility Breaks and No Backward-Compatibility
- A Bot That Sends Pull-Requests Daily
- Instant Upgrades on Click
- PHP Stands Out
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 million USDwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.