A reference of clear PHP code rules

A reference of clear PHP code rules

One question that keeps coming back while collecting the rules for clearPHP is the domain of the rule. Coding convention and conception rules covered different domains and seldom overlay each other. ClearPHP rules aim a being close to the language and its tricks, away from semantics and architecture. How come ?

Coding conventions

Coding conventions are the rules of writing. They encompass the number of spaces around operators, the level of indentation and the meaning of names. They are usually supported by PHP code sniffer which reports coding violations or PHP CS Fixer, which tries hard to fix them. This will avoid crossbreeding camels and snakes. Projects of all stripes and colours should have one and follow them.


Conception, on the other hand, tries to wrestle with the problem from a higher point of view. This is the place for design patterns, dependency injections or Unit testing. You’ll find them collected at PHP the right way. Automated feedback comes in metrics, with phpmetrics or phpmd, which gives global and local values of quality and urges you to reduce cyclomatic complexity or Halstead metrics.

Clear PHP code

In between, there are a range of recommendations that are none of the above. For example, where would the famous strpos() comparison rule be? Strpos must be compared with === to avoid missing the case where the found value is at the index 0, mistaken with the unfound result false. And foreach() loops that needs to unset their reference to avoid reuse (and misuse) ? Both can’t be can’t be coding standard, nor conception.

The solution is to avoid them, of course, to build experience. This basically means that once after having experienced them, some immunity will be build. Sadly, some tricks are of the die-hard kind. So, the better solution is to include code reviews to hunt for those smaller rules and eradicates.

Since those tricks are numerous, clearPHP has set the goal to collect them in one reference. Currently, there are 88 rules, with documentation, title and id. That makes half the job to build your own reference : simply select the one (or several) you need, and base your next code review with it. May be you’ll have experienced some even rarer situation of PHP, and will care to share it with the rest of the community. See you there !