gitmyhub

clean-code-php

PHP ★ 12k updated 2y ago

:bathtub: Clean Code concepts adapted for PHP

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.

PHPsetup: easycomplexity 1/5

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.

Where it fits