Rector: [Wordpress] Use Rector with Wordpress?

Created on 19 Dec 2019  路  6Comments  路  Source: rectorphp/rector

Question

Hi I'm playing around with rector and try to upgrade an old theme from 5.x to 7.2. So when I run

vendor/bin/rector process --set php72 --dry-run

I have two errors which I'm not able to solve.

Problem 1: Call to undefined function x()

The first is the Call to undefined function x() problem. Since wordpress exposes functions global like [get_template_directory()](https://developer.wordpress.org/reference/functions/get_template_directory/) or add_filter() and so on.

Problem 2:

If you work on a theme you can access plugin classes if they exist.
Most of the plugins doesn't offer you an autoloader. So I run in this:

         "Analyze error: "Class myplugin\classes\field_input
         was not found while trying to analyse it - autoloading is probably
         not configured properly.". Include your files in "parameters >
         autoload_paths".
         See https://github.com/rectorphp/rector#extra-autoloading".

Is there any workaround or maybe can I ignore this errors somehow?

Thanks

Most helpful comment

I guess you can't run rector on WordPress. It has require everywhere. Relies on globals, defines and other stuff that I was not able to run rector. You can add several files from wp-includes to autoload-path, but it comes the point, where it wants to access some globals and this is so awkward weird stuff from year 2003.

All 6 comments

  1. These functions needs to be loaded, put file with them into parameters > autoload_paths

  2. Better autoload it again the same way in 1, as you'll need that in the future, but you can skip it for now with --hide-autoload-errors

Hey

thanks for the answer but lets say the get_template_directory() relies in theme.php my rector.yaml will look like this?

services:
    Rector\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector: ~

parameters:
    sets:
        - 'code-quality'
        - 'php71'
        - 'php72'
        - 'php73'
    exclude_paths:
        - 'web/app/plugins'
        - 'vendor/*'
    php_version_features: '7.3'
    paths:
        - 'web/app/themes'
    autoload_paths:
        - './vendor/autoload.php'
        - './web/app/themes/mjtheme-child/vendor/autoload.php'
        - './web/app/themes/mjtheme/vendor/autoload.php'
        - './web/wp/wp-includes/theme.php'
        - './web/wp/wp-includes/plugin.php'
        - './web/app/mu-plugins/mj-fewbricks/lib/fewbricks.php'

I run it with

vendor/bin/rector process --config rector.yaml --set php72 --dry-run

Looks good. What happens when you run it?

You can use parameters > autoload_paths or composer.json autoload

Closing as answered

I guess you can't run rector on WordPress. It has require everywhere. Relies on globals, defines and other stuff that I was not able to run rector. You can add several files from wp-includes to autoload-path, but it comes the point, where it wants to access some globals and this is so awkward weird stuff from year 2003.

I see. It is technically possible to handle such cases, but it's not in Rector yet:
See https://github.com/rectorphp/rector/issues/4330

At current state, it's looking for contributor that will add this feature to Rector

Was this page helpful?
0 / 5 - 0 ratings