Conditions | 6 |
Paths | 4 |
Total Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
25 | function LibCaldav_upgrade($version_base, $version_ini) |
||
26 | { |
||
27 | require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
||
28 | require_once $GLOBALS['babInstallPath'].'utilit/functionalityincl.php'; |
||
29 | include_once $GLOBALS['babInstallPath']."utilit/eventincl.php"; |
||
30 | |||
31 | if (!@bab_functionality::includefile('CalendarBackend')) { |
||
|
|||
32 | return false; |
||
33 | } |
||
34 | |||
35 | $addonName = 'LibCaldav'; |
||
36 | $addonInfo = bab_getAddonInfosInstance($addonName); |
||
37 | |||
38 | $addonPhpPath = $addonInfo->getPhpPath(); |
||
39 | |||
40 | $functionalities = new bab_functionalities(); |
||
41 | $functionalities->registerClass('Func_CalendarBackend_Caldav', $addonPhpPath . 'caldav.class.php'); |
||
42 | |||
43 | $addonInfo->removeAllEventListeners(); |
||
44 | $addonInfo->addEventListener('bab_eventBeforePeriodsCreated', 'caldav_onBeforePeriodsCreated', 'eventperiod.php'); |
||
45 | $addonInfo->addEventListener('bab_eventBeforePeriodsCreated', 'caldav_onBeforePeriodsCreated', 'eventperiod.php'); |
||
46 | $addonInfo->addEventListener('bab_eventCollectCalendarsBeforeDisplay', 'caldav_onCollectCalendarsBeforeDisplay', 'eventperiod.php'); |
||
47 | $addonInfo->addEventListener('bab_eventPageRefreshed', 'LibCaldav_onPageRefreshed', 'init.php'); |
||
48 | $addonInfo->addEventListener('bab_eventUserCreated', 'LibCaldav_onUserCreated', 'init.php'); |
||
49 | |||
50 | $tables = new bab_synchronizeSql(); |
||
51 | $tables->fromSqlFile(dirname(__FILE__).'/tables.sql'); |
||
52 | |||
53 | $registry = bab_getRegistryInstance(); |
||
54 | $registry->changeDirectory('/LibCaldav/'); |
||
55 | |||
56 | if ($serverUrl = $registry->getValue('defaultServerUrl')) |
||
57 | { |
||
58 | // move default server from registry to server table |
||
59 | |||
60 | $userCalendarPath = $registry->getValue('defaultUserCalendarPath'); |
||
61 | |||
62 | $url = parse_url($serverUrl); |
||
63 | if (isset($url['host'])) |
||
64 | { |
||
65 | $host = $url['host']; |
||
66 | } else { |
||
67 | $host = 'Serveur'; |
||
68 | } |
||
69 | $useuniqueid = 'false'; |
||
70 | global $babDB; |
||
71 | $babDB->db_query('INSERT INTO libcaldav_servers (name, server_url, user_calendar_path, use_unique_id) |
||
72 | VALUES ('.$babDB->quote($host).', '.$babDB->quote($serverUrl).', '.$babDB->quote($userCalendarPath).', '.$babDB->quote($useuniqueid).')'); |
||
73 | |||
74 | $id_server = (int) $babDB->db_insert_id(); |
||
75 | |||
76 | $registry->removeKey('defaultUserCalendarPath'); |
||
77 | $registry->removeKey('defaultServerUrl'); |
||
78 | |||
79 | // browse users |
||
80 | |||
81 | $registry->changeDirectory('/LibCaldav/Users/'); |
||
82 | $keys = array(); |
||
83 | while ($id_user = $registry->fetchChildDir()) |
||
84 | { |
||
85 | $keys[] = $id_user; |
||
86 | } |
||
87 | |||
88 | foreach($keys as $dirkey) |
||
89 | { |
||
90 | $registry->changeDirectory("/LibCaldav/Users/$dirkey"); |
||
91 | $registry->setKeyValue('server', $id_server); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | return true; |
||
96 | } |
||
97 | |||
180 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: