Leanpub Header

Skip to main content

Recipes for Decoupling

Write software that survives

The author is letting you choose the price you pay for this book!

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

About

About the Book

Looking for a print edition? It's available on Lulu.

"Future me will appreciate it if I apply even just half of the recipes in this fantastic book. I learnt something new from every chapter and I can't wait to share some of these methods with my teams." Andrew Barlow

"Another excellent book from Matthias Noback! I learned a lot about techniques how to decouple your application from frameworks, test suites and other libraries. And the best part: it showed me how to unleash the power of PHPStan with custom rules to make sure you stay decoupled." Stefan Blanke

Software is never done. The world around your program changes faster than you want it to. Frameworks and libraries are abandoned and replaced with something better (or just something new), so you need to migrate. You can postpone this work for a bit, but eventually you'll have to catch up, or your project may end up hopelessly outdated. I'm sure you know one or two of those projects!

How can you make all of this easier for yourself and the future maintainers of the project? The keyword is "decoupling". You can change the design of your code to defend it against changes in any dependency your project relies on. Decoupling your code is a way to make it future-proof (without doing too much work that "you ain't gonna need").

About 10 years ago I started looking for ways to decouple my code, but at first I struggled to do it effectively. My code was decoupled in the wrong places, or in the wrong way. I got a better view on this topic after several intense experiences with some legacy projects, a big framework migration, and a complete project rewrite (that I'm sure could have been prevented). I've collected many recipes for decoupling along the way. This book gives you a practical overview of common situations that suffer from an often unintended high level of coupling in web applications. Of course, it also gives you step-by-step recipes to improve these situations. The examples in this book show you how to decouple from your web framework, templating engine, test framework, ORM, and so on.

Decoupling is one thing, but staying decoupled is something else entirely. That's why in this book we focus on how to solidify the decoupling rules with PHPStan, the automated static analysis tool for PHP. That way we don't have to rely on discipline and code reviews, but can let a tool point out possible coupling mistakes.

"Have you ever wondered how to efficiently decouple from your PHP framework? And how to enforce it through tooling? I did! And Recipes For Decoupling delivered me very well thought out explanations, examples and snippets for decoupling. It even explains step by step how to enforce these decoupling rules through PHPStan rules. The book contains examples from a variety of popular PHP frameworks including Symfony, Laravel, Mockery and PHPUnit." - Vincent Hagen

"'Decoupling' is a delightful read that merits a slow read because you will want to stop and code after every chapter! Noback’s clarity and honesty is welcome in an era of big personalities, as is his ability to distill framework concepts to elegant code." - Matthew Gatner

Price

Pick Your Price...

Minimum price

$39.00

$49.00

You pay

$49.00

Author earns

$39.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 3)

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

Author

About the Author

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

Contents

Table of Contents

Introduction

  1. Coupling, Why is it Bad?
  2. Decoupling, How to Do it Efficiently?
  3. Objection
  4. What’s Special About This Book?
  5. How to Stay Decoupled?
  6. Who Should Read This Book?
  7. Overview of the Contents
  8. About the Author
  9. Changelog
  10. 10 May 2022
  11. 17 May 2022
  12. 26 May 2022
  13. 31 May 2022
  14. 7 June 2022
  15. 15 June 2022
  16. 29 June 2022

1Creating Custom Rules for PHPStan

  1. This Chapter Covers:
  2. Introduction
  3. Analyzing Code with PHPStan
  4. Catching Specific Node Types
  5. Adding Automated Tests for a PHPStan Rule
  6. Deriving Types from the Current Scope
  7. Trinary Logic
  8. Putting a Node in Context
  9. Generalizing a Rule
  10. Conclusion

2Web Frameworks

  1. This Chapter Covers:
  2. Introduction
  3. Controllers
  4. Show How the Response is Created
  5. Only Use Constructor Injection for Dependencies
  6. Make Every Step Explicit
  7. Controllers Have No Parent Class
  8. Every Action Has its Own Controller Class
  9. Should We Use an HTTP Abstraction Library?
  10. Rules for Decoupled Controllers
  11. Forbidden Parent Classes
  12. Allowing Only Parameters of a Certain Type
  13. Enforcing Return Types
  14. One Action Per Controller
  15. Views
  16. Pass All the Data That the Template Needs
  17. Don’t Pass Objects That Don’t Belong in a Template
  18. Rules for Decoupled Views
  19. Don’t Use Certain Global Variables in a Template
  20. Don’t Use Certain Functions in a Template
  21. Don’t Pass Entities to a Template
  22. Conclusion

3CLI Frameworks

  1. This Chapter Covers:
  2. Introduction
  3. Input
  4. Collect Input First
  5. Jump to a Service
  6. Output
  7. Using an Observer for Showing Output
  8. Generalizing the Solution with Event Dispatching
  9. Turn the Command Class Itself Into an Event Subscriber
  10. Controllers Should Call Framework-agnostic Services
  11. Rules for Decoupling
  12. Use InputInterface and OutputInterface Only in Command Classes

4Form Validation

  1. This Chapter Covers:
  2. Introduction
  3. Form Validation
  4. Protecting Data Inside the Model
  5. Delegating Protection to Value Objects
  6. Removing Duplicate Validation Logic
  7. Defining an Explicit Shape for the Input Data
  8. Rules for Decoupling
  9. Don’t Pass a Single Array of Data to create()
  10. Enforcing the Use of Closure-based Form Validation
  11. Conclusion

5ORMs and the Database

  1. This Chapter Covers:
  2. Introduction
  3. Repository: an Abstraction for Persistence
  4. Trying an Alternative Implementation
  5. Application-generated IDs
  6. PHPStan Rule: Disallow Auto-incrementing Model IDs
  7. Defining Our Own Object API
  8. Custom Mapping Code
  9. PHPStan Rule: Only Allow Calls to fromDatabaseRecord() from Repository Classes
  10. No Magic Persistence of Related Objects
  11. Using Aggregate Design Rules
  12. Limiting Changes to One Entity
  13. Introducing View Models
  14. Provide Read Models When the Framework Needs Your Data
  15. Use Domain Events Instead of Persistence Events
  16. PHPStan Rule: Forbidden Parameter Types
  17. Conclusion

6Test Frameworks

  1. This Chapter Covers:
  2. Introduction
  3. Use Only the Most Basic Features of a Test Framework
  4. Declare Test Dependencies Explicitly
  5. PHPStan Rule: Don’t Allow PHPUnit Extensions
  6. PHPStan Rule: Don’t Allow Class-level Set-up and Tear-down Functions
  7. Assertions
  8. Write Tests in Plain Code
  9. Handwritten Test Doubles
  10. PHPStan Rule: Don’t Generate Mocks
  11. Conclusion

7Conclusion

8The End of the Book

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