Agile Zone is brought to you in partnership with:

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

Practical PHP Refactoring: Change Bidirectional Association to Unidirectional

09.05.2011
| 5364 views |
  • submit to reddit

This refactoring is the inverse of Change Unidirectional Association to Bidirectional. We start from a bidirectional association, where two objects reference each other; the association is simplified to only comprehend one direction, deleting one of the two back pointers.

Why should I change back an association?

An unidirectional association is simpler to establish at instantiation time, and simpler to maintain: changing the object participating to one side of the association or simply removing the instance of the association is easier as it involves a single pointer (actually called handler in PHP).

Another problem with bidirectionality is garbage collection: usually in PHP objects which pertained to a cycle were not deleted even if unreachable from any other scope.(both of them still have a potential reference: the other object.) Thus, maintaining a bidirectional references ensured that the occupied memory would never be freed until the end of the script.

However, from PHP 5.3 a patch has been integrated to detect cycles and address this issue once and for all. So, performance should not be a parameter for choosing a unidirectional or bidirectional association.

An issue which will always be present instead is dependency: two classes participating to a bidirectional associations normally depend on each other, making them inseparable. Class A won't be instantiable or complete with class B, and vice versa: separating their responsibilities in different classes does not help very much without independency.

The dependency cycle can be broken by the introduction of an interface to implement for one of the two sides:


However an interface is an additional moving part, and has a maintenance cost too. Thus I would always prefer an unidirectional association to a perfectly decoupled bidirectional one.

 

When should I simplify?

Whenever possible: when there are no accesses to the field reference from one side, that field reference is not useful anymore and can be deleted with this refactoring.

In the special case of an ORM which needs a particular side to exist, you may have discovered a particular configuration allowing for unidirectional associations in the one-to-many case.

Everytime I say "one side is not used" you can also read "one side is really seldom used". There are alternatives for obtaining a reference to maintaining it all the time (such as passing it as a method parameter when required), so the refactoring to unidirectional association can always be considered.

Steps

Consider the one of the two references which you want to remove. For the refactoring to be feasible, one of these options must occur:

  • the reference must not be used;
  • there must be another way for obtaining a reference or for getting a message to the other object, but this is the subject of other refactorings;
  • the other object can be added as an argument to the methods which use it, changing the reference from a field to a parameter one.

For example, when a private field is used by a strong minority of methods in a class, is on its way to become a parameter to improve the cohesion of the class itself. The same occurs in this special case.

  1. The reference should be checked for usage or substituted with a parameter or another algorithm, according to the case this scenario falls in.
  2. Remove all assignments to the field, and the calls to the methods that assigned the field if they become empty.

For example, by eliminating the noncontrolling side, you will eliminate the call from the controlling one. While by eliminating the controlling side, which is rarer, you will eliminate the method on that side and change the name of the noncontrolling one to become part of the Api of the class.

Example

Initially, we check the field Phonenumber::$user is not used anymore. We could also pass the User object as a parameter in __toString() in case it's referenced (of course we will need to change the name as it won't be a magic method anymore.)

<?php
class ChangeBidirectionalAssociationToUnidirectional extends PHPUnit_Framework_TestCase
{
    public function testPhonenumbersDoNotNeedToReferToUsers()
    {
        $user = new User('Giorgio');
        $user->addPhonenumber(new Phonenumber('012345'));
        $this->assertEquals("012345\n", $user->phonenumbersList());
    }
}

class User
{
    private $name;
    private $phonenumbers;

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

    public function addPhonenumber(Phonenumber $phonenumber)
    {
        $this->phonenumbers[] = $phonenumber;
        $phonenumber->internalSetUser($this);
    }

    public function phonenumbersList()
    {
        $list = '';
        foreach ($this->phonenumbers as $number) {
            $list .= "$number\n";
        }
        return $list;
    }

    public function getName()
    {
        return $this->name;
    }
}

class Phonenumber
{
    private $number;

    /**
     * @var User
     */
    private $user;

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

    public function internalSetUser(User $u)
    {
        $this->user = $u;
    }

    public function __toString()
    {
        return $this->number;
    }
}

We remove the assignment code, and the field. The code still passes the test.

class User
{
    // ...

    public function addPhonenumber(Phonenumber $phonenumber)
    {
        $this->phonenumbers[] = $phonenumber;
        $phonenumber->internalSetUser($this);
    }
}

class Phonenumber
{
    private $number;

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

    public function internalSetUser(User $u)
    {
    }

    public function __toString()
    {
        return $this->number;
    }
}

We can now remove the Api of the noncontrolling side (since it's this side that has no references to the other.) The Phonenumber objects are now simpler and they do not depend on the User class anymore.

class User
{
    private $name;
    private $phonenumbers;

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

    public function addPhonenumber(Phonenumber $phonenumber)
    {
        $this->phonenumbers[] = $phonenumber;
    }

    public function phonenumbersList()
    {
        $list = '';
        foreach ($this->phonenumbers as $number) {
            $list .= "$number\n";
        }
        return $list;
    }

    public function getName()
    {
        return $this->name;
    }
}

class Phonenumber
{
    private $number;

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

    public function __toString()
    {
        return $this->number;
    }
}
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.)