Pro PHP Refactoring

Book description

Many businesses and organizations depend on older high-value PHP software that risks abandonment because it is impossible to maintain. The reasons for this may be that the software is not well designed; there is only one developer (the one who created the system) who can develop it because he didn't use common design patterns and documentation; or the code is procedural, not object-oriented. With this book, you'll learn to identify problem code and refactor it to create more effective applications using test-driven design.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Author
  7. About the Technical Reviewer
  8. Acknowledgments
  9. Introduction
  10. CHAPTER 1: Introduction
    1. Lesson Learned
      1. Hidden Gems
      2. You Don't Know What You've Got 'til It's Gone
    2. Call of Duty
  11. CHAPTER 2: Finding “Bad Smells” in Code
    1. Why Code Can Smell
      1. Duplicated Code
      2. Long Method
      3. Large Class
      4. Long Parameter List
      5. Divergent Change
      6. Shotgun Surgery
      7. Feature Envy
      8. Data Clamps
      9. Primitive Obsession
      10. Switch Statements
      11. Lazy Class
      12. Speculative Generality
      13. Temporary Field
      14. Data Class
      15. Comments
      16. Procedural Code
  12. CHAPTER 3: Introduction to Refactoring
    1. The Concept: What Refactoring Is
    2. The Reason: What's the Goal of Refactoring?
      1. Architecture and Structure, They Fade Away
      2. Reworking Chaos into Well-Designed Code
    3. An Example, at Last
      1. Look Ma'! No Comments!
    4. Once Is Better than Twice
    5. Goliath Died in the End
  13. CHAPTER 4: Principles and Rules
    1. Why Should You Do Refactoring?
      1. Refactoring Improves the Design of Our Software
      2. Refactoring Makes Software Easier to Understand
      3. Refactoring Helps You Find Bugs
      4. Refactoring Increases Our Productivity
    2. When Should We Do Refactoring?
      1. The Rule of Three
      2. Refactoring When You Add Functionality
      3. Refactoring When You Need to Fix a Bug
    3. When You Shouldn't Do Refactoring
    4. Some Simple Rules
      1. Test Before Refactoring
      2. Small and Simple Changes
      3. Never Change Functionality
      4. Follow the Bad Smells
      5. Follow Refactoring Techniques Step-by-Step
    5. Summary
  14. CHAPTER 5: Test-First Development
    1. Building Value One-Way
    2. Chaos in a Cage
      1. Unit Tests
      2. Functional Tests
    3. You Don't Know What You've Got 'til It's Gone
      1. Trust Me: Communication
      2. Listen to What You Say, and Write It Down
      3. Pleasant Constraints
      4. Create Trust
      5. Test-Driven Development
    4. Summary
  15. CHAPTER 6: Refactoring Tools
    1. PHP IDE
      1. Refactoring Activities
      2. Cross-Platform Open-Source IDE
    2. Unit Tests with PHPUnit
      1. What Is It?
      2. Installation
      3. How to Write Unit Tests
      4. How to Run Tests
      5. How to Organize Our Tests
      6. Test Doubles
      7. PHPUnit Conclusion
    3. Functional Test with Selenium
      1. What Is It?
      2. Installation
      3. How to Record and Run Functional Tests
      4. How to Organize Selenium Tests
      5. Automated Test Execution with Selenium RC
      6. Selenium Conclusion
    4. The Best of Two Worlds
      1. Selenium RC and PHPUnit
      2. Selenium Functional Test with PHPUnit
    5. Summary
  16. CHAPTER 7: Structuring Behavior
    1. Extract Method
      1. Motivation
      2. Mechanics
      3. Example: No Local Variables
      4. Example: Using Local Variables
      5. Example: Reassigning a Local Variable
    2. Inline Method
      1. Motivation
      2. Mechanics
      3. Example
    3. Inline Temp
      1. Motivation
      2. Mechanics
      3. Example
    4. Replace Temp with Query
      1. Motivation
      2. Mechanics
      3. Example
    5. Introduce Explaining Variable
      1. Motivation
      2. Mechanics
      3. Example
    6. Split Temporary Variable
      1. Motivation
      2. Mechanics
      3. Example
    7. Replace Method with Method Object
      1. Motivation
      2. Mechanics
      3. Example
    8. Substitute Algorithm
      1. Motivation
      2. Mechanics
      3. Example
    9. Summary
  17. CHAPTER 8: Changing Class Responsibilities
    1. Move Method
      1. Motivation
      2. Mechanics
      3. Example
    2. Move Property (or Field)
      1. Motivation
      2. Mechanics
      3. Example
    3. Extract Class
      1. Motivation
      2. Mechanics
      3. Example
    4. Inline Class
      1. Motivation
      2. Mechanics
      3. Example
    5. Hide Delegate
      1. Motivation
      2. Mechanics
      3. Example
    6. Remove the Middle Man
      1. Motivation
      2. Mechanism
      3. Example
    7. Introduce Foreign Method
      1. Motivation
      2. Mechanism
      3. Example
    8. Summary
  18. CHAPTER 9: Dealing with Data Rationalization
    1. Self-Encapsulate Field
      1. Motivation
      2. Mechanics
      3. Example
    2. Replace Data Value with Object
      1. Motivation
      2. Mechanics
      3. Example
    3. Change Value to Reference
      1. Motivation
      2. Mechanics
      3. Example
    4. Change Reference to Value
      1. Motivation
      2. Mechanics
    5. Replace Array with Object
      1. Motivation
      2. Mechanics
      3. Example
    6. Change Unidirectional Association to Bidirectional
      1. Motivation
      2. Mechanics
      3. Example
    7. Change Bidirectional Association to Unidirectional
      1. Motivation
      2. Mechanics
      3. Example
    8. Replace Magic Number with Symbolic Constant
      1. Motivation
      2. Mechanics
      3. Example
    9. Encapsulate Field
      1. Motivation
      2. Mechanics
      3. Example
    10. Replacing Type Code with Subclasses
      1. Motivation
      2. Mechanics
      3. Example
    11. Replace Type Code with State/Strategy
      1. Motivation
      2. Mechanics
      3. Example
    12. Replace Subclass with Fields
      1. Motivation
      2. Mechanics
      3. Example
    13. Summary
  19. CHAPTER 10: Reducing to Essential Conditional Executions
    1. Decompose Conditional
      1. Motivation
      2. Mechanics
      3. Example
    2. Consolidate Conditional Expression
      1. Motivation
      2. Mechanism
      3. Example to Consolidate with ORs
      4. Example to Consolidate with AND
    3. Consolidate Duplicate Conditional Fragments
      1. Motivation
      2. Mechanism
      3. Example
    4. Remove Control Flag
      1. Motivation
      2. Mechanism
      3. Example with Control Flag Replaced with Break
      4. Example Replacing Control Flag with a Return Exit Point
    5. Replace Nested Conditional with Guard Clauses
      1. Motivation
      2. Mechanism
      3. Example
    6. Replace Conditional with Polymorphism
      1. Motivation
      2. Mechanism
      3. Example
    7. Summary
  20. CHAPTER 11: Simplifying Method Calls
    1. Rename Method
      1. Motivation
      2. Mechanics
      3. Example
    2. Add Parameter
      1. Motivation
      2. Mechanics
      3. Example
      4. Remove Parameter
      5. Motivation
      6. Mechanics
    3. Separate Query from Modifier
      1. Motivation
      2. Mechanics
      3. Example
    4. Parameterize Method
      1. Motivation
      2. Mechanics
      3. Example
    5. Replace Parameter with Explicit Method
      1. Motivation
      2. Mechanics
      3. Example
    6. Preserve Whole Object
      1. Motivation
      2. Mechanics
      3. Example
    7. Replace Parameter with Method
      1. Motivation
      2. Mechanics
      3. Example
    8. Introduce Parameter Object
      1. Motivation
      2. Mechanics
    9. Remove Setting Method
      1. Motivation
      2. Mechanics
      3. Example
    10. Hide Method
      1. Motivation
      2. Mechanics
    11. Replace Constructor with Factory Method
      1. Motivation
      2. Mechanics
      3. Example
    12. Replace Error Code with Exception
      1. Motivation
      2. Mechanics
      3. Example
    13. Replace Exception with Test
      1. Motivation
      2. Mechanics
    14. Summary
  21. CHAPTER 12: Simplifying Generalization Relationships
    1. Pull Up Field
      1. Motivation
      2. Mechanism
      3. Example
    2. Pull Up Method
      1. Motivation
      2. Mechanism
      3. Example
    3. Pull Up Constructor Body
      1. Motivation
      2. Mechanism
      3. Example
    4. Push Down Method
      1. Motivation
      2. Mechanism
      3. Example
    5. Push Down Field
      1. Motivation
      2. Mechanism
      3. Example
    6. Extract Subclass
      1. Motivation
      2. Mechanism
      3. Example
    7. Extract Super Class
      1. Motivation
      2. Mechanism
      3. Example
    8. Collapse Hierarchy
      1. Motivation
      2. Mechanism
      3. Example
    9. Form Template Method
      1. Motivation
      2. Mechanism
      3. Example
    10. Replace Inheritance with Delegation
      1. Motivation
      2. Mechanism
      3. Example
    11. Replace Delegation with Inheritance
      1. Motivation
      2. Mechanism
      3. Example
    12. Summary
  22. CHAPTER 13: Legacy Code
    1. Ugly Code
    2. Maintenance
      1. Example: SQL injection
      2. Example: Database Portability
    3. New Features
      1. Dynamic Layouts
      2. Internationalization
    4. Break the Cycle
    5. Summary
  23. CHAPTER 14: Regression Tests
    1. Ugly But Valuable
    2. Keeping Value vs. Wasting Value
    3. Putting the Chaos in a Cage
      1. Motivation
      2. Mechanics
      3. Examples
    4. Summary
  24. CHAPTER 15: Refactoring with Patterns
    1. Design Patterns
      1. What Are Design Patterns?
      2. Why Do I Need to Use Design Patterns?
      3. When Do I Need to Use Them?
      4. Refactoring with Patterns
    2. Transform Procedural Code into Object-Oriented Code
      1. Motivation
      2. Mechanism
      3. Example
    3. Replace SQL with ORM
      1. Motivation
      2. Mechanism
      3. Example
    4. Separate Business Logic from View
      1. Motivation
      2. Mechanism
      3. Example
    5. MVC Architecture
      1. Motivation
      2. Mechanism
      3. Example
    6. Summary
  25. Index

Product information

  • Title: Pro PHP Refactoring
  • Author(s): Francesco Trucchia, Jacopo Romei
  • Release date: July 2010
  • Publisher(s): Apress
  • ISBN: 9781430227274