| Total Complexity | 85 |
| Total Lines | 634 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 5 | Features | 1 |
Complex classes like GroupModel 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.
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 GroupModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GroupModel extends Model |
||
| 14 | { |
||
| 15 | protected $tableName='group'; |
||
| 16 | protected $table='group'; |
||
| 17 | protected $primaryKey='gid'; |
||
| 18 | const DELETED_AT=null; |
||
| 19 | const UPDATED_AT=null; |
||
| 20 | const CREATED_AT=null; |
||
| 21 | |||
| 22 | /* |
||
| 23 | join_policy: |
||
| 24 | 1:a user can join this group only by invitation |
||
| 25 | 2:a user can join this group only by application |
||
| 26 | 3:a user can join this group by both invitation and application |
||
| 27 | */ |
||
| 28 | public $role=[ |
||
| 29 | "-3"=>"None", |
||
| 30 | "-1"=>"Invited", |
||
| 31 | "0"=>"Pending", |
||
| 32 | "1"=>"Member", |
||
| 33 | "2"=>"Manager", |
||
| 34 | "3"=>"Leader" |
||
| 35 | ]; |
||
| 36 | public $role_color=[ |
||
| 37 | "-3"=>"wemd-black", |
||
| 38 | "-1"=>"wemd-deep-purple", |
||
| 39 | "0"=>"wemd-red", |
||
| 40 | "1"=>"wemd-grey", |
||
| 41 | "2"=>"wemd-light-blue", |
||
| 42 | "3"=>"wemd-amber" |
||
| 43 | ]; |
||
| 44 | |||
| 45 | public function trendingGroups() |
||
| 46 | { |
||
| 47 | return Cache::tags(['group'])->get('trending'); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function gid($gcode) |
||
| 51 | { |
||
| 52 | return DB::table($this->tableName)->where(["gcode"=>$gcode])->first()["gid"]; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function cacheTrendingGroups() |
||
| 56 | { |
||
| 57 | $trending_groups=DB::table($this->tableName)->where(["public"=>1])->orderBy('create_time', 'desc')->select("gid", "gcode", "img", "name", "verified")->get()->all(); |
||
| 58 | foreach ($trending_groups as &$t) { |
||
| 59 | $t["members"]=$this->countGroupMembers($t["gid"]); |
||
| 60 | } |
||
| 61 | usort($trending_groups, function ($a, $b) { |
||
| 62 | return $b["members"]<=>$a["members"]; |
||
| 63 | }); |
||
| 64 | Cache::tags(['group'])->put('trending', array_slice($trending_groups,0,12), 3600*24); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function userGroups($uid) |
||
| 68 | { |
||
| 69 | $user_groups=DB::table("group_member")->join("group", "group_member.gid", "=", "group.gid")->where(["uid"=>$uid])->select("group.gid as gid", "gcode", "img", "name", "verified")->limit(12)->get()->all(); |
||
| 70 | foreach ($user_groups as &$m) { |
||
| 71 | $m["members"]=$this->countGroupMembers($m["gid"]); |
||
| 72 | } |
||
| 73 | return $user_groups; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function countGroupMembers($gid) |
||
| 77 | { |
||
| 78 | return DB::table("group_member")->where(["gid"=>$gid])->count(); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getGroupTags($gid) |
||
| 82 | { |
||
| 83 | return DB::table("group_tag")->where(["gid"=>$gid])->select("tag")->get()->all(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function countGroupContest($gid) |
||
| 87 | { |
||
| 88 | return [ |
||
| 89 | "contest_ahead" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", ">", DB::raw("now()"))->count(), |
||
| 90 | "contest_going" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", "<=", DB::raw("now()"))->where("end_time", ">=", DB::raw("now()"))->count(), |
||
| 91 | "contest_end" => DB::table("contest")->where(["gid"=>$gid])->where("end_time", "<", DB::raw("now()"))->count() |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function changeNickName($gid, $uid, $nickName) |
||
| 99 | ]); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function changeGroupName($gid, $GroupName) |
||
| 103 | { |
||
| 104 | return DB::table("group")->where('gid',$gid)->update([ |
||
| 105 | "name"=>$GroupName |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function changeJoinPolicy($gid, $JoinPolicy){ |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function details($gcode) |
||
| 124 | } |
||
| 125 | |||
| 126 | public function joinPolicy($gid) |
||
| 127 | { |
||
| 128 | $ret=DB::table($this->tableName)->where(["gid"=>$gid])->first(); |
||
| 129 | return empty($ret) ? null : $ret["join_policy"]; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function userProfile($uid, $gid) |
||
| 133 | { |
||
| 134 | $info=DB::table("group_member") |
||
| 135 | ->join('users','users.id','=','group_member.uid') |
||
| 136 | ->where(["gid"=>$gid, "uid"=>$uid]) |
||
| 137 | ->where("role", ">", 0) |
||
| 138 | ->select('avatar','describes','email','gid','uid','name','nick_name','professional_rate','role','sub_group') |
||
| 139 | ->first(); |
||
| 140 | if (!empty($info)) { |
||
| 141 | $info["role_parsed"]=$this->role[$info["role"]]; |
||
| 142 | $info["role_color"]=$this->role_color[$info["role"]]; |
||
| 143 | } |
||
| 144 | return $info; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function userList($gid) |
||
| 148 | { |
||
| 149 | $user_list=DB::table("group_member")->join( |
||
| 150 | "users", |
||
| 151 | "users.id", |
||
| 152 | "=", |
||
| 153 | "group_member.uid" |
||
| 154 | )->where(["gid"=>$gid])->orderBy('role', 'desc')->select( |
||
| 155 | "role", |
||
| 156 | "uid", |
||
| 157 | "name", |
||
| 158 | "nick_name", |
||
| 159 | "avatar", |
||
| 160 | "sub_group", |
||
| 161 | "ranking" |
||
| 162 | )->get()->all(); |
||
| 163 | foreach ($user_list as &$u) { |
||
| 164 | $u["role_parsed"]=$this->role[$u["role"]]; |
||
| 165 | $u["role_color"]=$this->role_color[$u["role"]]; |
||
| 166 | if(is_null($u["sub_group"])) $u["sub_group"]="None"; |
||
| 167 | } |
||
| 168 | return $user_list; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function groupNotice($gid) |
||
| 172 | { |
||
| 173 | $notice_item=DB::table("group_notice")->where(["gid"=>$gid])->first(); |
||
| 174 | if (empty($notice_item)) { |
||
| 175 | return []; |
||
| 176 | } |
||
| 177 | $notice_author=DB::table("users")->where(["id"=>$notice_item["uid"]])->first(); |
||
| 178 | $notice_item["name"]=$notice_author["name"]; |
||
| 179 | $notice_item["avatar"]=$notice_author["avatar"]; |
||
| 180 | $notice_item["post_date_parsed"]=$this->formatPostTime($notice_item["post_date"]); |
||
| 181 | $notice_item["content_parsed"]=clean(convertMarkdownToHtml($notice_item["content"])); |
||
| 182 | return $notice_item; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function judgeClearance($gid, $uid) |
||
| 186 | { |
||
| 187 | $ret=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->first(); |
||
| 188 | return empty($ret) ? -3 : $ret["role"]; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function changeClearance($uid, $gid, $clearance) |
||
| 192 | { |
||
| 193 | return DB::table("group_member")->where([ |
||
| 194 | "uid"=>$uid, |
||
| 195 | "gid"=>$gid |
||
| 196 | ])->update([ |
||
| 197 | "role"=>$clearance |
||
| 198 | ]); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function removeClearance($uid, $gid) |
||
| 202 | { |
||
| 203 | return DB::table("group_member")->where([ |
||
| 204 | "uid"=>$uid, |
||
| 205 | "gid"=>$gid |
||
| 206 | ])->delete(); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function addClearance($uid, $gid, $clearance) |
||
| 210 | { |
||
| 211 | return DB::table("group_member")->insert([ |
||
| 212 | "uid"=>$uid, |
||
| 213 | "gid"=>$gid, |
||
| 214 | "role"=>$clearance, |
||
| 215 | "join_time"=>date("Y-m-d H:i:s") |
||
| 216 | ]); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function isMember($gid, $uid) |
||
| 220 | { |
||
| 221 | return DB::table("group_member")->where([ |
||
| 222 | "gid"=> $gid, |
||
| 223 | "uid"=> $uid |
||
| 224 | ])->where("role", ">", 0)->count(); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function problemTags($gid,$pid = -1) |
||
| 251 | } |
||
| 252 | |||
| 253 | public function problems($gid) |
||
| 254 | { |
||
| 255 | $contestModel = new ContestModel(); |
||
| 256 | $problems = DB::table('contest_problem') |
||
| 257 | ->join('contest','contest_problem.cid', '=', 'contest.cid') |
||
| 258 | ->join('problem','contest_problem.pid', '=', 'problem.pid' ) |
||
| 259 | ->select('contest_problem.cid as cid', 'problem.pid as pid', 'pcode', 'title') |
||
| 260 | ->where('contest.gid',$gid) |
||
| 261 | ->where('contest.practice',1) |
||
| 262 | ->orderBy('contest.create_time','desc') |
||
| 263 | ->distinct() |
||
| 264 | ->get()->all(); |
||
| 265 | $user_id = Auth::user()->id; |
||
| 266 | foreach($problems as $key => $value){ |
||
| 267 | if($contestModel->judgeClearance($value['cid'],$user_id) != 3){ |
||
| 268 | unset($problems[$key]); |
||
| 269 | }else{ |
||
| 270 | $problems[$key]['tags'] = $this->problemTags($gid,$value['pid']); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | return $problems; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function problemAddTag($gid,$pid,$tag) |
||
| 277 | { |
||
| 278 | return DB::table("group_problem_tag")->insert([ |
||
| 279 | "gid"=>$gid, |
||
| 280 | "pid"=>$pid, |
||
| 281 | "tag"=>$tag, |
||
| 282 | ]); |
||
| 283 | } |
||
| 284 | |||
| 285 | public function problemRemoveTag($gid,$pid,$tag) |
||
| 292 | } |
||
| 293 | |||
| 294 | public function formatPostTime($date) |
||
| 295 | { |
||
| 296 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
| 297 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
| 298 | |||
| 299 | $now=time(); |
||
| 300 | $unix_date=strtotime($date); |
||
| 301 | |||
| 302 | if (empty($unix_date)) { |
||
| 303 | return "Bad date"; |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($now>$unix_date) { |
||
| 307 | $difference=$now-$unix_date; |
||
| 308 | $tense="ago"; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function judgeEmailClearance($gid, $email) |
||
| 328 | { |
||
| 329 | $user=DB::table("users")->where(["email"=>$email])->first(); |
||
| 330 | if(empty($user)) return -4; |
||
| 331 | $ret=DB::table("group_member")->where([ |
||
| 332 | "gid"=>$gid, |
||
| 333 | "uid"=>$user["id"], |
||
| 334 | ])->first(); |
||
| 335 | return empty($ret) ? -3 : $ret["role"]; |
||
| 336 | } |
||
| 337 | |||
| 338 | public function inviteMember($gid, $email) |
||
| 346 | ]); |
||
| 347 | } |
||
| 348 | |||
| 349 | public function changeGroup($uid, $gid, $sub) |
||
| 350 | { |
||
| 351 | return DB::table("group_member")->where([ |
||
| 352 | "uid"=>$uid, |
||
| 353 | "gid"=>$gid |
||
| 354 | ])->update([ |
||
| 355 | "sub_group"=>$sub |
||
| 356 | ]); |
||
| 357 | } |
||
| 358 | |||
| 359 | public function isUser($email) |
||
| 364 | } |
||
| 365 | |||
| 366 | public function isGroup($gcode) |
||
| 371 | } |
||
| 372 | |||
| 373 | public function createGroup($uid, $gcode, $img, $name, $public, $description, $join_policy) |
||
| 374 | { |
||
| 375 | $gid=DB::table("group")->insertGetId([ |
||
| 376 | "gcode"=>$gcode, |
||
| 377 | "img"=>$img, |
||
| 378 | "name"=>$name, |
||
| 379 | "public"=>$public, |
||
| 380 | "verified"=>0, |
||
| 381 | "description"=>$description, |
||
| 382 | "join_policy"=>$join_policy, |
||
| 383 | "custom_icon"=>null, |
||
| 384 | "custom_title"=>null, |
||
| 385 | "create_time"=>date("Y-m-d H:i:s") |
||
| 386 | ]); |
||
| 387 | return DB::table("group_member")->insert([ |
||
| 388 | "uid"=>$uid, |
||
| 389 | "gid"=>$gid, |
||
| 390 | "role"=>3, |
||
| 391 | "join_time"=>date("Y-m-d H:i:s") |
||
| 392 | ]); |
||
| 393 | } |
||
| 394 | |||
| 395 | public function detailNotice($gcode) |
||
| 396 | { |
||
| 397 | $group=DB::table("group")->where([ |
||
| 398 | "gcode"=>$gcode, |
||
| 399 | ])->first(); |
||
| 400 | return $group_notice=DB::table("group_notice")->where([ |
||
| 401 | "gid"=>$group["gid"], |
||
| 402 | ])->first(); |
||
| 403 | } |
||
| 404 | |||
| 405 | public function createNotice($gid, $uid, $title, $content) |
||
| 416 | ]); |
||
| 417 | } |
||
| 418 | |||
| 419 | public function groupMemberPracticeContestStat($gid) |
||
| 504 | } |
||
| 505 | |||
| 506 | public function groupMemberPracticeTagStat($gid) |
||
| 565 | } |
||
| 566 | |||
| 567 | public function refreshAllElo() |
||
| 578 | } |
||
| 579 | |||
| 580 | public function refreshElo($gid) |
||
| 581 | { |
||
| 582 | DB::table('group_rated_change_log') |
||
| 583 | ->where('gid',$gid) |
||
| 584 | ->delete(); |
||
| 585 | DB::table('group_member') |
||
| 586 | ->where('gid',$gid) |
||
| 587 | ->update([ |
||
| 588 | 'ranking' => 1500 |
||
| 589 | ]); |
||
| 590 | $contests = DB::table('contest') |
||
| 591 | ->where([ |
||
| 592 | 'gid' => $gid, |
||
| 593 | 'practice' => 1 |
||
| 594 | ]) |
||
| 595 | ->where('end_time','<',date("Y-m-d H:i:s")) |
||
| 596 | ->select('cid','name') |
||
| 597 | ->orderBy('end_time') |
||
| 598 | ->get()->all(); |
||
| 599 | |||
| 600 | if(empty($contests)) { |
||
| 601 | return []; |
||
| 602 | } |
||
| 603 | $result = []; |
||
| 604 | $contestModel = new \App\Models\ContestModel(); |
||
| 605 | foreach ($contests as $contest) { |
||
| 606 | $judge_status = $contestModel->judgeOver($contest['cid']); |
||
| 607 | if($judge_status['result'] == true){ |
||
| 608 | $calc = new GroupRatingCalculator($contest['cid']); |
||
| 609 | $calc->calculate(); |
||
| 610 | $calc->storage(); |
||
| 611 | $result[] = [ |
||
| 612 | 'ret' => 'success', |
||
| 613 | 'cid' => $contest['cid'], |
||
| 614 | 'name' => $contest['name'] |
||
| 615 | ]; |
||
| 616 | }else{ |
||
| 617 | $result[] = [ |
||
| 618 | 'ret' => 'judging', |
||
| 619 | 'cid' => $contest['cid'], |
||
| 620 | 'name' => $contest['name'], |
||
| 621 | 'submissions' => $judge_status['sid'] |
||
| 622 | ]; |
||
| 623 | } |
||
| 624 | } |
||
| 625 | |||
| 626 | return $result; |
||
| 627 | } |
||
| 628 | |||
| 629 | public function getEloChangeLog($gid,$uid) |
||
| 647 | } |
||
| 648 | } |
||
| 649 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: