Struggling with config/execution on WordPress project #9159
-
I've used Rector before without issue, but working on another project now and running into an issue (well multiple). Basically been down a circular path of "why this not working". So would love if there's something stupid I'm doing or a good workaround. Here's the story in short: So being WordPress we have a number of plugins that we use that are ours and we want processed by Rector. But also there's a lot of libraries and other-other plugins that provide methods used by ours, etc. So basically I started off with config that looked similar to this: return RectorConfig::configure()
->withPaths([
__DIR__ . '/wp-content/mu-plugins', // 👈 We will exclude 'vendor' below 👇
__DIR__ . '/wp-content/plugins/COMPANY*', // 👈 Only care about 'our' plugins
__DIR__ . '/wp-content/sites',
__DIR__ . '/wp-content/themes/COMPANY*', // 👈 Only care about 'our' themes
])
->withSkip([
// Separate one-off composer vendor folder that exists (not main one) that needs skipped
__DIR__ . '/wp-content/mu-plugins/vendor',
])
->withAutoloadPaths([
// Pull in all libraries that aren't composer-installed & autoloaded already
__DIR__ . '/wp-content/lib/Linkify',
__DIR__ . '/wp-content/lib/mandrill',
__DIR__ . '/wp-content/lib/Stripe',
__DIR__ . '/wp-content/*.php',
__DIR__ . '/wp-content/plugins',
__DIR__ . '/wp', // 👈 Make sure it understands 'wp itself' which is installed in a sub-folder
])
.... So the initial problem(s) were that it was complaining about constants it couldn't find and was expecting. Right. So I had to end up adding in a bootstrap of return RectorConfig::configure()
->withPaths([
__DIR__ . '/wp-content/mu-plugins', // 👈 We will exclude 'vendor' below 👇
__DIR__ . '/wp-content/plugins/COMPANY*', // 👈 Only care about 'our' plugins
__DIR__ . '/wp-content/sites',
__DIR__ . '/wp-content/themes/COMPANY*', // 👈 Only care about 'our' themes
])
->withSkip([
// Separate one-off composer vendor folder that exists (not main one) that needs skipped
__DIR__ . '/wp-content/mu-plugins/vendor',
])
->withAutoloadPaths([
// Pull in all libraries that aren't composer-installed & autoloaded already
__DIR__ . '/wp-content/lib/Linkify',
__DIR__ . '/wp-content/lib/mandrill',
__DIR__ . '/wp-content/lib/Stripe',
__DIR__ . '/wp-content/*.php',
__DIR__ . '/wp-content/plugins',
__DIR__ . '/wp', // 👈 Make sure it understands 'wp itself' which is installed in a sub-folder
])
->withBootstrapFiles([
__DIR__ . '/wp-config-env.php',
])
.... OK that solved that problem, but then it was complaining that return RectorConfig::configure()
->withPaths([
__DIR__ . '/wp-content/mu-plugins', // 👈 We will exclude 'vendor' below 👇
__DIR__ . '/wp-content/plugins/COMPANY*', // 👈 Only care about 'our' plugins
__DIR__ . '/wp-content/sites',
__DIR__ . '/wp-content/themes/COMPANY*', // 👈 Only care about 'our' themes
])
->withSkip([
// Separate one-off composer vendor folder that exists (not main one) that needs skipped
__DIR__ . '/wp-content/mu-plugins/vendor',
])
->withAutoloadPaths([
// Pull in all libraries that aren't composer-installed & autoloaded already
__DIR__ . '/wp-content/lib/Linkify',
__DIR__ . '/wp-content/lib/mandrill',
__DIR__ . '/wp-content/lib/Stripe',
__DIR__ . '/wp-content/*.php',
__DIR__ . '/wp-content/plugins',
])
->withBootstrapFiles([
__DIR__ . '/wp-config-env.php',
__DIR__ . '/wp-content/lib/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php',
])
.... And here is where things get extra frustrating. So that works, but then I get errors about methods from But it turns out that a number of WP plugins end up hard-re-including core WP files, such as So the solution is the just add a withSkip statement of So long story long. I'm stuck trying to figure out how to get it to work ... wherein it either 'scans the WP files to understand any functions being declared in them' ... OR ... 'uses a bootstrap for that effect but then doesn't explode when those files are hard-included again'. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I suggest to only run on root project or when composer.json exists in the root rector.php located to avoid autoload juggling :) Once you found a path that have its own |
Beta Was this translation helpful? Give feedback.
I suggest to only run on root project or when composer.json exists in the root rector.php located to avoid autoload juggling :)
Once you found a path that have its own
composer.json
, addrector.php
there, and run from that path