Practical PHP Refactoring: Collapse Hierarchy
Join the DZone community and get the full member experience.
Join For FreeIn the scenario of today, a subclass and superclass are not very different: the evolution of the code has brought them to this situation. Behavior has been removed, or moved elsewhere in the system, and they are almost the same entity.
The Collapse Hierarchy refactoring provides a series of steps to unify two such classes into one, simplifying the design.
Why removing a class?
A design that has fewer moving parts is simpler to understand and a little harder to break. An additional entity that is not representing a concept of the domain, nor providing value by eliminating duplication, is just adding accidental complexity to the design.
Keeping a hierarchy to as fewer levels as possible is also beneficial as it avoids you going up and down the hierarchy to find where a called method is defined. Even when it produces a long class, it can be a step to exit from the local minimum: form a unique class that can be broken into little pieces with composition.
The assumption of this refactoring is that there are no other subclasses in the hierarchy; if there are, they should now extend the unified class, but you should look for possible violations of the Liskov Substitution Principle. For example in the transportation domain, it's not correct to collapse a Vehicle class into its subclass Coach while making the sibling class Car extend Coach as a result.
Steps
- Choose which class to remove: the subclass or the superclass.
- Use down refactorings until one of the classes is empty.
- All references (mostly instantiations, but also type hints and docblocks), should now refer to the class that you're keeping.
- Remove the empty class, which by now is only dead code.
Between steps 2 and 3 the tests may fail; execute them in short iterations to ensure you are always in a working state. Neither pulling everything up nor pushing everything down are general solution to keep the tests green: there are counterexamples for both cases of references that break, such as the instantiation of the superclass, or a type hint for the subclass.
Example
In the example, we work with the familiar class NewsFeedItem and its subclass Link. The requirements remained for them are few, and there are no other subclasses involved; there is actually no need for NewsFeedItem by itself, and it could just as well be abstract.
<?php class CollapseHierarchy extends PHPUnit_Framework_TestCase { public function testALinkShowsItsAuthor() { $link = new Link("/posts/php-refactoring", "giorgiosironi"); $this->assertEquals("<a href=\"/posts/php-refactoring\">/posts/php-refactoring</a> -- @giorgiosironi", $link->toHtml()); } } class NewsFeedItem { protected $content; protected $author; public function __construct($content, $author) { $this->content = $content; $this->author = '@' . ltrim($author, '@'); } /** * @return string an HTML printable version */ public function __toString() { return "$this->content -- $this->author"; } } class Link extends NewsFeedItem { public function toHtml() { return "<a href=\"$this->content\">$this->content</a> -- $this->author"; } }
We choose to remove the superclass. We push everything down, but we must look for references like new NewsFeedItem(...) to fix before accomplishing this step.
class NewsFeedItem { } class Link extends NewsFeedItem { protected $content; protected $author; public function __construct($content, $author) { $this->content = $content; $this->author = '@' . ltrim($author, '@'); } /** * @return string an HTML printable version */ public function __toString() { return "$this->content -- $this->author"; } public function toHtml() { return "<a href=\"$this->content\">$this->content</a> -- $this->author"; } }
Now we can remove the superclass altogether, along with the extends keyword on Link.
class Link { protected $content; protected $author; public function __construct($content, $author) { $this->content = $content; $this->author = '@' . ltrim($author, '@'); } /** * @return string an HTML printable version */ public function __toString() { return "$this->content -- $this->author"; } public function toHtml() { return "<a href=\"$this->content\">$this->content</a> -- $this->author"; } }
Opinions expressed by DZone contributors are their own.
Comments