Source: juststeveking.uk

Fun with Refactoring
Refactoring isn’t a dirty word, quite the opposite. Refactoring is something that you do when you have leveled up or generally improved.

We will use a real-life project that is open source on GitHub and do a refactoring exercise in stages.

'/../config/blueprint.php', 'blueprint' ); File::mixin(new FileMixins()); $this->app->bind('command.blueprint.build', fn ($app) => new BuildCommand($app['files'], app(Builder::class))); $this->app->bind('command.blueprint.erase', fn ($app) => new EraseCommand($app['files'])); $this->app->bind('command.blueprint.trace', fn ($app) => new TraceCommand($app['files'], app(Tracer::class))); $this->app->bind('command.blueprint.new', fn ($app) => new NewCommand($app['files'])); $this->app->bind('command.blueprint.init', fn ($app) => new InitCommand()); $this->app->bind('command.blueprint.stubs', fn ($app) => new PublishStubsCommand()); $this->app->singleton(Blueprint::class, function ($app) { $blueprint = new Blueprint(); $blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer($app)); $blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer()); $blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer()); $blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer())); foreach (config('blueprint.generators') as $generator) { $blueprint->registerGenerator(new $generator($app['files'])); } return $blueprint; }); $this->app->make('events')->listen(CommandFinished::class, function ($event) { if ($event->command == 'stub:publish') { $this->app->make(Kernel::class)->queue('blueprint:stubs'); }}); $this->commands([ 'command.blueprint.build', 'command.blueprint.erase', 'command.blueprint.trace', 'command.blueprint.new', 'command.blueprint.init', 'command.blueprint.stubs', ]); }

$this->app->bind(BuildCommand::class, fn ($app) => new BuildCommand($app['files'], app(Builder::class))); $this->app->bind(EraseCommand::class, fn ($app) => new EraseCommand($app['files'])); $this->app->bind(TraceCommand::class, fn ($app) => new TraceCommand($app['files'], app(Tracer::class))); $this->app->bind(NewCommand::class, fn ($app) => new NewCommand($app['files'])); $this->app->bind(InitCommand::class, fn ($app) => new InitCommand()); $this->app->bind(PublishStubsCommand::class, fn ($app) => new PublishStubsCommand()); This is the first step; next, let us add named arguments to make this look cleaner.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners