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 637 posts at DZone. You can read more from them at their website. View Full User Profile

Practical PHP Refactoring: Replace Subclass with Fields

10.03.2011
| 4375 views |
  • submit to reddit

Sometimes a refactoring that start from type codes take the inverse direction.

One common case is when a hierarchy of subclasses presents variations only in constant data returned by public methods, or protected methods called as hooks.
These variations are usually hardcoded, or just pointing to constants, or class or object fields always initialized with the same value.

To simplify the design, the hierarchy (or part of it) that only variates with these values can be collapsed in a single class where one or more fields model the variable values.

Why it has come to this?

For example, because specialized logic has been removed due to a change or refinement in requirements. It's not common to introduce unnecessary subclasses if you're trying to come up with a simple design; but it's common to end up with superfluous ones after other refactorings or implementation changes.

Each refactoring is bidirectional: in this case we go back to fields instead of polymorphism. In this case it should be a simplification of the object model.

The fields-based solution allow for variation:

  • in constant values returned by methods;
  • in logic in the construction phase, by targeting a common constructor which is called by different Factory Methods, containing different logic.

It's rare that a subclass is a worthy concept of the model when it differs from the parent or its sibling just for a few constant return values. I also found out that you gain more flexibility by modelling the constant values with fields: you can configure them on the fly in testing scenarios, instead of using the same fixed values of the production ones.

This solution results in test code that inserts Dummy value for these fields and cover the entire hierarchy of cases, now targeting a single class instead of every single subclass. I'll show this simplification in the example.

Steps

  1. Replace usage of the constructor with a call to a Factory Method for each of the various subclasses: this way you can change the concrete class to instantiate in one place. You should put these Factory Methods in the superclass, since it will be the only one to survive this refactoring.
  2. Code that refers to subclasses, like type hints, should only refer to the superclass you are going to collapse them into.
  3. Add fields on the superclass for each of the values, usually private ones.
  4. Modify the superclass constructor to protected visibility and to accept the values of the fields.
  5. Now the subclasses should use this new version of the constructor and pass to it their constant values for the fields: you're still instantiating different concrete classes.
  6. Implement each constant method in the superclass, returning the correspondent field; now the method can be removed from the subclass. Repeat.
  7. For each subclass, if its constructor has any additional logic, you can inline that in the correspondent Factory Method on the superclass. You can then remove the subclass.

Usually the standard values for the fields are just a few, so you leave the Factory Methods in place.

Example

We start from a case of two specialization of a link (<a>) View Helper, an object producing HTML code. Our hierarchi consists of a class Link with an abstract method to differentiate the rendered HTML, and an ExternalLink and InternalLink couple of subclasses.

Some commits ago, these subclasses rendered very differently: for example, one of them added a target="_blank", or an indirection URL allow for counting the clicks on the external link.

Now, the classes have become almost the same.

<?php
class ReplaceSubclassWithFields extends PHPUnit_Framework_TestCase
{
    public function testInternalLinkShouldRender()
    {
        $a = new InternalLink('/posts/32', 'Last post');
        $this->assertEquals('<a href="/posts/32" class="internal">Last post</a>',
                            $a->__toString());
    }

    public function testExternalLinkShouldRender()
    {
        $a = new ExternalLink('http://www.google.com', 'Google');
        $this->assertEquals('<a href="http://www.google.com" class="external">Google</a>',
                            $a->__toString());
    }
}

abstract class Link
{
    private $href;
    private $text;

    public function __construct($href, $text)
    {
        $this->href = $href;
        $this->text = $text;
    }

    public function __toString()
    {
        return '<a href="' . $this->href 
             . '" class="' . $this->getCssClass() . '">'
             . $this->text . '</a>';
    }

    abstract protected function getCssClass();
}

class InternalLink extends Link
{
    public function getCssClass()
    {
        return 'internal';
    }
}

class ExternalLink extends Link
{
    public function getCssClass()
    {
        return 'external';
    }
}

We introduce Factory Methods on Link: now all the client code should call them.

<?php
class ReplaceSubclassWithFields extends PHPUnit_Framework_TestCase
{
    public function testInternalLinkShouldRender()
    {
        $a = Link::internalLink('/posts/32', 'Last post');
        $this->assertEquals('<a href="/posts/32" class="internal">Last post</a>',
                            $a->__toString());
    }

    public function testExternalLinkShouldRender()
    {
        $a = Link::externalLink('http://www.google.com', 'Google');
        $this->assertEquals('<a href="http://www.google.com" class="external">Google</a>',
                            $a->__toString());
    }
}

abstract class Link
{
    private $href;
    private $text;

    public function __construct($href, $text)
    {
        $this->href = $href;
        $this->text = $text;
    }

    public static function internalLink($href, $text)
    {
        return new InternalLink($href, $text);
    }

    public static function externalLink($href, $text)
    {
        return new ExternalLink($href, $text);
    }

    public function __toString()
    {
        return '<a href="' . $this->href 
             . '" class="' . $this->getCssClass() . '">'
             . $this->text . '</a>';
    }

    abstract protected function getCssClass();
}

We add the field $cssClass, which is now valorized with different values by the Factory Method (skipping the usage of constructors in the subclasses, which aren't present). Now we can use the new field in the superclass, and we do not need logic anymore in the subclasses.

abstract class Link
{
    private $href;
    private $text;
    private $cssClass;

    public function __construct($href, $text, $cssClass)
    {
        $this->href = $href;
        $this->text = $text;
        $this->cssClass = $cssClass;
    }

    public static function internalLink($href, $text)
    {
        return new InternalLink($href, $text, 'internal');
    }

    public static function externalLink($href, $text)
    {
        return new ExternalLink($href, $text, 'external');
    }

    public function __toString()
    {
        return '<a href="' . $this->href 
             . '" class="' . $this->getCssClass() . '">'
             . $this->text . '</a>';
    }

    abstract protected function getCssClass();
}

The last step is to make Link into a concrete class (it could already have been) and delete the unnecessary subclasses.

class Link
{
    private $href;
    private $text;
    private $cssClass;

    public function __construct($href, $text, $cssClass)
    {
        $this->href = $href;
        $this->text = $text;
        $this->cssClass = $cssClass;
    }

    public static function internalLink($href, $text)
    {
        return new Link($href, $text, 'internal');
    }

    public static function externalLink($href, $text)
    {
        return new Link($href, $text, 'external');
    }

    public function __toString()
    {
        return '<a href="' . $this->href 
             . '" class="' . $this->cssClass . '">'
             . $this->text . '</a>';
    }
}

I leave the constructor public for a further step, made possible by the fact that the Factory Method are really simple and will be tested indirectly by other functional or end-to-end tests. Thus, I can simplify the unit tests to use a dummy value for the new field.

Now we have less cases to cover: the variations due to the different values of the field that collapsed the subclasses have been eliminated even from the test code.

<?php
class ReplaceSubclassWithFields extends PHPUnit_Framework_TestCase
{
    public function testInternalLinkShouldRenderWithTheRightCssClass()
    {
        $a = new Link('/posts/32', 'Last post', 'myClass');
        $this->assertEquals('<a href="/posts/32" class="myClass">Last post</a>',
                            $a->__toString());
    }
}
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.)