Installation
composer require a9f/fractor
Requires PHP 8.1+ and Laravel 9+ (or standalone CLI usage).
First Use Case Run a basic refactoring command (e.g., for PHP files):
php artisan fractor:refactor --type=php --rule=example-rule
(Note: Default rules aren’t included; see Implementation Patterns for rule integration.)
Key Files to Explore
config/fractor.php: Core configuration (paths, default rules).app/Console/Commands/FractorCommand.php: CLI entry point.src/RefactorableInterface.php: Contract for refactorable files.Define a Rule
Create a custom rule class (e.g., app/Rules/UpdateNamespaceRule) implementing Fractor\Rule\RuleInterface:
use Fractor\Rule\RuleInterface;
use Fractor\File\Refactorable;
class UpdateNamespaceRule implements RuleInterface {
public function apply(Refactorable $file): void {
$file->replaceNamespace('Vendor\\NewNamespace');
}
}
Register the Rule
Bind it in config/fractor.php under rules or use a service provider:
Fractor::registerRule('update-namespace', UpdateNamespaceRule::class);
Execute via Artisan
php artisan fractor:refactor --type=php --rule=update-namespace --path=app/Models
Batch Processing
Use --batch to process files sequentially with rollback safety:
php artisan fractor:refactor --batch --dry-run
filesystem.updated).post-checkout or post-merge to auto-fix files.Fractor\File\Refactorable for non-PHP files (e.g., YAML, JSON).Rule Dependencies
use statements). Always test with --dry-run.if (!$file->hasNamespace()) return;
Path Handling
--path are resolved from the project root, not the command’s working directory.realpath() for clarity.Stateful Refactoring
file_put_contents). Use Refactorable::save() for atomic changes.Performance
--chunk=100 to process in batches.Dry Run Mode
php artisan fractor:refactor --dry-run --verbose
Shows changes without modifying files.
Log Output
Enable debug mode in config/fractor.php:
'debug' => env('FRACTOR_DEBUG', false),
Custom File Analyzers
Override Fractor\File\Analyzer\AnalyzerInterface to parse domain-specific files (e.g., Blade templates).
Pre/Post-Refactor Hooks Bind events in a service provider:
Fractor::beforeRefactor(function (Refactorable $file) {
// Validate file before changes
});
Rule Validation Add metadata to rules for dynamic filtering:
class MyRule implements RuleInterface {
public static function supports(string $filePath): bool {
return str_ends_with($filePath, '.php') && file_exists($filePath);
}
}
How can I help you explore Laravel packages today?