Laravel0Fun with Refactoring | Laravel News

[ad_1]

Refactoring isn’t a dirty word, quite the opposite. Refactoring is something that you do when you have leveled up or generally improved. It is impossible to refactor something if you have not improved unless you didn’t try the first time, of course!

This tutorial will discuss refactoring, what we could do, and how we might approach a few real-world examples. We will use a real-life project that is open source on GitHub and do a refactoring exercise in stages. The project we will use is Laravel Shift Blueprint, a popular way to bootstrap your Laravel applications.

Let’s dive in. I have created a new repository fork to build things in isolation – running GitHub actions in my account so that the core library is not flooded. Now that I have done that, I can clone my fork and start working locally.

The first thing I noticed when opening the project is that composer needs to set a required PHP level. This isn’t a huge problem – however, being me, I would put this to the minimum supported PHP version. PHP 8.0 is the only version with enough support to feel comfortable writing this. Let’s make our first change.

1"require": {

2 "doctrine/dbal": "^3.3",

3 "illuminate/console": "^9.0",

4 "illuminate/filesystem": "^9.0",

5 "illuminate/support": "^9.0",

6 "laravel-shift/faker-registry": "^0.2.0",

7 "symfony/yaml": "^6.0"

8},

After updating our dependencies and adding the language constraint, it now looks like the following:

1"require": {

2 "php": "^8.0",

3 "doctrine/dbal": "^3.5",

4 "illuminate/console": "^9.39",

5 "illuminate/filesystem": "^9.39",

6 "illuminate/support": "^v9.39",

7 "laravel-shift/faker-registry": "^0.2.0",

8 "symfony/yaml": "^6.1"

9},

We need to run tests before and after each change to ensure that the latest change has not broken anything. We either need to revert or refactor the broken part if it has. This is a crucial step in the refactoring process, as you always want to ensure you are refactoring for the betterment of the project – not just because you have a different opinion.

1OK (430 tests, 2056 assertions)

So far, so good! Let’s now move on and look at the code specifically. I noticed it was not taking advantage of type hinting or return types when opening a random file in the codebase. Also, as we have now set the minimum PHP version, we can update how we use the PHP language. Let’s look at a quick example:

1class Tree

2{

3 private $tree;

4 

5 public function __construct(array $tree)

6 {

7 $this->tree = $tree;

8 

9 $this->registerModels();

10 }

11 

12 private function registerModels()

13 {

14 $this->models = array_merge($this->tree['cache'] ?? [], $this->tree['models'] ?? []);

15 }

16 

17 // More code was there, but let's simplify

18}

Looking at this code, we know we can make some quick wins in refactoring – just to the language level we now want to set at the minimum. First, let us fix that constructor and remove the property.

1class Tree

2{

3 public function __construct(

4 private array $tree,

5 ) {

6 $this->registerModels();

7 }

8 

9 // ...

10}

Moving over to using constructor property promotion allows us to simplify our code. Let’s run our tests.

1OK (430 tests, 2056 assertions)

Still good. We made an easy refactor of this code and can move on to the next part. Let’s look at the method called in the constructor. We have a dynamic property on the class – which is being dropped in future versions of PHP support. While it may not be a problem right now – we know that in the future, it will be. So we can refactor this in advance.

1class Tree

2{

3 public function __construct(

4 private array $tree,

5 private array $models = [],

6 ) {

7 $this->registerModels();

8 }

9 

10 private function registerModels(): void

11 {

12 $this->models = [

13 ...$this->tree['cache'] ?? [],

14 ...$this->tree['models'] ?? []

15 ];

16 }

17 

18 // ...

19}

We have now set the property for the class in the constructor – and refactored the array merge to use array destructuring. This makes our code much easier to read. Let’s rerun those tests!

1OK (430 tests, 2056 assertions)

Let us look at the following method: a getter to get the controllers from the Tree class. We currently have no return type and need to know what this may contain. Doing a quick look through the code base using “Find Usages” I see that elsewhere we are dynamically setting this to an instance of Controller. Let’s have a look at this method:

1public function controllers()

2{

3 return $this->tree['controllers'];

4}

Firstly, we want to add the return type, which will be an array.

1public function controllers(): array

2{

3 return $this->tree['controllers'];

4}

So far, so good. All tests are still passing. But is this enough? Should we add a dynamic type setting in our project when we can let our project know about this in one place? Let’s update this.

1/**

2 * @return array<int,Controller>

3 */

4public function controllers(): array

5{

6 return $this->tree['controllers'];

7}

We know this will return a Controller, so why not set this type in our docblock as generic so we can make the most of modern PHP? Now to rerun our tests.

1OK (430 tests, 2056 assertions)

So far, our refactors have been successful. Let’s move on to another example.

Next, let’s look at the ServiceProvider for this package. Like the other example, there aren’t any return types – so I will quickly get them added and not worry about those examples here. Let’s have a look at how the register method could be improved.

1public function register(): void

2{

3 $this->mergeConfigFrom(

4 __DIR__ . '/../config/blueprint.php',

5 'blueprint'

6 );

7 

8 File::mixin(new FileMixins());

9 

10 $this->app->bind('command.blueprint.build', fn ($app) => new BuildCommand($app['files'], app(Builder::class)));

11 $this->app->bind('command.blueprint.erase', fn ($app) => new EraseCommand($app['files']));

12 $this->app->bind('command.blueprint.trace', fn ($app) => new TraceCommand($app['files'], app(Tracer::class)));

13 $this->app->bind('command.blueprint.new', fn ($app) => new NewCommand($app['files']));

14 $this->app->bind('command.blueprint.init', fn ($app) => new InitCommand());

15 $this->app->bind('command.blueprint.stubs', fn ($app) => new PublishStubsCommand());

16 

17 $this->app->singleton(Blueprint::class, function ($app) {

18 $blueprint = new Blueprint();

19 $blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer($app));

20 $blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());

21 $blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer());

22 $blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));

23 

24 foreach (config('blueprint.generators') as $generator) {

25 $blueprint->registerGenerator(new $generator($app['files']));

26 }

27 

28 return $blueprint;

29 });

30 

31 $this->app->make('events')->listen(CommandFinished::class, function ($event) {

32 if ($event->command == 'stub:publish') {

33 $this->app->make(Kernel::class)->queue('blueprint:stubs');

34 }

35 });

36 

37 $this->commands([

38 'command.blueprint.build',

39 'command.blueprint.erase',

40 'command.blueprint.trace',

41 'command.blueprint.new',

42 'command.blueprint.init',

43 'command.blueprint.stubs',

44 ]);

45}

First up, we are binding quite a bit to the container and using string constants, which is ok – but we could quickly improve this.

1$this->app->bind(BuildCommand::class, fn ($app) => new BuildCommand($app['files'], app(Builder::class)));

2$this->app->bind(EraseCommand::class, fn ($app) => new EraseCommand($app['files']));

3$this->app->bind(TraceCommand::class, fn ($app) => new TraceCommand($app['files'], app(Tracer::class)));

4$this->app->bind(NewCommand::class, fn ($app) => new NewCommand($app['files']));

5$this->app->bind(InitCommand::class, fn ($app) => new InitCommand());

6$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.

1$this->app->bind(

2 abstract: BuildCommand::class,

3 concrete: fn (Application $app): BuildCommand => new BuildCommand(

4 filesystem: $app['files'],

5 builder: $app->make(

6 abstract: Builder::class,

7 ),

8 ),

9);

10$this->app->bind(

11 abstract: EraseCommand::class,

12 concrete: fn (Application $app): EraseCommand => new EraseCommand(

13 filesystem: $app['files'],

14 ),

15);

16$this->app->bind(

17 abstract: TraceCommand::class,

18 concrete: fn (Application $app): TraceCommand => new TraceCommand(

19 filesystem: $app['files'],

20 tracer: $app->make(

21 abstract: Tracer::class,

22 ),

23 ),

24);

25$this->app->bind(

26 abstract: NewCommand::class,

27 concrete: fn (Application $app): NewCommand => new NewCommand(

28 filesystem: $app['files'],

29 ),

30);

31$this->app->bind(

32 abstract: InitCommand::class,

33 concrete: fn (): InitCommand => new InitCommand(),

34);

35$this->app->bind(

36 abstract: PublishStubsCommand::class,

37 concrete: fn () => new PublishStubsCommand(),

38);

Things, at least to me, now look a lot cleaner. We have more type hinting added and return types added for clarity. Not everyone will agree with the named arguments, but this makes the code nicer to read.

The purpose of refactoring is to make the code more manageable, performant, or both. What we have done above displays how we can make the code more type-safe and easier to manage moving forward. Blueprint is quite a complicated project. It has a lot of moving parts to make sure it works. Without spending a long time diving into the specifics of the project and its future goals, it is hard to see what improvements could be made.

My typical approach to refactoring is to hit those quick wins first, the supported language level and package version, then type hinting and return types, then move on to modernizing parts of the code. It is crucial to make sure, as you step through it, that you ensure tests are still running.

[ad_2]

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *