Conditions | 9 |
Paths | 24 |
Total Lines | 72 |
Code Lines | 43 |
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 |
||
43 | public function checkSlave($id) |
||
44 | { |
||
45 | global $opt; |
||
46 | $nActive = 0; |
||
47 | $nOnline = 0; |
||
48 | $sLogName = ''; |
||
49 | $sLogPos = ''; |
||
50 | $nTimeDiff = - 1; |
||
51 | |||
52 | $slave = $opt['db']['slaves'][$id]; |
||
53 | if ($slave['active'] == true) { |
||
54 | $nActive = 1; |
||
55 | |||
56 | // connect |
||
57 | $dblink = @mysql_connect($slave['server'], $slave['username'], $slave['password']); |
||
58 | if ($dblink !== false) { |
||
59 | if (mysql_select_db($opt['db']['placeholder']['db'], $dblink)) { |
||
60 | // read slave time |
||
61 | $rs = mysql_query('SELECT `data` FROM `sys_repl_timestamp`', $dblink); |
||
62 | if ($rs !== false) { |
||
63 | $rTime = mysql_fetch_assoc($rs); |
||
64 | mysql_free_result($rs); |
||
65 | |||
66 | // read current master db time |
||
67 | $nMasterTime = sql_value("SELECT NOW()", null); |
||
68 | |||
69 | $nTimeDiff = strtotime($nMasterTime) - strtotime($rTime['data']); |
||
70 | if ($nTimeDiff < $opt['db']['slave']['max_behind']) { |
||
71 | $nOnline = 1; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // update logpos |
||
76 | $rs = mysql_query("SHOW SLAVE STATUS"); |
||
77 | $r = mysql_fetch_assoc($rs); |
||
78 | mysql_free_result($rs); |
||
79 | $sLogName = $r['Master_Log_File']; |
||
80 | $sLogPos = $r['Read_Master_Log_Pos']; |
||
81 | } |
||
82 | mysql_close($dblink); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // only-flag changed? |
||
87 | if ($nOnline != sql_value("SELECT `online` FROM `sys_repl_slaves` WHERE `id`='&1'", 0, $id)) { |
||
88 | mail( |
||
89 | $opt['db']['error']['mail'], |
||
90 | "MySQL Slave Server Id " . $id . " (" . $slave['server'] . ") is now " . (($nOnline != 0) ? 'Online' : 'Offline'), |
||
91 | '' |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | sql( |
||
96 | "INSERT INTO `sys_repl_slaves` |
||
97 | (`id`, `server`, `active`, `weight`, `online`, `last_check`, `current_log_name`, `current_log_pos`) |
||
98 | VALUES ('&1', '&2', '&3', '&4', '&5', NOW(), '&6', '&7') |
||
99 | ON DUPLICATE KEY UPDATE `server`='&2', `active`='&3', `weight`='&4', `online`='&5', `last_check`=NOW(), |
||
100 | `current_log_name`='&6', `current_log_pos`='&7'", |
||
101 | $id, |
||
102 | $slave['server'], |
||
103 | $nActive, |
||
104 | $slave['weight'], |
||
105 | $nOnline, |
||
106 | $sLogName, |
||
107 | $sLogPos |
||
108 | ); |
||
109 | |||
110 | // update time_diff? |
||
111 | if ($nTimeDiff != - 1) { |
||
112 | sql("UPDATE `sys_repl_slaves` SET `time_diff`='&1' WHERE `id`='&2'", $nTimeDiff, $id); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.