| Conditions | 13 |
| Paths | 82 |
| Total Lines | 98 |
| 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 |
||
| 25 | function DbWrite($db, $tbl, $data, $pkey) |
||
| 26 | {
|
||
| 27 | //对要写入数据库的内容进行处理,主要是根据字段类型添加引号 |
||
| 28 | //:TODO:改用缓存机制 |
||
| 29 | $col = $db->MetaColumns($tbl); |
||
| 30 | foreach ($data as $key => $val) |
||
| 31 | {
|
||
| 32 | if (isset($col[strtoupper($key)])) |
||
| 33 | {
|
||
| 34 | //根据字段类型添加引号 |
||
| 35 | if (!in_array($col[strtoupper($key)]->type, array('int', 'integer', 'tinyint', 'decimal', 'bolean', 'numeric')))
|
||
| 36 | $data[$key] = "'" . addslashes($val) . "'"; |
||
| 37 | } |
||
| 38 | else |
||
| 39 | {
|
||
| 40 | return("Column $key is not found in db schema.");
|
||
| 41 | } |
||
| 42 | } |
||
| 43 | //检查要写入的数据是否存在 insert or update? |
||
| 44 | $sql = "select count(1) as c from $tbl where 1=1 "; |
||
| 45 | if (is_array($pkey)) |
||
| 46 | {
|
||
| 47 | //多个值的定位 |
||
| 48 | //$s_pkey will be used again when actually write to db |
||
| 49 | $s_pkey = ''; |
||
| 50 | foreach ($pkey as $key) |
||
| 51 | {
|
||
| 52 | //检查键值是否被指定,如果没有被指定,中止操作 |
||
| 53 | if (isset($data[$key])) |
||
| 54 | $s_pkey .= " and $key = $data[$key] "; |
||
| 55 | else |
||
| 56 | {
|
||
| 57 | return("Key $key has not assigned a value.");
|
||
| 58 | } |
||
| 59 | } |
||
| 60 | $sql .= $s_pkey; |
||
| 61 | } |
||
| 62 | else |
||
| 63 | {
|
||
| 64 | //单个键值 |
||
| 65 | $s_pkey = " and $pkey = $data[$pkey] "; |
||
| 66 | $sql .= $s_pkey; |
||
| 67 | } |
||
| 68 | $rs = $db->Execute($sql); |
||
| 69 | $i = $rs->fields['c']; |
||
| 70 | if (0 == $i) |
||
| 71 | {
|
||
| 72 | //its insert |
||
| 73 | $sql = "insert into $tbl (";
|
||
| 74 | //keys |
||
| 75 | foreach ($data as $key=>$val) |
||
| 76 | {
|
||
| 77 | $sql .= "$key, "; |
||
| 78 | } |
||
| 79 | $sql = substr($sql, 0, strlen($sql) - 2); |
||
| 80 | $sql .= ") values (";
|
||
| 81 | //values |
||
| 82 | foreach ($data as $key=>$val) |
||
| 83 | {
|
||
| 84 | $sql .= "$val, "; |
||
| 85 | } |
||
| 86 | $sql = substr($sql, 0, strlen($sql) - 2); |
||
| 87 | $sql .= ")"; |
||
| 88 | } |
||
| 89 | elseif (1 == $i) |
||
| 90 | {
|
||
| 91 | //its update |
||
| 92 | $sql = "update $tbl set "; |
||
| 93 | foreach ($data as $key=>$val) |
||
| 94 | {
|
||
| 95 | $sql .= "$key = $val, "; |
||
| 96 | } |
||
| 97 | $sql = substr($sql, 0, strlen($sql) - 2); |
||
| 98 | $sql .= " where 1=1 "; |
||
| 99 | //use pkey to locate data |
||
| 100 | $sql .= $s_pkey; |
||
| 101 | } |
||
| 102 | else |
||
| 103 | {
|
||
| 104 | //got too many match rows |
||
| 105 | return("Got >1 rows by given pkey, which to update ?");
|
||
| 106 | } |
||
| 107 | //finally, write to database |
||
| 108 | //$db->debug = true; |
||
| 109 | $rs = $db->Execute($sql); |
||
|
|
|||
| 110 | $i = $db->ErrorNo(); |
||
| 111 | if (0 == $i) |
||
| 112 | {
|
||
| 113 | //no error |
||
| 114 | //echo("1 row writed to database, no error.<br />\n");
|
||
| 115 | return 1; |
||
| 116 | } |
||
| 117 | else |
||
| 118 | {
|
||
| 119 | //error occur |
||
| 120 | return($db->ErrorMsg()); |
||
| 121 | } |
||
| 122 | } // end of function DbWrite |
||
| 123 | |||
| 125 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.