清理PHP,清理不必要的代码

时间:2009-09-07 08:42:16

标签: php refactoring function

我还是某些PHP函数的新手。 有什么方法可以清理下面的代码,因为我知道所有这些都是不必要的,这让我很头疼。 if语句之后的所有内容对于每组代码都是相同的。

    if($class == "2"){if ($posts >= 1){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "3"){if ($posts >= 2){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}


if($class == "4"){if ($posts >= 3){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "5"){if ($posts >= 4){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "6"){if ($posts >= 5){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else {$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());}}


if($class == "7"){if ($posts >= 6){
$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error());
if (!mysql_query($sql)) {die('Error: ' . mysql_error());} echo "";}
else { $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
mysql_query($sql) or die(mysql_error());
$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
mysql_query($insert) or die(mysql_error()); }}}}}}

6 个答案:

答案 0 :(得分:4)

你没有从your previous question学到什么吗?

if ($class >= 2 && $class <= 7) {
    if ($posts >= ($class - 1)) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
}

答案 1 :(得分:3)

哎呀那伤害了我的眼睛。以下是一些提示。

0 - 编写供人阅读的代码。要学习的最重要的一课是,代码是供人类阅读而不是计算机。

1 - 代码风格。注意大括号。作为一般规则,{应该导致{和随附的}之间的所有代码缩进。 4个空格是合理的缩进量。这使得代码的意图更容易确定,因此更容易发现错误。

2 - 验证所有输入。永远不要相信用户提交的任何内容。在您的sql语句中,您有以下内容:

$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";`

问题是$friend变量。我认为这是基于用户输入。用户可以输入像2; DELETE * FROM *这样令人讨厌的东西(好吧,SQL可能不正确,但你明白了)。验证所有输入并始终使用SQL参数。

3 - 使用功能。应该使用函数来减少代码重复。减少代码中的语句数量可以减少出错的可能性。应使用函数来指示代码的意图。例如,如果您有if语句的复杂表达式,则可以将表达式移动到单独的函数中。例如if (isDateInRange($date, $range)) {}

最后,花时间反思您的代码。问问自己'代码的意图是否明确?','有更好的方法吗?'确保您了解代码的每一行。你刚开始时的诱惑就是采取“万岁的态度,它有效!让我们继续'。抵制这种诱惑,从长远来看,它不会很好地为你服务。

答案 2 :(得分:1)

如果它很简单 - if语句之后的所有内容对于每组代码都是相同的 - 你可以简单地创建函数并为每个类调用它。

挖掘这段代码也让我很头疼,所以我没有在整个过程中做到这一点,但看起来这里可以进行一些重大的重构。

乍一看我能说出来 - 你不需要所有这些if陈述,因为它们总是符合模式:

if ($posts >= ($class - 1)) {
    ...
}

答案 3 :(得分:1)

我将从中提取两个函数insert()update()。这是insert()的代码,我为您留下另一个代码:

/**
 * @param  string $table  Table name to insert $values into
 * @param  array  $values Key is field name, value is field value to insert
 * @return null
 * @throws Exception if query fails
 */
function insert($table, $values) {
    $sql = array();

    foreach ($values as $field => $value) {
        $sql[] = "`$field` = '" . mysql_real_escape_string($value) . "'";
    }

    $sql = "INSERT INTO `$table` SET " . implode(', ', $sql);

    if (! mysql_query($q)) {
        throw new Exception(mysql_error());
    }
}

完成此步骤后,使用它,看看还有哪些代码重复,并将其提取到其他一些函数中。

对于update(),您需要WHERE部分的第三个参数。

答案 4 :(得分:0)

<?php

function update_users($friend){

$sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";  

if (!mysql_query($sql)) {  die('Error: ' . mysql_error());    } 

}

function insertproof($friend,$binfo){

$insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";

mysql_query($insert) or die(mysql_error());    

echo "";

}

?> 

认为这可能对你有用。

答案 5 :(得分:-1)

这对眼睛来说有点容易,而且很容易使用,你在那里删除了一些额外的}

我会将一些重复的代码分解为几个函数至少

<?php

if ($class == "2") {
    if ($posts >= 1) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25  WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
        echo "";
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
}

if ($class == "3") {
    if ($posts >= 2) {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
        if (!mysql_query($sql)) {
            die('Error: ' . mysql_error());
        }
        echo "";
    } else {
        $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
        mysql_query($sql) or die(mysql_error());
        $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
        mysql_query($insert) or die(mysql_error());
    }
    if ($class == "4") {
        if ($posts >= 3) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "5") {
        if ($posts >= 4) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "6") {
        if ($posts >= 5) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
    if ($class == "7") {
        if ($posts >= 6) {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1,points=points+25,tpoints=tpoints+25 WHERE `users`.`friendid`='$friend'";
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
            if (!mysql_query($sql)) {
                die('Error: ' . mysql_error());
            }
            echo "";
        } else {
            $sql = "UPDATE users SET posts=posts+1,tposts=tposts+1 WHERE `users`.`friendid` = '$friend'";
            mysql_query($sql) or die(mysql_error());
            $insert = "INSERT INTO proofs ( `ID` ,`friendid` ,`bulletinid`)VALUES('', '$friend' , '$binfo');";
            mysql_query($insert) or die(mysql_error());
        }
    }
}

?>