Automated PHP Code Upgrades with Rector for WordPress Plugins and Themes

Upgrade old WordPress plugin code automatically with Rector

A plugin written in 2017 for PHP 5.6 still runs, but it is full of array(), missing type declarations, isset( $x ) ? $x : null and string functions that receive null and produce deprecation notices in PHP 8.1. Rector rewrites such code automatically: it parses the PHP into a syntax tree, applies rules and writes the changed code back. PHPStan finds problems, Rector fixes the mechanical ones.

Problem: A plugin should require PHP 8.1. The code base has 120 files in PHP 5.6 style, and the PHP 8.1 logs show deprecation notices like “Passing null to parameter #1 ($string) of type string is deprecated”.

Solution: Install Rector as a dev dependency, configure it with RectorConfig::configure() for the plugin’s source folder, the PHP sets up to the target version and a few type declaration rules, load WordPress stubs instead of WordPress itself, run it with --dry-run first, review the diff in small steps, and run the tests after each step.

Install

composer require --dev rector/rector php-stubs/wordpress-stubs

Rector 1.0 (February 2024) introduced the RectorConfig::configure() builder used below; older tutorials with static function ( RectorConfig $config ) still work but look different.

rector.php

<?php
// rector.php in the plugin root
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;

return RectorConfig::configure()
    ->withPaths( array(
        __DIR__ . '/src',
        __DIR__ . '/ha-plugin.php',
    ) )
    ->withSkip( array(
        __DIR__ . '/vendor',
        __DIR__ . '/src/Legacy',
    ) )
    ->withBootstrapFiles( array(
        __DIR__ . '/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php',
    ) )
    ->withPhpSets( php81: true )
    ->withRules( array(
        NullToStrictStringFuncCallArgRector::class,
    ) )
    ->withTypeCoverageLevel( 0 )
    ->withDeadCodeLevel( 0 );

What the settings do:

  • withPaths() limits Rector to the plugin’s own code. vendor is skipped; code from other packages is never rewritten.
  • The WordPress stubs tell Rector the signatures of core functions, so it knows that get_option() returns mixed and esc_html() wants a string. Bootstrapping wp-load.php instead would load a whole WordPress with database connection for a code analysis.
  • withPhpSets( php81: true ) applies the language upgrades up to PHP 8.1: short array syntax, ?? instead of isset() ternaries, str_contains(), constructor promotion and others.
  • NullToStrictStringFuncCallArgRector wraps arguments that can be null in (string) casts where a string function expects a string, which removes the PHP 8.1 deprecations.
  • withTypeCoverageLevel() and withDeadCodeLevel() enable the type declaration and dead code rules step by step: level 0 first, then raise the number in later runs.

Run it

vendor/bin/rector process --dry-run          # show the diff, change nothing
vendor/bin/rector process                    # apply
vendor/bin/rector process src/Admin/Settings.php --dry-run   # one file

Commit before every run, and commit each run separately with the rule level in the message. A diff of 3,000 lines cannot be reviewed; ten diffs of 300 lines can.

Review what changed

Rector’s changes are mechanically correct, but they can change behavior in code that relied on loose PHP:

// before
function ha_get_label( $key ) {
    $labels = get_option( 'ha_labels' );
    return isset( $labels[ $key ] ) ? $labels[ $key ] : '';
}
// after the PHP sets and type rules
function ha_get_label( $key ): string {
    $labels = get_option( 'ha_labels' );
    return $labels[ $key ] ?? '';
}

The added : string return type throws a TypeError if the option contains a number or an array for that key, where the old code returned it silently. Types are added where Rector can prove them from the code; for data from the database it can only see what the code does, not what is stored. Run the tests and, without tests, at least the pages that use the changed functions. Type rules that touch hook callbacks deserve extra care: WordPress passes whatever other code returned to a filter, and a strict parameter type turns a sloppy third-party value into a fatal error.

In CI

vendor/bin/rector process --dry-run --no-progress-bar

The command exits with a non-zero code when it would change something, so a CI job fails on new code in the old style. Together with PHPStan: static analysis with PHPStan for WordPress plugins.

NOTE: Raise Requires PHP in the plugin header to the version of the PHP set before releasing the converted code; WordPress then refuses to activate the plugin on older PHP instead of failing with a parse error. Rector does not upgrade WordPress API usage (deprecated functions, old hooks); that part stays manual.