Looking back to 5 years ago, writing my first 20.000 lines of PHP codes, these are some of the worst codes, I have ever written in my personal projects. LOL. Have fun finding the errors.
TIPS: check for indentation error, redundancy, too complex solution, inefficient sql queries, unneccessary logic blocks (if and switch), unhelpful variable and function names. 🙂 I skipped a few errors, can you spot them?
<?php /** * Errors in this function: * 1. in the while loop, the * speed has drastically been reduced to one iteration * per second because the value of time() only changes once every second. * * This function would however still work in a short time, if ALL files * in the folder are generated by this function, since they will always * be unique, thanks to time(); * * 2. For concatenating the strings. implode() would have been faster instead of the for loop * like this * $art=explode(".", "$file_name"); * $time=time()-1491262093; * $art[0]=$art[0]."_copy".$time; * $rname = implode(".", $art); */ function dmmp(){ //....truncated function if (file_exists($saveto)){ while (file_exists($saveto)) { $art=explode(".", "$file_name"); $time=time()-1491262093; $rname=$art[0]."_copy".$time; for ($i=0; $i <count($art) ; $i++) { if($i==0){ $rname=$art[0]."_copy".$time; } else{ $rname=$rname.".".$art[$i]; } } $rty++; // $saveto = loc()."$store_at/$rty/$rname"; } } //....truncated function } /*** * the breaks in the switch after each image/(*) strings are redundant * Solution * case "image/gif": * case "image/jpeg": * case "image/jpeg": * case "image/png": * return true; * default: * return false; * Another shorter solution would have been * return in_array($file_type, ["image/gif", "image/jpeg", "image/jpeg", "image/png"]); * since there are only 4 strings to check */ function is_image($file_type){ $typeok = TRUE; switch($file_type) { case "image/gif": break; case "image/jpeg": // Both regular and progressive jpegs case "image/jpeg": break; case "image/png": break; default: $typeok = FALSE; break; } return $typeok; } /** * this function tries to get a user friendly date string * from time() in seconds e.g 1592523448 * this is unneccesarily complex and long * shorter solution * return (intval($time) > 0)? date('d-m-Y at H:i:s', intval($time)):0; */ function formatThisForHr($time){ if($time>0){ $time_array=getdate($time); return $time_array['mday']."-".$time_array['mon']."-".$time_array['year']." at ".$time_array['hours'].":".$time_array['minutes'].":".$time_array['seconds']; }else{ return 0; } } /*for the if block * a shorter solution would have been *if ($ert[$i]=="." || if ($ert[$i]==".."){ * continue; * }else{ * ... * } */ function getFiles($link){ $user=getUsername_(); $query="SELECT * FROM assign_teach WHERE ass_code='$link' AND username='$user'"; $num=querynum($query); if ($num>0){ $rt=$this->loc().$link; if(!is_dir($rt)){ @mkdir($rt); } $ert=scandir($rt); $ee=count($ert); $svdh=array(); $row=array(); $res=queryMysql($query); $arr=$res->fetch_array(MYSQLI_ASSOC); array_push($row, $arr); for($i=0; $i<$ee; $i++){ if ($ert[$i]==".") { continue; }else if ($ert[$i]==".."){ continue; }else{ $gdygfg=$rt."/".$ert[$i]; $file_about=file_about($gdygfg); //put file content _here array_push($svdh, $file_about); } } $e=count($svdh); $rowl=array("total"=>$e); array_push($row, $svdh); array_push($row, $rowl); return $row; } return error(6476); } /** of course SQL. * This function deactivates all * assignments older than now * * shorter solution: * * $time=time(); * $query="UPDATE assign_teach SET deactivated_=1, deactivated_time=$time WHERE deactivated_=0 AND deactivated_time < $time"; * queryMysql($query); */ function deactivateAll_(){ $qr="SELECT * FROM assign_teach WHERE deactivated_=0"; $rows=fetch_assoc($qr); $time=time(); for ($i=0; $i<count($rows); $i++){ $id=$rows[$i]['id']; $deact=$rows[$i]['deactivation_time']; if ($time>$deact){ $query="UPDATE assign_teach SET deactivated_=1, deactivated_time=$time WHERE id=$id"; queryMysql($query); } } } /** * check if an API ID is correct. LMAO * a tenary would have made this look better and shorter * an even better solution is * return (getAppId_()==appidee); **/ function verifyAppid(){ $id=getAppId_(); if ($id==appidee){ return true; } return false; } /** * Ensures an ip address is both a valid IP and does not fall within * a private network range. * * shorter solution * return !(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false); */ function validate_ip($ip){ if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) { return false; } return true; } ?>
Leave a Reply