Complex classes like LDAPAuthenticator 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 LDAPAuthenticator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class LDAPAuthenticator extends Authenticator |
||
68 | { |
||
69 | /** The URL for the LDAP host */ |
||
70 | private $host; |
||
71 | /** The base DN for all users in the LDAP server */ |
||
72 | public $user_base; |
||
73 | /** The base DN for all groups in the LDAP server */ |
||
74 | public $group_base; |
||
75 | /** The DN to use to bind if binding as the read/write user */ |
||
76 | private $bindDistinguishedName; |
||
77 | /** The password to use to bind if binding as the read/write user */ |
||
78 | private $bindPassword; |
||
79 | /** The DN to use to bind if binding as the read only user */ |
||
80 | private $bindRODistinguishedName; |
||
81 | /** The password to use to bind if binding as the read only user */ |
||
82 | private $bindROPassword; |
||
83 | |||
84 | /** |
||
85 | * Create an LDAP Authenticator |
||
86 | * |
||
87 | * @param array $params Parementers needed to initialize the authenticator |
||
88 | */ |
||
89 | public function __construct($params) |
||
100 | |||
101 | /** |
||
102 | * Return the host string for this authenticator |
||
103 | * |
||
104 | * @param array $params The initial parameters of the authenticator |
||
105 | * |
||
106 | * @return string The host string |
||
107 | * |
||
108 | * @SuppressWarnings("StaticAccess") |
||
109 | */ |
||
110 | private function getHostParam($params) |
||
119 | |||
120 | /** |
||
121 | * Return the required paramter for this authenticator |
||
122 | * |
||
123 | * @param array $params The initial parameters of the authenticator |
||
124 | * @param string $paramName The name of the parameter in the $paramsArray |
||
125 | * @param string $settingsLocation The location in the Settings class |
||
126 | * @param string $settingsName The name in the Settings class |
||
127 | * |
||
128 | * @return mixed The paramter value |
||
129 | * |
||
130 | * @SuppressWarnings("StaticAccess") |
||
131 | */ |
||
132 | private function getParam($params, $paramName, $settingsLocation = '$ldap', $settingsName = false) |
||
145 | |||
146 | /** |
||
147 | * Return an instance to the \LDAP\LDAPServer object instance |
||
148 | * |
||
149 | * @param boolean $bindWrite Should we be able to write to the server? |
||
150 | * |
||
151 | * @return \LDAP\LDAPServer|false The server instance if the binding was successful, otherwise false |
||
152 | * |
||
153 | * @SuppressWarnings("StaticAccess") |
||
154 | */ |
||
155 | public function getAndBindServer($bindWrite = false) |
||
182 | |||
183 | /** |
||
184 | * Log the user in provided the credetials |
||
185 | * |
||
186 | * @param string $username The UID or email address for the user |
||
187 | * @param string $password The password for the user |
||
188 | * |
||
189 | * @return mixed False if the login failed and array otherwise |
||
190 | */ |
||
191 | public function login($username, $password) |
||
214 | |||
215 | /** |
||
216 | * Does this array represent a successful login? |
||
217 | * |
||
218 | * @param array $data The array data stored in the session after a login call |
||
219 | * |
||
220 | * @return boolean True if the user is logged in, false otherwise |
||
221 | */ |
||
222 | public function isLoggedIn($data) |
||
230 | |||
231 | /** |
||
232 | * Obtain the currently logged in user from the session data |
||
233 | * |
||
234 | * @param \stdClass $data The AuthData from the session |
||
235 | * |
||
236 | * @return null|\Auth\LDAPUser The LDAPUser represented by this data |
||
237 | */ |
||
238 | public function getUser($data) |
||
242 | |||
243 | /** |
||
244 | * Obtain the group based on the group name |
||
245 | * |
||
246 | * @param string $name The Group's name |
||
247 | * |
||
248 | * @return null|\Auth\LDAPGroup The LDAPGroup represented by the name or null if not found |
||
249 | */ |
||
250 | public function getGroupByName($name) |
||
259 | |||
260 | public function getGroupsByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false) |
||
289 | |||
290 | public function getActiveUserCount() |
||
299 | |||
300 | /** |
||
301 | * @param array $data The array data to filter and sort |
||
302 | * @param boolean|array $select The fields to return |
||
303 | * @param boolean|integer $top The number of records to return |
||
304 | * @param boolean|integer $skip The number of records to skip |
||
305 | * @param boolean|array $orderby The fields to sort by |
||
306 | */ |
||
307 | private function processFilteringParams(&$data, &$select, $top, $skip, $orderby) |
||
330 | |||
331 | /** |
||
332 | * @param boolean|\Data\Filter $filter The filter to user when reading users |
||
333 | * @param boolean|array $select The fields to return |
||
334 | * @param boolean|integer $top The number of records to return |
||
335 | * @param boolean|integer $skip The number of records to skip |
||
336 | * @param boolean|array $orderby The fields to sort by |
||
337 | * |
||
338 | * @return array|boolean False if no users found, an array of user objects otherwise |
||
339 | */ |
||
340 | public function getUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false) |
||
370 | |||
371 | public function activatePendingUser($user) |
||
410 | |||
411 | public function getUserByResetHash($hash) |
||
420 | } |
||
421 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
422 |
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.