Conditions | 15 |
Paths | 129 |
Total Lines | 99 |
Code Lines | 62 |
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 |
||
59 | function select_uotd($force_new = false) { |
||
60 | echo gmdate("F d Y", time())." UTC: Starting\n"; |
||
61 | $current_uotd = get_current_uotd(); |
||
62 | if ($current_uotd && !$force_new) { |
||
63 | $assigned = getdate($current_uotd->uotd_time); |
||
64 | $now = getdate(time()); |
||
65 | if ($assigned['mday'] == $now['mday']) { |
||
66 | $user = BoincUser::lookup_id($current_uotd->userid); |
||
67 | echo "Already have UOTD for today\n"; |
||
68 | generate_uotd_gadget($current_uotd, $user); |
||
69 | exit(); |
||
|
|||
70 | } |
||
71 | } |
||
72 | if ($force_new) { |
||
73 | echo "Forcing new UOTD\n"; |
||
74 | } |
||
75 | |||
76 | // get a list of profiles that have been 'approved' for UOTD, |
||
77 | // using a project-specific query if supplied in project.inc |
||
78 | // |
||
79 | if (function_exists('uotd_candidates_query')) { |
||
80 | $query = uotd_candidates_query(); |
||
81 | echo "using project supplied candidates_query\n"; |
||
82 | } else { |
||
83 | $query = default_uotd_candidates_query(); |
||
84 | echo "using default candidates_query\n"; |
||
85 | } |
||
86 | $db = BoincDb::get(); |
||
87 | $result = $db->do_query($query); |
||
88 | |||
89 | // If the number of approved profiles dips below a threshold, |
||
90 | // email the sys admin every time we pick a new one. |
||
91 | // |
||
92 | if (defined('UOTD_ADMIN_EMAIL') |
||
93 | && $result |
||
94 | && $result->num_rows < UOTD_THRESHOLD |
||
95 | ) { |
||
96 | echo "approved candidates for UOTD under UOTD_THRESHOLD\n"; |
||
97 | $u = new BoincUser; |
||
98 | $u->email_addr = UOTD_ADMIN_EMAIL; |
||
99 | $u->name = "UOTD admin"; |
||
100 | send_email($u, |
||
101 | PROJECT . ": User of the Day pool is running low!", |
||
102 | "The pool of approved candidates for User of the Day has". |
||
103 | " reached your assigned threshold: there are now only " . $result->num_rows . " approved users.\n\n". |
||
104 | "To approve more candidates for User of the Day,". |
||
105 | " go to the " . PROJECT . " administration page and click \"Screen user profiles\"" |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | if ($result && $result->num_rows == 0) { |
||
110 | echo "no new verified profile found, selecting old UOTD that was shown least recently\n"; |
||
111 | $result->free(); |
||
112 | // If all verified profiles have been selected as UOTD, |
||
113 | // reshow a random one of the 100 least recently shown profiles. |
||
114 | // |
||
115 | $inner = "SELECT profile.userid FROM profile,user WHERE profile.userid=user.id AND verification=1 AND uotd_time>0 ORDER BY uotd_time ASC LIMIT 100"; |
||
116 | $result = $db->do_query("SELECT * from ($inner) as t ORDER BY RAND() LIMIT 1"); |
||
117 | } |
||
118 | |||
119 | if (!$result || $result->num_rows == 0) { |
||
120 | // No valid users of the day - do something. |
||
121 | echo "No screened users found\n"; |
||
122 | exit(); |
||
123 | } |
||
124 | $candidate = $result->fetch_object(); |
||
125 | $result->free(); |
||
126 | |||
127 | // depending on the candidates query the profile must not exist |
||
128 | // |
||
129 | $profile = BoincProfile::lookup_userid($candidate->userid); |
||
130 | if (!$profile) { |
||
131 | echo "Could not find profile returned from candidates query.\n"; |
||
132 | exit(); |
||
133 | } |
||
134 | |||
135 | // "orphaned" profiles can only be detected if the candidate query doesn't join profile and user table |
||
136 | // if this happens, delete the profile and try again |
||
137 | // |
||
138 | $user = BoincUser::lookup_id($candidate->userid); |
||
139 | if (!$user) { |
||
140 | echo "Profile for user $candidate->userid is orphaned and will be deleted\n"; |
||
141 | $profile->delete(); |
||
142 | select_uotd($force_new); |
||
143 | exit(); |
||
144 | } |
||
145 | |||
146 | $profile->uotd_time = time(); |
||
147 | $profile->update("uotd_time = ".time()); |
||
148 | |||
149 | send_email($user, |
||
150 | "You're the " . PROJECT . " user of the day!", |
||
151 | "Congratulations!\n\nYou've been chosen as the " |
||
152 | . PROJECT . " user of the day! |
||
153 | Your profile will be featured on the " . PROJECT . " website for the next 24 hours." |
||
154 | ); |
||
155 | echo "Chose user $user->id as UOTD\n"; |
||
156 | |||
157 | generate_uotd_gadget($profile, $user); |
||
158 | } |
||
225 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.