Leanpub Header

Skip to main content

Rector - The Power of Automated Refactoring

Learn how to automatically and continuously upgrade and improve your PHP code base

The authors are letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
166
Pages
About

About

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

Share this book

Categories

Price

Pick Your Price...

Minimum price

$29.00

$29.00

You pay

$29.00

Authors earn

$23.20
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Authors

Matthias Noback

Matthias Noback has been building web applications since 2003. He is the author of Principles of Package Design and Object Design Style Guide and Advanced Web Application Architecture. He is a regular blogger, public speaker and trainer.

Leanpub Podcast

Episode 224

An Interview with Matthias Noback

Tomas Votruba

Tomas Votruba is a software architect and open-source advocate who makes complex PHP codebases easier to work with. He’s the creator of Rector, a tool that helps thousands of developers upgrade their legacy code automatically.

Tomas writes with the same clarity and precision he brings to code: sharp, no fluff, always actionable. When he’s not coding, you’ll find him pruning trees, exploring train timetables, or benchmarking CPUs like Threadripper for real-world dev workflows.

He believes the best solutions are boring, fast, and easy to maintain.

His writing speaks to devs who value craft, clarity, and getting things done.

Contents

Table of Contents

Preface

  1. A Trainer’s Journey
  2. The Other Side
  3. Who’s Fault is It?
  4. From Blame to Pain
  5. From Pain to Idea
  6. From Idea to First Real Reconstructor
  7. We’ve Just Started, Now You Get on Board

Introduction

  1. What is Rector?
  2. Who Should Read This Book?
  3. An Overview of the Contents
  4. About the Code Samples
  5. About the Authors
  6. Matthias Noback
  7. Tomas Votruba
  8. Acknowledgements
  9. Tomas
  10. Matthias

Programmatically Modifying PHP Code

  1. This Chapter Covers:
  2. Introduction
  3. Primitive Ways of Modifying Code
  4. Tokenizing PHP Code
  5. Parsing PHP Tokens: the Abstract Syntax Tree
  6. Converting the AST Back to PHP Code
  7. Manipulating the AST
  8. Node Visitors
  9. Manipulating the AST with a Node Visitor
  10. Built-in Node Visitors
  11. Resolving Fully-qualified Names
  12. Finding Nodes
  13. Summary

PHP Tools in the Game

  1. This Chapter Covers:
  2. Introduction
  3. Working Together with Giants
  4. 2007 - Now Timeline
  5. The Primary Feature
  6. 1. Coding Standard Tools
  7. PHP_CodeSniffer
  8. PHP CS Fixer
  9. Easy Coding Standard
  10. 2. Static Analyzers
  11. PHP-Parser
  12. PHPStan
  13. Psalm
  14. 3. Instant Upgrade Tools
  15. Symfony-Upgrade-Fixer
  16. Rector
  17. When to Use Which Tool?
  18. Is Your Project Bare Without Any Tools?
  19. Run Rector First, Then Polish with Coding Standards
  20. Recommended Tools
  21. Summary

Creating Your First Rector Rule

  1. This Chapter Covers:
  2. Introduction
  3. What’s a Rector Rule?
  4. Creating a Custom Rule
  5. Requirements
  6. Extending AbstractRector
  7. Where to Store Your Rules?
  8. Finding the Right Node Class
  9. Install PHP-Parser
  10. Expr vs Stmt
  11. Running a Single Rule
  12. Refactoring the Method Call Node
  13. How Do We Know That?
  14. What Is the Type of a Variable?
  15. Commit Your Code Before Running Rector
  16. What if We Run Rector Twice?
  17. Effectivity Beats Perfection
  18. Summary

Test-driven Rule Development

  1. This Chapter Covers:
  2. Introduction
  3. Migrating from DateTime to DateTimeImmutable
  4. Creating a Test Class
  5. Install PHPUnit
  6. Adding First Test Fixture
  7. Making the First Test Pass
  8. Use FullyQualified to avoid miss-imports
  9. Narrowing the Scope of the Refactoring
  10. Capturing the Return Value of modify()
  11. Skipping Calls on Classes That Are Not DateTime
  12. Summary

More Testing Techniques

  1. This Chapter Covers:
  2. Rules Should Be Idempotent
  3. Removing the Clone Step
  4. Low-Hanging Fruit First
  5. Create a Configurable Generic Rule
  6. Making Rule Behavior Configurable
  7. Configurable Rule Makes Hidden Assumptions Explicit
  8. Using Supporting Classes in Fixtures
  9. Summary

Continuous Rectifying

  1. This Chapter Covers:
  2. Introduction
  3. The Next Member of Your Team
  4. Turning a Junior Into a Senior on Day 1
  5. Who’s to Blame?
  6. What Does it Look Like to Work with Rector in CI?
  7. How Rector is Rectifying Itself
  8. Removing Boring Work Opens Your Creativity
  9. Summary

Rector + CI = Next Member of Your Team

  1. This Chapter Covers:
  2. Introduction
  3. What are The Steps for a Rector Run?
  4. How to Add Rector to GitHub Actions
  5. 1. Generic PHP Setup
  6. 2. Rector Setup
  7. Allow Rector to contribute the Pull Request
  8. Summary

The Future of Instant Upgrades

  1. This Chapter Covers:
  2. Introduction
  3. Removing Legacy
  4. How Can We Be So Confident?
  5. A Member of your Team That Sends Pull-Requests Daily
  6. PHP Stands Out

Epilogue

Node Type and Refactor Examples

Book Revisions

  1. Update 2025-03-07
  2. Update 2024-12-09 and Rector 2.0
  3. Update 2024-02-07 and Rector 1.0
  4. Update 2024-01-30 and Rector 0.19.3

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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 earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub