| Conditions | 19 |
| Paths | 6480 |
| Total Lines | 128 |
| Code Lines | 78 |
| 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 |
||
| 29 | function forum_page($forum, $user, $msg=null) { |
||
| 30 | global $forum_sort_styles; |
||
| 31 | |||
| 32 | if (DISABLE_FORUMS) { |
||
| 33 | if (!$user || !is_admin($user)) { |
||
| 34 | error_page("Forums are disabled"); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | $sort_style = get_int("sort", true); |
||
| 38 | $start = get_int("start", true); |
||
| 39 | if (!$start) $start = 0; |
||
| 40 | |||
| 41 | $subs = []; |
||
| 42 | if ($user) { |
||
| 43 | BoincForumPrefs::lookup($user); |
||
| 44 | $subs = BoincSubscription::enum("userid=$user->id"); |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!is_forum_visible_to_user($forum, $user)) { |
||
| 48 | if ($user) { |
||
| 49 | remove_subscriptions_forum($user->id, $forum->id); |
||
| 50 | } |
||
| 51 | error_page(tra("Not visible to you")); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (!$sort_style) { |
||
| 55 | // get the sort style either from the logged in user or a cookie |
||
| 56 | if ($user){ |
||
| 57 | $sort_style = $user->prefs->forum_sorting; |
||
| 58 | } else { |
||
| 59 | list($sort_style, $thread_style) = parse_forum_cookie(); |
||
| 60 | } |
||
| 61 | } else { |
||
| 62 | // set the sort style |
||
| 63 | if ($user){ |
||
| 64 | $user->prefs->forum_sorting = $sort_style; |
||
| 65 | $user->prefs->update("forum_sorting=$sort_style"); |
||
| 66 | } else { |
||
| 67 | list($old_style, $thread_style) = parse_forum_cookie(); |
||
| 68 | send_cookie( |
||
| 69 | 'sorting', implode("|", array($sort_style, $thread_style)), true |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | switch ($forum->parent_type) { |
||
| 75 | case 0: |
||
| 76 | $category = BoincCategory::lookup_id($forum->category); |
||
| 77 | page_head(sprintf("%s '%s'", tra("Forum"), $forum->title)); |
||
| 78 | if ($msg) echo "<p>$msg</p>\n"; |
||
| 79 | show_forum_header($user); |
||
| 80 | echo forum_title($category, $forum, NULL); |
||
| 81 | break; |
||
| 82 | case 1: |
||
| 83 | $team = BoincTeam::lookup_id($forum->category); |
||
| 84 | page_head(tra("Team message board for %1", $team->name)); |
||
| 85 | if ($msg) echo "<p>$msg</p>\n"; |
||
| 86 | show_forum_header($user); |
||
| 87 | echo team_forum_title($forum); |
||
| 88 | break; |
||
| 89 | } |
||
| 90 | |||
| 91 | echo ' |
||
| 92 | <p> |
||
| 93 | <form action="forum_forum.php" method="get" class="form-inline"> |
||
| 94 | <input type="hidden" name="id" value="'.$forum->id.'"> |
||
| 95 | <table width="100%" cellspacing="0" cellpadding="0"> |
||
| 96 | <tr valign="top"> |
||
| 97 | <td colspan=2> |
||
| 98 | '; |
||
| 99 | |||
| 100 | switch (user_can_create_thread($user, $forum)) { |
||
| 101 | case 'yes': |
||
| 102 | show_button( |
||
| 103 | "forum_post.php?id=$forum->id", tra("New thread"), |
||
| 104 | tra("Add a new thread to this forum") |
||
| 105 | ); |
||
| 106 | break; |
||
| 107 | case 'login': |
||
| 108 | echo "To add a thread, you must <a href=login_form.php>log in</a>."; |
||
| 109 | break; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($user) { |
||
| 113 | if (is_subscribed(-$forum->id, $subs)) { |
||
| 114 | BoincNotify::delete_aux(sprintf( |
||
| 115 | 'userid=%d and type=%d and opaque=%d', |
||
| 116 | $user->id, |
||
| 117 | NOTIFY_SUBSCRIBED_FORUM, |
||
| 118 | $forum->id |
||
| 119 | )); |
||
| 120 | show_button_small( |
||
| 121 | "forum_forum.php?id=$forum->id&action=unsubscribe", |
||
| 122 | 'Unsubscribe', |
||
| 123 | 'Unsubscribe from this forum' |
||
| 124 | ); |
||
| 125 | } else { |
||
| 126 | show_button_small( |
||
| 127 | "forum_forum.php?id=$forum->id&action=subscribe", |
||
| 128 | 'Subscribe', |
||
| 129 | 'Click to get notified when there are new threads in this forum' |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | echo '</td> |
||
| 135 | <td valign=top align="right"> |
||
| 136 | <div class="form-group"> |
||
| 137 | '; |
||
| 138 | echo select_from_array("sort", $forum_sort_styles, $sort_style); |
||
| 139 | echo sprintf(' |
||
| 140 | <input class="btn btn-sm" %s type="submit" value="Sort"> |
||
| 141 | </div> |
||
| 142 | </td> |
||
| 143 | </tr> |
||
| 144 | </table> |
||
| 145 | </form> |
||
| 146 | <p></p> ', |
||
| 147 | button_style() |
||
| 148 | ); |
||
| 149 | |||
| 150 | show_forum_threads($forum, $start, $sort_style, $user, $subs); |
||
| 151 | |||
| 152 | echo " |
||
| 153 | <p>". |
||
| 154 | tra("This message board is available as an %1 RSS feed %2", "<a href=forum_rss.php?forumid=$forum->id&setup=1>", "<img src=img/feed_logo.png></a>"); |
||
| 155 | |||
| 156 | page_tail(); |
||
| 157 | } |
||
| 287 |