Conditions | 7 |
Paths | 18 |
Total Lines | 51 |
Code Lines | 36 |
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 |
||
69 | public function write($uid, $uname, $time, $module, $ip) |
||
70 | { |
||
71 | $uid = (int) $uid; |
||
72 | $uname = $this->db->quote($uname); |
||
73 | $time = (int) $time; |
||
74 | $module = (int) $module; |
||
75 | $ip = $this->db->quote($ip); |
||
76 | |||
77 | if ($uid > 0) { |
||
78 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('online') . " WHERE online_uid={$uid}"; |
||
79 | } else { |
||
80 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('online') |
||
81 | . " WHERE online_uid={$uid} AND online_ip={$ip}"; |
||
82 | } |
||
83 | $result = $this->db->queryF($sql); |
||
84 | if (!$this->db->isResultSet($result)) { |
||
85 | throw new \RuntimeException( |
||
86 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | list($count) = $this->db->fetchRow($result); |
||
91 | if ($count > 0) { |
||
92 | $sql = 'UPDATE ' . $this->db->prefix('online') |
||
93 | . " SET online_updated = {$time}, online_module = {$module} WHERE online_uid = {$uid}"; |
||
94 | if ($uid === 0) { |
||
95 | $sql .= " AND online_ip={$ip}"; |
||
96 | } |
||
97 | } else { |
||
98 | if ($uid != 0) { |
||
99 | // this condition (no entry for a real user) exists when a user first signs in |
||
100 | // first, cleanup the uid == 0 row the user generated before signing in |
||
101 | $loginSql = sprintf('DELETE FROM %s WHERE online_uid = 0 AND online_ip=%s', $this->db->prefix('online'), $ip); |
||
102 | $this->db->queryF($loginSql); |
||
103 | } |
||
104 | $sql = sprintf( |
||
105 | 'INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_module)' |
||
106 | . ' VALUES (%u, %s, %u, %s, %u)', |
||
107 | $this->db->prefix('online'), |
||
108 | $uid, |
||
109 | $uname, |
||
110 | $time, |
||
111 | $ip, |
||
112 | $module |
||
113 | ); |
||
114 | } |
||
115 | if (!$this->db->queryF($sql)) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | return true; |
||
120 | } |
||
206 |