A comprehensive Rector package with custom rules specifically designed for WP Lemon projects. This package includes Rector itself along with custom transformation rules for the Bulldozer block framework.
Install this package in your WP Lemon project using Composer:
composer require --dev studiolemon/wp-lemon-rectorThis will install Rector along with all custom WP Lemon rules.
Since this package includes Rector, you can run it directly without any additional configuration:
# Using the WP Lemon Rector executable
vendor/bin/wp-lemon-rector process path/to/your/blocks --dry-run
vendor/bin/wp-lemon-rector process path/to/your/blocks
# Or using Rector directly
vendor/bin/rector process path/to/your/blocks --dry-run
vendor/bin/rector process path/to/your/blocksIf you need to customize paths, add additional rules, or change settings, create a rector.php file in your project root:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/web/app/themes/your-theme/blocks',
__DIR__ . '/web/app/plugins/your-plugin/src',
])
// Import the WP Lemon Rector configuration
->withSets([
__DIR__ . '/vendor/studiolemon/wp-lemon-rector/rector.php',
]);This package includes custom Rector rules tailored for WP Lemon's Bulldozer block framework:
Transforms array assignments to the add_class() method:
// Before
$this->classes[] = 'section hero alignfull has-background';
// After
$this->add_class(['section', 'hero', 'alignfull', 'has-background']);Optimizes multiple consecutive add_class() calls by merging them into a single call:
// Before
$this->add_class(['bg-pill']);
$this->add_class(['section', 'has-background']);
// After
$this->add_class(['bg-pill', 'section', 'has-background']);Transforms property assignments on a Site_Icons instance into a single constructor array argument:
// Before
$icons = new Site_Icons();
$icons->short_name = 'BusinessBase';
$icons->background_color = '#ffffff';
$icons->theme_color = '#f5f9e5';
// After
$icons = new Site_Icons([
'short_name' => 'BusinessBase',
'background_color' => '#ffffff',
'theme_color' => '#f5f9e5',
]);Transforms the align attribute assignment to the set_alignment() method:
// Before
$this->attributes['align'] = 'full';
// After
$this->set_alignment('full');Transforms the id attribute assignment to the set_anchor() method:
// Before
$this->attributes['id'] = 'formulier';
// After
$this->set_anchor('formulier');Fallback transformation for attribute assignments into set_attribute() when no more-specific rule applies:
// Before
$this->attributes['foo'] = 'bar';
// After
$this->set_attribute('foo', 'bar');Transforms field array access to the get_field() method:
// Before
$value = $this->fields['image_field'];
// After
$value = $this->get_field('image_field');Transforms is_preview property access to method call:
// Before
if ($this->is_preview) {
// do something
}
// After
if ($this->is_preview()) {
// do something
}Transforms block_disabled property assignment to the set_disabled() method:
// Before
$this->block_disabled = true;
// After
$this->set_disabled();Transforms InnerBlocks HTML string concatenation to the create_inner_blocks() method:
// Before
'InnerBlocks' => '<InnerBlocks allowedBlocks="' . esc_attr(wp_json_encode($allowed_blocks)) . '" template="' . esc_attr(wp_json_encode($template)) . '" />'
// After
'InnerBlocks' => self::create_inner_blocks($allowed_blocks, $template)In addition to custom rules, this package includes:
- PHP 8.3+ Features: Modern PHP syntax and features
- Dead Code Removal: Removes unused code and variables
- Code Quality: Improves overall code quality
- Coding Style: Enforces consistent coding standards
- Type Declarations: Adds type hints where possible
- Privatization: Makes properties and methods private when possible
- Naming: Improves variable and method naming
- Early Return: Promotes early return patterns
- Strict Booleans: Enforces strict boolean comparisons
Some rules are intentionally skipped to maintain compatibility with WordPress:
StaticClosureRector- WordPress often uses dynamic closuresStaticArrowFunctionRector- WordPress often uses dynamic arrow functionsReadOnlyPropertyRector- May conflict with WordPress patterns
Contributions are welcome! To add or modify rules:
- Edit or add Rector rules in the
src/directory - Update the
rector.phpconfiguration to register new rules - Add test cases in
tests/blocks/ - Submit a pull request
MIT