explaingit

piotrplenik/clean-code-php

12,443PHPAudience · developerComplexity · 1/5Setup · easy

TLDR

Clean Code PHP adapts Robert C. Martin's Clean Code principles for PHP, showing before-and-after examples of how to write PHP that is easier to read, change, and reuse.

Mindmap

mindmap
  root((repo))
    What it does
      Clean Code for PHP
      Before and after examples
      SOLID principles
    Topics
      Naming variables
      Writing functions
      Class structure
      Comparisons
    Audience
      PHP developers
      Code reviewers
    Format
      Bad and Good examples
      PHP 7.1 and above
    Related
      Translated versions
      JS clean code guide
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Refactor your PHP code to use descriptive variable names and named constants instead of mystery numbers.

USE CASE 2

Learn how to keep PHP functions short and single-purpose, avoiding boolean flags and hidden side effects.

USE CASE 3

Apply the SOLID principles to structure PHP classes for easier future changes.

USE CASE 4

Use the Bad and Good examples as a code review checklist when reviewing a PHP pull request.

Tech stack

PHP

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

This repository adapts the principles from Robert C. Martin's book "Clean Code" for the PHP programming language. It is not a style guide about formatting or spacing. The focus is on writing PHP code that is easy to read, easy to change, and easy to reuse by yourself or others later. The guide is structured as a series of sections, each covering a different area of code design: how to name variables, how to write functions, how to structure classes, how comparisons work, and how to apply five foundational object-oriented design principles known as SOLID. Within each section, the guide presents concrete before-and-after examples labeled "Bad" and "Good" to show what the recommendation looks like in practice. The naming advice covers things like choosing variable names that are readable aloud, using consistent terminology across similar variables, and replacing mystery numbers with named constants so future readers can tell what the value means. The functions section addresses how to keep functions short, how to avoid passing boolean flags as arguments, and how to prevent a function from silently changing external state. The classes section covers inheritance, composition, and when to mark a class as final so it cannot be extended. The README targets PHP 7.1 and above, though it notes that many developers still use PHP 5. The guide acknowledges that not every principle will suit every project and that reasonable people disagree on some of them. It is presented as a codification of experience rather than a rulebook. The project was inspired by a similar clean-code guide written for JavaScript. Translations into several languages are available in the repository. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
Based on the Clean Code PHP guide, review this PHP function and rewrite it to remove boolean flags, avoid hidden state changes, and use a descriptive name.
Prompt 2
Applying Clean Code PHP naming principles, rename the variables in this PHP snippet so each reads like a plain English sentence.
Prompt 3
Using the SOLID principles from Clean Code PHP, refactor this PHP class so it has a single responsibility and does not need to change when a new feature is added.
Prompt 4
I'm doing a PHP code review. Give me a checklist of the most important Clean Code PHP rules to check for in each function and class.
Open on GitHub → Explain another repo

← piotrplenik on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.