Rector - The Power of Automated Refactoring
Minimum price
Suggested price

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"

-- @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

About the Authors

Matthias Noback
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.

Tomas Votruba
Tomas Votruba

Tomas loves to combine open-source and innovations... yet he's super lazy. Traumatized in childhood by doing repeated tasks as a punishment by his father (whom he loves), he's unable to do anything twice without deep physical pain. That's why he always spends dozens of hours to automate problems that would take and hour or two to do manually.

This extreme lazyness created the Rector ecosystem. A tool that automatically upgrades and refactores PHP, so you can have a coffee instead while your code is upgraded from PHP 5.3 to PHP 8.1.

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
      • Connecting Nodes
      • Resolving Fully-qualified Names
      • Finding Nodes
    • Summary
  • PHP Tools in the Game
    • Introduction
    • Working Together with Giants
    • 2007 to 2021 Timeline
    • The Primary Feature
    • 1. Coding Standard Tools
      • PHP_CodeSniffer
      • PHP CS Fixer
      • EasyCodingStandard
    • 2. Static Analyzers
      • PHP-Parser
      • PHPStan
      • Psalm
    • 3. Instant Upgrade Tools
      • Symfony-Upgrade-Fixer
      • Rector
    • 4. Instant Downgrade Tools
      • PHP-Backporter
      • Galapagos
      • Transphpilea
      • 7to5
      • 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
    • 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
    • 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
    • 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
    • Work First, Enjoy Later
    • Removing Boring Work Opens Your Creativity
    • Summary
  • Rector + GitHub Actions = 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
    • 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
    • Summary
  • The Future of Instant Upgrades
    • Introduction
    • Removing Legacy
      • 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
  • Epilogue

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms

Do Well. Do Good.

Authors have earned$11,043,141writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

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), EPUB (for phones and tablets) and MOBI (for 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, EPUB and/or MOBI files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub