Clean Code in PHP

Book description

Practical lessons, examples, and practices from PHP experts on how to take your PHP skills to a professional level

Key Features

  • Easily navigate to key clean code principles specific to PHP development with this hands-on guide
  • Learn the how and why of writing clean code through practical examples
  • Skip the superfluous knowledge and grasp everything that's relevant to the real-world development environment

Book Description

PHP is a beginner-friendly language, but also one that is rife with complaints of bad code,;yet no clean code books are specific to PHP. Enter Clean Code in PHP. This book is a one-stop guide to learning the theory and best practices of clean code specific to real-world PHP app development environments.

This PHP book is cleanly split to help you navigate through coding practices and theories to understand and adopt the nuances of the clean code paradigm. In addition to covering best practices, tooling for code quality, and PHP design patterns, this book also presents tips and techniques for working on large-scale PHP apps with a team and writing effective documentation for your PHP projects.

By the end of this book, you'll be able to write human-friendly PHP code, which will fuel your PHP career growth and set you apart from the competition.

What you will learn

  • Build a solid foundation in clean coding to craft human-readable code
  • Understand metrics to determine the quality of your code
  • Get to grips with the basics of automated tests
  • Implement continuous integration for your PHP applications
  • Get an overview of software design patterns to help you write reusable code
  • Gain an understanding of coding guidelines and practices for working in teams

Who this book is for

This book is for early-career PHP developers who wish to avoid writing messy code by learning how to write understandable and maintainable code that sets them apart from rest. The book assumes familiarity with PHP coding and principles, but no knowledge of advanced principles will be necessary.

Publisher resources

Download Example Code

Table of contents

  1. Clean Code in PHP
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1 – Introducing Clean Code
  7. Chapter 1: What Is Clean Code and Why Should You Care?
    1. What this book will cover
    2. Understanding what clean code is
    3. The importance of clean code in teams
    4. The importance of clean code in personal projects
    5. Summary
  8. Chapter 2: Who Gets to Decide What “Good Practices” Are?
    1. Who decides these things anyway?
    2. Best practices – where do they really come from?
      1. Design pattern principles
    3. Being context-aware
    4. Being consistent – get results quicker
      1. About source code analysis tools
      2. About testing and its multiple forms
    5. Summary
  9. Chapter 3: Code, Don’t Do Stunts
    1. Understanding code
      1. A bit of history
      2. The purpose of code
    2. Be understood, not clever
    3. A note on maintainability
      1. Using binary operators and octal, hexadecimal, and binary notations
      2. Assigning a variable and using gotos
      3. Excessively using comments
      4. Using ternary comparisons
      5. Using abbreviations
      6. Bringing micro-optimizations to your code
      7. Recoding the methods of the SPL
    4. Summary
  10. Chapter 4: It is about More Than Just Code
    1. PHP as an ecosystem
    2. Choosing the right libraries
    3. A word about semantic versioning
      1. What is semantic versioning?
      2. How to deal with semantic versioning
    4. Stability versus trends
    5. Summary
  11. Chapter 5: Optimizing Your Time and Separating Responsibilities
    1. Naming and organizational conventions
      1. Class files and interface files
      2. Executables
      3. Web assets and resources
      4. Naming classes, interfaces, and methods
      5. Naming folders
    2. Separating responsibilities
      1. Event dispatching
    3. Demystifying polymorphism – interfaces and abstract classes
      1. Interfaces
      2. Abstract classes
    4. Summary
  12. Chapter 6: PHP is Evolving – Deprecations and Revolutions
    1. Old versus new PHP
      1. Strict typing
      2. Error reporting
      3. Attributes
    2. The version 8 revolution
      1. Match syntax
      2. Named arguments
      3. Read-only classes and properties
      4. Migrating resources to proper classes
      5. Protecting your sensitive arguments from leaking
    3. Summary
  13. Part 2 – Maintaining Code Quality
  14. Chapter 7: Code Quality Tools
    1. Technical requirements
    2. Syntax checking and code styling
      1. The PHP built-in linter
      2. PHP CS Fixer: a code sniffer
    3. Static Code Analysis
      1. phpcpd – the copy and paste detector
      2. PHPMD: the PHP mess detector
      3. PHPStan – a static analyzer for PHP
      4. Psalm: A PHP static analysis linting machine
    4. IDE extensions
      1. PHP Inspections (EA Extended)
      2. Intelephense
    5. Summary
    6. Further reading
  15. Chapter 8: Code Quality Metrics
    1. Technical requirements
    2. Introducing code quality metrics
      1. Aspects of software quality
      2. Code quality metrics
    3. Gathering metrics in PHP
      1. phploc
      2. PHP Depend
      3. PhpMetrics
    4. The pros and cons of using metrics
      1. The pros
      2. The cons
    5. Summary
    6. Further reading
  16. Chapter 9: Organizing PHP Quality Tools
    1. Technical requirements
    2. Installing code quality tools using Composer
      1. Installing code quality tools using require-dev
      2. Global installation
      3. Composer scripts
    3. Installing code quality tools as phar files
      1. Keeping your phar files organized
    4. Managing phar files using Phive
      1. Adding Phive to your project
    5. Summary
  17. Chapter 10: Automated Testing
    1. Technical requirements
    2. Why you need automated tests
      1. Easier refactoring with tests
    3. Types of automated tests
      1. Unit tests
      2. Integration tests
      3. E2E tests
      4. The testing pyramid in practice
    4. About code coverage
      1. Understanding code coverage
      2. How to generate code coverage reports
      3. Using the @covers annotation
      4. What to test
    5. Summary
    6. Further reading
  18. Chapter 11: Continuous Integration
    1. Technical requirements
    2. Why you need CI
      1. The costs of a bug
      2. How to prevent bugs
      3. Introducing CI
    3. The build pipeline
      1. Stage 1: Build project
      2. Stage 2 – Code analysis
      3. Stage 3 – Tests
      4. Stage 4 – Deploy
      5. Integrating the pipeline into your workflow
    4. Building a pipeline with GitHub Actions
      1. GitHub Actions in a nutshell
      2. Stage 1 – Build project
      3. Stage 2 – Code analysis
      4. Stage 3 – Tests
      5. Stage 4 – Deploy
      6. Integrating the pipeline into your workflow
    5. Your local pipeline – Git hooks
      1. Setting up Git hooks
      2. Git hooks in practice
      3. Advanced usage
    6. Excursion – Adding CI to existing software
      1. Step by step
    7. An outlook on CD
    8. Summary
    9. Further reading
  19. Chapter 12: Working in a Team
    1. Technical requirements
    2. Coding standards
      1. Going with existing standards
    3. Coding guidelines
      1. Examples of coding guidelines
      2. Setting up guidelines
    4. Code reviews
      1. Why you should do code reviews
      2. What code reviews should cover
      3. Best practices for code reviews
      4. Ensuring code reviews are done
      5. Definition of done
      6. Code reviews conclusion
    5. Design patterns
      1. Understanding design patterns
      2. Common design patterns in PHP
      3. Anti-patterns
    6. Summary
    7. Further reading
  20. Chapter 13: Creating Effective Documentation
    1. Technical requirements
    2. Why documentation matters
      1. Why documentation is important
      2. Developer documentation
    3. Creating documentation
      1. Text documents
      2. Diagrams
      3. Documentation generators
    4. Inline documentation
      1. Annotations are no code
      2. Unreadable code
      3. Outdated comments
      4. Useless comments
      5. Wrong or useless DocBlocks
      6. TODO comments
      7. When commenting is useful
      8. Tests as documentation
    5. Summary
    6. Further reading
  21. Index
    1. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Clean Code in PHP
  • Author(s): Carsten Windler, Alexandre Daubois
  • Release date: October 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781804613870