Complex classes like LDAPUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAPUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class LDAPUser extends User |
||
5 | { |
||
6 | use LDAPCachableObject; |
||
7 | |||
8 | private $ldapObj; |
||
9 | private $server; |
||
10 | |||
11 | /** |
||
12 | * Initialize a LDAPUser object |
||
13 | * |
||
14 | * @SuppressWarnings("StaticAccess") |
||
15 | */ |
||
16 | public function __construct($data = false) |
||
17 | { |
||
18 | $this->server = \LDAP\LDAPServer::getInstance(); |
||
19 | $this->initialize($data); |
||
20 | } |
||
21 | |||
22 | private function checkChildGroup($array) |
||
39 | |||
40 | /** |
||
41 | * @param string $listName The name of the list to search |
||
42 | * @param Group $group The group to search inside |
||
43 | * @param string $dn The distringuished name to search for |
||
44 | */ |
||
45 | private function isInListOrChild($listName, $group, $dn) |
||
57 | |||
58 | private function uidInMemberUid($group, $uid) |
||
62 | |||
63 | public function isInGroupNamed($name) |
||
85 | |||
86 | protected $valueDefaults = array( |
||
87 | 'o' => 'Volunteer' |
||
88 | ); |
||
89 | |||
90 | protected $multiValueProps = array( |
||
91 | 'title', |
||
92 | 'ou', |
||
93 | 'host' |
||
94 | ); |
||
95 | |||
96 | protected $cachedOnlyProps = array( |
||
97 | 'uid' |
||
98 | ); |
||
99 | |||
100 | /** |
||
101 | * Allow write for the user |
||
102 | * |
||
103 | * @SuppressWarnings("StaticAccess") |
||
104 | */ |
||
105 | protected function enableReadWrite() |
||
115 | |||
116 | public function getGroups() |
||
134 | |||
135 | public function addLoginProvider($provider) |
||
139 | |||
140 | private function generateLDAPPass($pass) |
||
147 | |||
148 | public function setPass($password) |
||
164 | |||
165 | public function validate_password($password) |
||
169 | |||
170 | public function validate_reset_hash($hash) |
||
178 | |||
179 | public static function from_name($name, $data) |
||
193 | |||
194 | public function flushUser() |
||
215 | |||
216 | private function getHashFromUser($ldapObj) |
||
224 | |||
225 | public function getPasswordResetHash() |
||
240 | |||
241 | public function delete() |
||
247 | } |
||
248 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
249 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.