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