vendredi 20 janvier 2017

Refactoring PHP file to remove code duplication

I have a class that imports some users, transforms the users information and then inserts it into a database. I have on file ImportEmployees that has a method which calls 2 other classes to import data. One class gets some users matching certain criteria and the other class handles the remaining users. These two classes do exactly the same thing except for one method. That one method is what extracts the users data correctly so there are 2 additional, different classes that it will call. What is a good design pattern that handles this well, or a better way to handle this to remove code duplication.

Here is the entry point class ImportEmployees: full class here

public function handle()
{
    handle(new ImportNapaUsers($this->file));
    handle(new ImportNonNapaUsers($this->file));
}

And the ImportNapaUsers class: full class here

private function extractUsers()
{
    $users = new ExtractNapaUsers($this->users);
    $this->napaUsers = $users->getUsers();
}

And the ImportNonNapaUsers class: full class here

private function extractUsers()
{
    $users = new ExtractNonNapaUsers($this->users);
    $this->napaUsers = $users->getUsers();
}

$this->users is the exact same in both of these. Also, both classes are identical except for the extractUsers method.

Aucun commentaire:

Enregistrer un commentaire