DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Practical PHP Refactoring: Extract Superclass

Practical PHP Refactoring: Extract Superclass

Giorgio Sironi user avatar by
Giorgio Sironi
·
Jan. 16, 12 · Interview
Like (0)
Save
Tweet
Share
5.54K Views

Join the DZone community and get the full member experience.

Join For Free

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>";
    }
}
PHP Extract

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Using GPT-3 in Our Applications
  • 3 Main Pillars in ReactJS
  • Practical Example of Using CSS Layer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: