I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 636 posts at DZone. You can read more from them at their website. View Full User Profile

Practical PHP Refactoring: Extract Superclass

01.16.2012
| 3841 views |
  • submit to reddit

In the scenario of today, there are two or more unrelated classes with similar members, like common methods or fields. While these classes may be already linked on the semantical level (via duplicated names or namespaces), there is nothing that ties them in the PHP interpreter's mind (if it has one.)

The Extract Superclass refactoring establishes an inheritance hierarchy by introducing a base class that the duplicated classes can extend. Subsequently, class members can be moved in the superclass to be unified and kept locked in a single place.

Why a base class?

A superclass is one of the ways we can use to remove duplication, in this case using inheritance. A often cleaner alternative is delegation, which we'll see it later in this series. This refactoring is an enabler for the Pull Up refactorings, that can only be executed after a hierarchy is in place.

The extracted superclass is abstract: there is usually no reason to make it concrete.

Tests are also not a concern as usually they already cover the two classes where duplication has formed; so there is no need to establish an independent test for the superclass. A corner case is when you have lots of subclasses that contain little logic and just configuration: in that case it would be a significant saving to add a concrete class just for testing. The final result would be a test case for that dummy, concrete class; the tests for the N production concrete classes would be deleted.

Steps

  1. Create an abstract superclass. Choose its name as the minimum common denominator for the existing classes.
  2. Execute Pull Up Field, Pull Up Method, and Pull Up Constructory Body where applicable. The order of application matters: the fields can be moved first, since the methods will continue to see them in the subclasses.
  3. After each (or a couple of, if you're brave) pull operations, run the tests.

Fowler suggestion here is that if there are still common parts between the methods of the subclasses, you can apply Extract Method and Pull Up Method to eliminate all forms of duplication.

A nice side-effect of this refactoring is that if client code depends only on the superclass API, you can remove type hints and other references on the concrete classes and just establish one towards the base class. You may discover you can reuse some code originally written for subclass A also on the other subclasses.

Example

Our starting point is a pair of unrelated presentation objects, Post and Link.

<?php
class ExtractSuperclass extends PHPUnit_Framework_TestCase
{
    public function testAPostShowsItsAuthor()
    {
        $post = new Post("Hello, world!");
        $this->assertEquals("<p>Hello, world!</p>",
                            $post->toHtml());
    }

    public function testALinkShowsItsAuthor()
    {
        $link = new Link("/posts/php-refactoring");
        $this->assertEquals("<p><a href=\"/posts/php-refactoring\">/posts/php-refactoring</a></p>",
                            $link->toHtml());
    }
}

class Post
{
    public function __construct($text)
    {
        $this->text = $text;
    }

    public function toHtml()
    {
        return "<p>" . $this->text . "</p>";
    }
}

class Link
{
    public function __construct($href)
    {
        $this->href = $href;
    }

    public function toHtml()
    {
        return "<p><a href=\"" . $this->href . "\">" . $this->href . "</a></p>";
    }
}

There is some duplication visible to the naked eye. Let's take some preliminary step to make it more evident, like unifying their fields by naming them as $this->content.

class Post
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    public function toHtml()
    {
        return "<p>" . $this->content . "</p>";
    }
}

class Link
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    public function toHtml()
    {
        return "<p><a href=\"" . $this->content . "\">" . $this->content . "</a></p>";
    }
}

We also unify the method toHtml() by extracting the different bit:

class Post
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    private function displayContent()
    {
        return $this->content;
    }

    public function toHtml()
    {
        return "<p>" . $this->displayContent() . "</p>";
    }
}

class Link
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    private function displayContent()
    {
        return "<a href=\"$this->content\">$this->content</a>";
    }

    public function toHtml()
    {
        return "<p>" . $this->displayContent() . "</p>";
    }
}

Now we can extract an empty superclass:

abstract class ParagraphBox
{
}

class Post extends ParagraphBox
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    private function displayContent()
    {
        return $this->content;
    }

    public function toHtml()
    {
        return "<p>" . $this->displayContent() . "</p>";
    }
}

class Link extends ParagraphBox
{
    public function __construct($content)
    {
        $this->content = $content;
    }

    private function displayContent()
    {
        return "<a href=\"$this->content\">$this->content</a>";
    }

    public function toHtml()
    {
        return "<p>" . $this->displayContent() . "</p>";
    }
}

Then, we pull everything that is common between the classes up, with the appropriate visibility (protected). We also add a definition for the $this->content field, which has been ignored and created as-needed until now. The tests (not shown) are still the same as in the first step, and passing.

abstract class ParagraphBox
{
    protected $content;

    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * @return string
     */
    abstract protected function displayContent();

    public function toHtml()
    {
        return "<p>" . $this->displayContent() . "</p>";
    }
}

class Post extends ParagraphBox
{
    protected function displayContent()
    {
        return $this->content;
    }

}

class Link extends ParagraphBox
{
    protected function displayContent()
    {
        return "<a href=\"$this->content\">$this->content</a>";
    }
}
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)