Unlock the Power of Modern PHP Features

Why Transpile to PHP 7.1?

As a PHP developer, you want to take advantage of the latest language features, but sometimes, compatibility issues with older versions hold you back. The good news is that you can use a transpiler to convert your modern code into a format compatible with earlier PHP versions. This way, you can enjoy the best of both worlds: write code using the latest features and deploy it on older versions.

I recently decided to transpile my WordPress plugin to PHP 7.1, even though WordPress still supports PHP 5.6. The reason is that Rector, a powerful reconstructor tool, has limited support for downgrading code to PHP 5.6. Additionally, many third-party dependencies require PHP 7.1, making it a more practical target.

What Features Can Be Transpiled?

Rector currently supports downgrading various PHP 7.4 features, including:

  • Typed properties
  • Arrow functions
  • The null coalescing assignment operator

You can even use some PHP 8.0 features, like union types, in your code!

Transpiling a WordPress Plugin

To demonstrate the transpilation process, I’ll use my own GraphQL API for WordPress plugin. The plugin’s code uses features from PHP 7.4, 7.3, and 7.2, which need to be converted to PHP 7.1. Rector takes care of this conversion, ensuring that the plugin remains compatible with older PHP versions.

// Example code using PHP 7.4 features
class MyClass {
    private int $myProperty;

    public function myMethod(): int {
        return $this->myProperty?? 0;
    }
}

Dealing with Third-Party Dependencies

When transpiling third-party dependencies, you need to check if they require PHP 7.2 or above. If so, you’ll need to inspect their source code to determine if the PHP 7.2 code can be transpiled. In my case, most dependencies only used PHP 7.1 code, but one required PHP 7.2.5. Fortunately, the problematic code was not used in my plugin, so I didn’t need to transpile it.

Setting Up Rector

To configure Rector, you need to define the sets of rules to apply to your code. These rules downgrade the code from higher to lower PHP versions. You can even uncomment a line to enable support for PHP 8.0 features when they’re released!

# Example Rector configuration
parameters:
  php_version: 7.1
  rectify_set_list:
    - 'php70'
    - 'php71'

Loading WordPress and Running Rector

To transpile the code, you need to provide the path to WordPress’s source files. Then, run Rector with the --dry-run flag to see the transformations without modifying the original code.

$ vendor/bin/rector process src --dry-run

Testing the Transpiled Code

Once you’ve transpiled the code, you need to test it to ensure it works correctly. I use PHPStan to analyze the transpiled code and verify that it’s compatible with PHP 7.1.

Generating the Asset for Production

To generate the asset for production, I use a GitHub action that transpiles the code, modifies the Requires PHP header, and creates a.zip file containing the plugin.

# Example GitHub action workflow
name: Transpile and Package Plugin

on:
  push:
    branches:
      - main

jobs:
  transpile-and-package:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Transpile code
        run: vendor/bin/rector process src
      - name: Modify Requires PHP header
        run: sed -i '/Requires PHP: 7.4/Requires PHP: 7.1/' readme.txt
      - name: Create.zip file
        run: zip plugin.zip *.php

Leave a Reply