Complex classes like auth_plugin_authplain 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 auth_plugin_authplain, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class auth_plugin_authplain extends DokuWiki_Auth_Plugin |
||
12 | { |
||
13 | /** @var array user cache */ |
||
14 | protected $users = null; |
||
15 | |||
16 | /** @var array filter pattern */ |
||
17 | protected $pattern = array(); |
||
18 | |||
19 | /** @var bool safe version of preg_split */ |
||
20 | protected $pregsplit_safe = false; |
||
21 | |||
22 | /** |
||
23 | * Constructor |
||
24 | * |
||
25 | * Carry out sanity checks to ensure the object is |
||
26 | * able to operate. Set capabilities. |
||
27 | * |
||
28 | * @author Christopher Smith <[email protected]> |
||
29 | */ |
||
30 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * Check user+password |
||
56 | * |
||
57 | * Checks if the given user exists and the given |
||
58 | * plaintext password is correct |
||
59 | * |
||
60 | * @author Andreas Gohr <[email protected]> |
||
61 | * @param string $user |
||
62 | * @param string $pass |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function checkPass($user, $pass) |
||
72 | |||
73 | /** |
||
74 | * Return user info |
||
75 | * |
||
76 | * Returns info about the given user needs to contain |
||
77 | * at least these fields: |
||
78 | * |
||
79 | * name string full name of the user |
||
80 | * mail string email addres of the user |
||
81 | * grps array list of groups the user is in |
||
82 | * |
||
83 | * @author Andreas Gohr <[email protected]> |
||
84 | * @param string $user |
||
85 | * @param bool $requireGroups (optional) ignored by this plugin, grps info always supplied |
||
86 | * @return array|false |
||
87 | */ |
||
88 | public function getUserData($user, $requireGroups = true) |
||
93 | |||
94 | /** |
||
95 | * Creates a string suitable for saving as a line |
||
96 | * in the file database |
||
97 | * (delimiters escaped, etc.) |
||
98 | * |
||
99 | * @param string $user |
||
100 | * @param string $pass |
||
101 | * @param string $name |
||
102 | * @param string $mail |
||
103 | * @param array $grps list of groups the user is in |
||
104 | * @return string |
||
105 | */ |
||
106 | protected function createUserLine($user, $pass, $name, $mail, $grps) |
||
115 | |||
116 | /** |
||
117 | * Create a new User |
||
118 | * |
||
119 | * Returns false if the user already exists, null when an error |
||
120 | * occurred and true if everything went well. |
||
121 | * |
||
122 | * The new user will be added to the default group by this |
||
123 | * function if grps are not specified (default behaviour). |
||
124 | * |
||
125 | * @author Andreas Gohr <[email protected]> |
||
126 | * @author Chris Smith <[email protected]> |
||
127 | * |
||
128 | * @param string $user |
||
129 | * @param string $pwd |
||
130 | * @param string $name |
||
131 | * @param string $mail |
||
132 | * @param array $grps |
||
133 | * @return bool|null|string |
||
134 | */ |
||
135 | public function createUser($user, $pwd, $name, $mail, $grps = null) |
||
162 | |||
163 | /** |
||
164 | * Modify user data |
||
165 | * |
||
166 | * @author Chris Smith <[email protected]> |
||
167 | * @param string $user nick of the user to be changed |
||
168 | * @param array $changes array of field/value pairs to be changed (password will be clear text) |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function modifyUser($user, $changes) |
||
219 | |||
220 | /** |
||
221 | * Remove one or more users from the list of registered users |
||
222 | * |
||
223 | * @author Christopher Smith <[email protected]> |
||
224 | * @param array $users array of users to be deleted |
||
225 | * @return int the number of users deleted |
||
226 | */ |
||
227 | public function deleteUsers($users) |
||
259 | |||
260 | /** |
||
261 | * Return a count of the number of user which meet $filter criteria |
||
262 | * |
||
263 | * @author Chris Smith <[email protected]> |
||
264 | * |
||
265 | * @param array $filter |
||
266 | * @return int |
||
267 | */ |
||
268 | public function getUserCount($filter = array()) |
||
284 | |||
285 | /** |
||
286 | * Bulk retrieval of user data |
||
287 | * |
||
288 | * @author Chris Smith <[email protected]> |
||
289 | * |
||
290 | * @param int $start index of first user to be returned |
||
291 | * @param int $limit max number of users to be returned |
||
292 | * @param array $filter array of field/pattern pairs |
||
293 | * @return array userinfo (refer getUserData for internal userinfo details) |
||
294 | */ |
||
295 | public function retrieveUsers($start = 0, $limit = 0, $filter = array()) |
||
320 | |||
321 | /** |
||
322 | * Only valid pageid's (no namespaces) for usernames |
||
323 | * |
||
324 | * @param string $user |
||
325 | * @return string |
||
326 | */ |
||
327 | public function cleanUser($user) |
||
332 | |||
333 | /** |
||
334 | * Only valid pageid's (no namespaces) for groupnames |
||
335 | * |
||
336 | * @param string $group |
||
337 | * @return string |
||
338 | */ |
||
339 | public function cleanGroup($group) |
||
344 | |||
345 | /** |
||
346 | * Load all user data |
||
347 | * |
||
348 | * loads the user file into a datastructure |
||
349 | * |
||
350 | * @author Andreas Gohr <[email protected]> |
||
351 | */ |
||
352 | protected function loadUserData() |
||
367 | |||
368 | /** |
||
369 | * Read user data from given file |
||
370 | * |
||
371 | * ignores non existing files |
||
372 | * |
||
373 | * @param string $file the file to load data from |
||
374 | * @return array |
||
375 | */ |
||
376 | protected function readUserFile($file) |
||
400 | |||
401 | /** |
||
402 | * Get the user line split into it's parts |
||
403 | * |
||
404 | * @param string $line |
||
405 | * @return string[] |
||
406 | */ |
||
407 | protected function splitUserData($line) |
||
434 | |||
435 | /** |
||
436 | * return true if $user + $info match $filter criteria, false otherwise |
||
437 | * |
||
438 | * @author Chris Smith <[email protected]> |
||
439 | * |
||
440 | * @param string $user User login |
||
441 | * @param array $info User's userinfo array |
||
442 | * @return bool |
||
443 | */ |
||
444 | protected function filter($user, $info) |
||
457 | |||
458 | /** |
||
459 | * construct a filter pattern |
||
460 | * |
||
461 | * @param array $filter |
||
462 | */ |
||
463 | protected function constructPattern($filter) |
||
470 | } |
||
471 |
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.