DEV Community

Discussion on: PHP - Elegant method call

Collapse
 
suddenlyrust profile image
Ruslan • Edited

Thank you for your feedback Christian. Yeah for the use case of sending mails this makes no sense. It is a very simplified example. I use this method in the following case. Imagine you have a pretty big class. Like 500 or more lines and it is pretty hard to read it. You have many functions that perform different tasks. Now you can use this pattern to split the class in different little classes. Let's call the little classes "actions". Each action you can call now on your own and refactor it out of the big class.

// BigClass.php
class BigClass
{
    public function handle()
    {
        // Imagine a 500 line big file and plenty of functions

        // We have to parse a big XML-File

        // We have to harmonize the parsed Information

        // We have to store the values in different locations
    }
}

We can now refactor it like this.

// RefactoredBigClass.php
class RefactoredBigClass
{
    public function handle()
    {
        ParseXmlAction::execute();

        HarmonizeInformationAction::execute();

        StoreInformationAction::execute();
    }
}

// ParseXmlAction.php
class ParseXmlAction
{
    public function handle()
    {
        //
    }

    public static function execute()
    {
        (new static)->handle();
    }
}

// HarmonizeInformationAction.php
class HarmonizeInformationAction
{
    public function handle()
    {
        //
    }

    public static function execute()
    {
        (new static)->handle();
    }
}

// StoreInformationAction.php
class StoreInformationAction
{
    public function handle()
    {
        //
    }

    public static function execute()
    {
        (new static)->handle();
    }
}

In my eyes this helps to clean up my big classes. You get following benefits.

  • In the big class you can see very fast what is happening. What actions are executed here.
  • Every Action is now in a separate file and has a single task to do. No functions that are not used to solve the action.
  • You can reuse Actions in different places if it is required.
  • The best part is. You can test every action isolated.

I hope this explanation gives you a better understanding.

Collapse
 
vlasales profile image
Vlastimil Pospichal

This is a wrong way in my opinion. You need split data structures too, coupled with methods.

Collapse
 
klauenboesch profile image
Christian

While I understand your case (and I, for sure, have also written such code) I have two main concerns about your example:

  1. You say „a huge class that performs many tasks“. I‘ll just refer to „Single Responsibility Principle“ here: 1 Class -> 1 Purpose/Task

  2. How would you ever write a Unit test for your RefactoredBigClass? There is no way you could ever test your handle() function there.

Thread Thread
 
suddenlyrust profile image
Ruslan

Thank you for your concern, Christian.
At your first point: I don't know what you mean by that 🤔
And to your second point: I'm not unit testing this class 😁

Thread Thread
 
klauenboesch profile image
Christian

For the first point, see: scotch.io/bar-talk/s-o-l-i-d-the-f...
For the second point: Well, that's your fault ;)