LogoLogo
  • Planet 4
  • Development
    • Contribute
    • Installation
    • Git Guidelines
    • Coding Standards
    • Continuous Delivery
  • CI/CD
    • Test Instances
    • Deployment
    • Testing
      • End-to-end Tests
      • Visual Regression Tests
  • NRO Customization
    • Development
      • Using Child Themes
      • Package Registry
      • Plugins
    • Testing
      • Visual Regression Tests
    • Deployment
      • Production
      • DB/Media Sync
  • Infrastructure
    • NRO Generation
    • ElasticSearch
    • Cloudflare
  • Recipes
    • Maintenance page
    • Production sync
    • Running commands
  • Platform
    • Practices
    • ADRs
      • [ADR-0001] Use Gitbook for Technical Documentation
      • [ADR-0002] P3 Archive elastic search integration
      • [ADR-0003] WYSIWYG Blocks Architecture
      • [ADR-0004] Switch to Monorepo
      • [ADR-0006] Define scope for deployment environments
      • [ADR-0008] PSR-4 Autoloading Standard
      • [ADR-0009] Include Media Library in master theme
      • [ADR-0011] PHP Coding Standards
      • [ADR-0012] Use custom SCSS syntax for variables
      • [ADR-0013] Choose a ticketing system
      • [ADR-0014] Choose a testing framework
      • [ADR-0015] Use block templates to build block patterns
      • [ADR-0016] Form Builder data retention policy
      • [ADR-0017] Move blocks into the theme
    • Changelog
      • 2024
      • 2023
      • 2022
      • 2021
      • 2020
      • 2019
      • 2018
  • Tech
    • Wordpress
    • Blocks
    • Plugins
    • Hooks
    • Data migrations
    • CSS variables
Powered by GitBook
On this page
  • Context and Problem Statement
  • Decision Drivers
  • Considered Options
  • Decision Outcome
  • Pros and Cons of the Options
  • Links
Edit on GitHub
  1. Platform
  2. ADRs

[ADR-0011] PHP Coding Standards

Implement modern and consistent PHP coding standards

Previous[ADR-0009] Include Media Library in master themeNext[ADR-0012] Use custom SCSS syntax for variables

Last updated 5 months ago

  • Status: accepted

  • Deciders: Development Team

Technical Story: Planet 4 repositories currently follow , using PHPCS rules to enforce those. Wordpress choices are guided by a necessity for backward compatibility with , and on older systems.

We are not bound by those necessities, and could choose to follow different coding standards if we see a benefit in it.

Context and Problem Statement

This ADR is a proposition to override some of the Wordpress coding standards to allow for a more modern, concise, secure and readable code, using current PHP structures and syntaxic evolutions.

PHP-FIG edited two PSR (, ) that we could also use to update our rules.

Our coding standards could be allowed to diverge from Wordpress, because:

  • we are not bound by backward-compatibility to an older PHP version, we need to follow for security reasons. Even Wordpress running on a recent PHP version.

  • we don’t have to edit Wordpress core itself, which would require us to follow those standards for coherence

  • as stated in : “you can (and should) choose practices that best suits your development style for your own plugins and themes”

Please feel free to add any decision driver and option you would like to revise or discuss.

All the validated options should be enforced through phpcs rules, automatically fixable through phpcbf, and controlled by a static analyzer, so that it does not become a limitation to participate. If not possible, they should appear in contributing guidelines.

Decision Drivers

  • Wordpress stays compatible with PHP 5.6; since this version, many features have been introduced that would make our code more readable and resilient to bugs consequences of type mismanagement, in particular:

    • Until PHP 7.2: type declaration for function parameters and return values, constants visibility, nullable types

    • PHP 7.4: typed properties

    • PHP 8.0: union types, attributes, constructor property promotion, static/mixed return types

  • Some of the forbidden constructs make the code more cumbersome

    • Short ternary operator, spread operator, etc.

    • Both functions are equivalent, but the current guidelines forced me to write the longer one

Considered Options

    • Using short syntax `[]` is preferred

    • Putting spaces on the inner side of opening/closing parentheses is not needed

    • Putting spaces around array indexes is not needed

    • Using a string to trigger a specific behavior is prone to error (typos, etc.); if you want to use a string as flag value, declare a constant in a class and use this constant wherever necessary

    • Ternary operator should be used anywhere useful

We will have a problem with

We will have a problem with

Other rules to consider and write in guidelines:

  • Check Sonar code smells, it is very useful to see complexity and simplify code

  • Writing a function more than 80 lines long should lead to thinking about splitting it.

  • An ideal case is 1 function = 1 responsibility = an obvious name describing this responsibility.

Enforcement of types:

  • Declare types at all times possible

  • Return only one type per function (nullable if needed), when possible

  • Make files type strict `declare(strict_types=1);`

Decision Outcome

Pros and Cons of the Options

Most of the PSR-12 recommendations make sense, and could be applied right away.

Exceptions are indentation (spaces/tabs) and case for properties and variables (camelCase/snake_case).

Feel free to add talking topics and points about things you want to discuss.

Indentation - Tabs

  • Good, because it is consistent with WordPress codebase

  • Bad, if you prefer spaces

Indentation - Spaces

  • Good, because it is consistent with PSR-12 recommendations

  • Bad, if you prefer tabs

Case - camelCase

  • Good, because it is consistent with PSR-12 recommendations

  • Good, because it makes clear what comes from WordPress and what comes from our code

  • Bad, because it is inconsistent with WordPress codebase, so we can’t exactly copy/paste source code

Strict typing

  • Good, because it makes your code predictable, and readable by static analyzers, limiting bugs

  • Good, because it effectively replaces type documentation in comments

  • Bad, because it is more work to figure out all your inputs and outputs types, especially in a WordPress context

Abandoning alignment on equal signs and arrows

  • Good, because it reduces line length and large empty spacing

  • Bad, because it looks less like a data table

Abandoning yoda conditions

  • Good, because it impairs readability/understanding

  • Bad, because it can still stop some errors

Links

Some divergences from :

Borrowing from :

: advocating for method name in camelCase, our codebase is only snake_case, and using wordpress globals forces us to at least use snake_case in those situations

Borrowing from :

: advocating for spaces, our codebase is only tabs

- Sonar takes care of it

unless necessary, it’s often a sign of a piece of logic that should be extracted - Sonar takes care of it

Align assignments , a long list of assignments is often of code that should be split up

Decision matrix is available for comments and votes at

Chosen option:

Wordpress Coding Standards
older PHP versions
friendly failure
PSR-1
PSR-12
supported versions
recommends
this blog post
in this gist
Wordpress standards
Declaring arrays
Space usage
Self-Explanatory Flag Values for Function Arguments
Ternary operator
PSR-1
4.3 Methods
PSR-12
2.4 Indenting
Return early
Avoid using `else`
only if it makes sense
a sign
Planet4 PHP Coding Standards decisions
PHPCS configuration implemented
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/
https://github.com/WordPress/WordPress-Coding-Standards
https://www.php-fig.org/psr/psr-1/
https://www.php-fig.org/psr/psr-12/
https://pear.php.net/manual/en/standards.php
https://github.com/slevomat/coding-standard
https://jira.greenpeace.org/browse/PLANET-5954