Rumel
Rumel

Reputation: 319

Refactor code to reduce redundancy in PHP

I have several classes that share common logic. For example, class AddEmployee is responsible for adding an employee to the api. AddLocation is responsible for adding location of a person. Classes are following :

class AddEmployee
{
   public function Add()
   {
      $apiUrl = "https://api.abc.com/Employees";;

        $authParameters = array(
            'oauth_consumer_key' => $this->CONSUMER_KEY,

        );

        $xml .= <<<EOM
                <SuperFund>
                <ABN>$obj->abn</ABN>
                <Type>REGULATED</Type>
                </SuperFund>
                EOM;

        $queryParameters = array(
            'xml' => $xml
        );

        $response = $this->ProcesssRequestAndGetResponse('POST',$apiUrl,$authParameters,$queryParameters,$oauth_secret);

        return $response;

   }
}


class AddLocation
{
   public function Add()
   {
      $apiUrl = "https://api.abc.com/Locations";;

        $authParameters = array(
            'oauth_consumer_key' => $this->CONSUMER_KEY,

        );

        $xml .= <<<EOM
                <Location>
                <Address1>$obj->abn</Address1>
                <City>Dhaka</Citry>
                </Location>
                EOM;

        $queryParameters = array(
            'xml' => $xml
        );

        $response = $this->ProcesssRequestAndGetResponse('POST',$apiUrl,$authParameters,$queryParameters,$oauth_secret);

        return $response;

   }
}

In the above two classes, only xml part is different and others are same. In future other new classes will be added where only xml will be different.

My question is how can I refactor to remove duplicate from each of the classes ?

Which design pattern to remove duplicate code ?

Upvotes: 0

Views: 113

Answers (2)

Carl Manaster
Carl Manaster

Reputation: 40336

class AddThing {
    public function Add() {//all your stuff, except it calls getXml()}
    public abstract function getXml();
}

class AddEmployee extends AddThing {
    public function getXml() { // get the employee XML }
}

class AddLocation extends AddThing {
    public function getXml() { // get the location XML }
}

Upvotes: 2

Mike Robinson
Mike Robinson

Reputation: 8945

In this case: "nevermind 'patterns' ... just do it."

It is quite obvious that you could define a (protected ...) method which takes two parameters:   URL and XML. Simply spin-off the bulk of the logic into that method, then change the other (public ...) methods to call it.

Also (and, "IMHO"):   "at the end of the day, 'design patterns' are meant to be guidelines." Rules-of-thumb, if you will. Not everything that you encounter in real life will ever exactly conform to a "pattern," nor do you need to feel that "you must find one" to justify what you, as an engineer, decide to do.

Instead, think very-pragmatically about the application, the work-group, and the changes that you anticipate being necessary in the future. Try to design methods in such a way that your successors, when faced with an inevitable future change, might not have to change "a hundred methods." (Instead, thanks to your foresight, they need only change a few.) Use your best judgment, after discussing the matter carefully with your manager and with other members of your team.

Upvotes: 2

Related Questions