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.3 to PHP 8.4.
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"
-- @psren
"The book is easy to read in a complex subject to me. Congrats both on your excellent work!!"
-- @KPikaza
"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
Bundles that include this book
Table of Contents
-
Preface
- 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
-
Introduction
- What is Rector?
- Who Should Read This Book?
- An Overview of the Contents
- About the Code Samples
-
About the Authors
- Matthias Noback
- Tomas Votruba
- Acknowledgements
- We Want Your Feedback
-
Programmatically Modifying PHP Code
- Introduction
- 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
- Resolving Fully-qualified Names
- Finding Nodes
- Summary
-
PHP Tools in the Game
- Introduction
- Working Together with Giants
- 2007 - Now Timeline
- The Primary Feature
-
1. Coding Standard Tools
- PHP_CodeSniffer
- PHP CS Fixer
- Easy Coding Standard
-
2. Static Analyzers
- PHP-Parser
- PHPStan
- Psalm
-
3. Instant Upgrade Tools
- Symfony-Upgrade-Fixer
- Rector
- When to Use Which Tool?
- Is Your Project Bare Without Any Tools?
- Run Rector First, Then Polish with Coding Standards
- Recommended Tools
- Summary
-
Creating Your First Rector Rule
- Introduction
- What’s a Rector Rule?
- Creating a Custom Rule
-
Extending AbstractRector
- Finding the Right Node Class
- Expr vs Stmt
- 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
- Summary
-
Test-driven Rule Development
- Introduction
- 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
- Summary
-
More Testing Techniques
- Testing Multiple Rules Combined
- Rules Should Be Idempotent
- Removing the Clone Step
- Shouldn’t You Start with a Rule Set Test?
- Low-Hanging Fruit First
- Create a Configurable Generic Rule
- Using a Configurable Rule over Custom Rule
- Making Rule Behavior Configurable
- Configurable Rule Makes Hidden Assumptions Explicit
- Using Supporting Classes in Fixtures
- Summary
-
Continuous Rectifying
- Introduction
- 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
- Removing Boring Work Opens Your Creativity
- Summary
-
Rector + CI = Next Member of Your Team
- Introduction
- What are The Steps for a Rector Run?
-
How to Add Rector to GitHub Actions
- 1. Generic PHP Setup
- 2. Rector Setup
- Allow Rector to contribute the Pull Request
- Summary
-
The Future of Instant Upgrades
- Introduction
-
Removing Legacy
- How Can We Be So Confident?
- A Member of your Team That Sends Pull-Requests Daily
- PHP Stands Out
- Epilogue
- Node Type and Refactor Examples
-
Book Revisions
- Update 2024-12-09 and Rector 2.0
- Update 2024-02-07 and Rector 1.0
- Update 2024-01-30 and Rector 0.19.3
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.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, 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.
Learn more about Leanpub's ebook formats and where to read them