Practical PHP Refactoring: Introduce Foreign Method
Join the DZone community and get the full member experience.
Join For FreeA new method is needed, as we are factoring out some lines of code. It would be nice to have it available on a class which already has all the fields to execute it, but unfortunately we cannot modify original code (because it's part of a library, or it's bundled with PHP.)
A classic example of Introduce Foreign Method is cause by a missing method on ArrayObject, or SplQueue, or other external classes. The first place to place a new method is the object the code works with, since it's cohesive with the rest of the data. The second best place is the client object, although the method could get duplicated in different clients. A Foreign Method is a method created in the client code to complete the functionality of a source object.
How it works
The source object becomes a first additional argument of the Foreign Method: Python and some other languages reflects this with the self first argument, which makes refactoring Foreign Methods into normal ones easier. For a PHP- example, consider PHPUnit: I use Foreign Methods in my test cases to wrap $this->getMock() multiple usages. I factor away the adaptation to the Api (use of false arguments or MockBuilder) and leave as arguments the variability of the mocks, like the number of calls or the expected parameters.
A Foreign Method is a work-around: if you have the possibility to change the source class, definitely go for that. Another alternative is to introduce a wrapper, when you can modify the lifecycle in order to inject a wrapper in the client code instead of the original object. It's not the case for PHPUnit (you extend the class), but it is for SplQueue and other collection objects; wrapping has also other benefits which will be shown in the next articles.
Steps
- Create the method in the client class.
- Make the server object the first parameter of the method, if it cannot be passed already through $this (in case it's referenced by a field).
- Substitute the duplicated code with method calls.
Fowler suggests also to comment the method calling it Foreign Method and saying it should be moved onto the server object when it will become possible.
Example
Initially, we're using an ArrayObject and continuing to ordering it after each addition of an element:
<?php class IntroduceForeignMethod extends PHPUnit_Framework_TestCase { public function testLinksAreViewedInOrder() { $links = new LinkGroup(); $links->addUrl('twitter.com'); $links->add('plus.google.com', 'Google+'); $links->add('facebook.com', 'Facebook'); $expected = "<a href=\"facebook.com\">Facebook</a>\n" . "<a href=\"plus.google.com\">Google+</a>\n" . "<a href=\"twitter.com\">twitter.com</a>"; $this->assertEquals($expected, $links->__toString()); } } class LinkGroup { private $links; public function __construct() { $this->links = new ArrayObject(); } public function add($url, $text) { $this->links[$url] = $text; $this->links->asort(); } public function addUrl($url) { $this->links[$url] = $url; $this->links->asort(); } public function __toString() { $links = array(); foreach ($this->links as $url => $text) { $links[] = "<a href=\"$url\">$text</a>"; } return implode("\n", $links); } }
We introduce a Foreign Method, newLink():
class LinkGroup { private $links; public function __construct() { $this->links = new ArrayObject(); } public function add($url, $text) { $this->newLink($url, $text); } public function addUrl($url) { $this->newLink($url, $url); } public function __toString() { $links = array(); foreach ($this->links as $url => $text) { $links[] = "<a href=\"$url\">$text</a>"; } return implode("\n", $links); } private function newLink($url, $text) { $this->links[$url] = $text; $this->links->asort(); } }
We also add some comments noting that if more and more Foreign Methods pop out, a radical solution would be needed:
/** * Foreign Method of the ArrayObject. Should be moved onto a newly extracted * collaborator which wraps the ArrayObject, or an heap-like data structure * should be used. */ private function newLink($url, $text) { $this->links[$url] = $text; $this->links->asort(); }
Opinions expressed by DZone contributors are their own.
Comments