Conditions | 83 |
Paths | > 20000 |
Total Lines | 208 |
Code Lines | 158 |
Lines | 61 |
Ratio | 29.33 % |
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 |
||
32 | public function __construct($enc) |
||
33 | { |
||
34 | parent::__construct(__CLASS__, $enc); |
||
35 | switch (strtolower(PSI_PLUGIN_BAT_ACCESS)) { |
||
36 | case 'command': |
||
37 | if (PSI_OS == 'WINNT') { |
||
38 | $_cim = null; //root\CIMv2 |
||
39 | $_wmi = null; //root\WMI |
||
40 | // don't set this params for local connection, it will not work |
||
41 | $strHostname = ''; |
||
42 | $strUser = ''; |
||
43 | $strPassword = ''; |
||
44 | try { |
||
45 | // initialize the wmi object |
||
46 | $objLocatorCIM = new COM('WbemScripting.SWbemLocator'); |
||
47 | if ($strHostname == "") { |
||
48 | $_cim = $objLocatorCIM->ConnectServer(); |
||
49 | |||
50 | } else { |
||
51 | $_cim = $objLocatorCIM->ConnectServer($strHostname, 'root\CIMv2', $strHostname.'\\'.$strUser, $strPassword); |
||
52 | } |
||
53 | |||
54 | // initialize the wmi object |
||
55 | $objLocatorWMI = new COM('WbemScripting.SWbemLocator'); |
||
56 | if ($strHostname == "") { |
||
57 | $_wmi = $objLocatorWMI->ConnectServer($strHostname, 'root\WMI'); |
||
58 | |||
59 | } else { |
||
60 | $_wmi = $objLocatorWMI->ConnectServer($strHostname, 'root\WMI', $strHostname.'\\'.$strUser, $strPassword); |
||
61 | } |
||
62 | |||
63 | } catch (Exception $e) { |
||
64 | $this->global_error->addError("WMI connect error", "PhpSysInfo can not connect to the WMI interface for security reasons.\nCheck an authentication mechanism for the directory where phpSysInfo is installed."); |
||
65 | } |
||
66 | $buffer_info = ''; |
||
67 | $buffer_state = ''; |
||
68 | $bufferWB = CommonFunctions::getWMI($_cim, 'Win32_Battery', array('EstimatedChargeRemaining', 'DesignVoltage', 'BatteryStatus', 'Chemistry')); |
||
69 | if (sizeof($bufferWB)>0) { |
||
70 | $capacity = ''; |
||
71 | if (isset($bufferWB[0]['EstimatedChargeRemaining'])) { |
||
72 | $capacity = $bufferWB[0]['EstimatedChargeRemaining']; |
||
73 | } |
||
74 | if (isset($bufferWB[0]['BatteryStatus'])) { |
||
75 | switch ($bufferWB[0]['BatteryStatus']) { |
||
76 | case 1: $batstat = 'Discharging'; break; |
||
77 | case 2: $batstat = 'AC connected'; break; |
||
78 | case 3: $batstat = 'Fully Charged'; break; |
||
79 | case 4: $batstat = 'Low'; break; |
||
80 | case 5: $batstat = 'Critical'; break; |
||
81 | case 6: $batstat = 'Charging'; break; |
||
82 | case 7: $batstat = 'Charging and High'; break; |
||
83 | case 8: $batstat = 'Charging and Low'; break; |
||
84 | case 9: $batstat = 'Charging and Critical'; break; |
||
85 | case 10: $batstat = 'Undefined'; break; |
||
86 | case 11: $batstat = 'Partially Charged'; break; |
||
87 | default: $batstat = ''; |
||
88 | } |
||
89 | if ($batstat != '') $buffer_state .= 'POWER_SUPPLY_STATUS='.$batstat."\n"; |
||
90 | } |
||
91 | $techn = ''; |
||
92 | if (isset($bufferWB[0]['Chemistry'])) { |
||
93 | View Code Duplication | switch ($bufferWB[0]['Chemistry']) { |
|
94 | case 1: $techn = 'Other'; break; |
||
95 | case 2: $techn = 'Unknown'; break; |
||
96 | case 3: $techn = 'PbAc'; break; |
||
97 | case 4: $techn = 'NiCd'; break; |
||
98 | case 5: $techn = 'NiMH'; break; |
||
99 | case 6: $techn = 'Li-ion'; break; |
||
100 | case 7: $techn = 'Zinc-air'; break; |
||
101 | case 8: $techn = 'Li-poly'; break; |
||
102 | } |
||
103 | } |
||
104 | $bufferWPB = CommonFunctions::getWMI($_cim, 'Win32_PortableBattery', array('DesignVoltage', 'Chemistry', 'DesignCapacity', 'FullChargeCapacity')); |
||
105 | if (isset($bufferWPB[0]['DesignVoltage'])) { |
||
106 | $buffer_info .= 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN='.($bufferWPB[0]['DesignVoltage']*1000)."\n"; |
||
107 | } |
||
108 | // sometimes Chemistry from Win32_Battery returns 2 but Win32_PortableBattery returns e.g. 6 |
||
109 | if ((($techn == '') || ($techn == 'Unknown')) && isset($bufferWPB[0]['Chemistry'])) { |
||
110 | View Code Duplication | switch ($bufferWPB[0]['Chemistry']) { |
|
111 | case 1: $techn = 'Other'; break; |
||
112 | case 2: $techn = 'Unknown'; break; |
||
113 | case 3: $techn = 'PbAc'; break; |
||
114 | case 4: $techn = 'NiCd'; break; |
||
115 | case 5: $techn = 'NiMH'; break; |
||
116 | case 6: $techn = 'Li-ion'; break; |
||
117 | case 7: $techn = 'Zinc-air'; break; |
||
118 | case 8: $techn = 'Li-poly'; break; |
||
119 | } |
||
120 | } |
||
121 | if ($techn != '') $buffer_info .= 'POWER_SUPPLY_TECHNOLOGY='.$techn."\n"; |
||
122 | |||
123 | $bufferBS = CommonFunctions::getWMI($_wmi, 'BatteryStatus', array('RemainingCapacity', 'Voltage')); |
||
124 | if (sizeof($bufferBS)>0) { |
||
125 | if (isset($bufferBS[0]['RemainingCapacity']) && ($bufferBS[0]['RemainingCapacity']>0)) { |
||
126 | $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.($bufferBS[0]['RemainingCapacity']*1000)."\n"; |
||
127 | $capacity = ''; |
||
128 | } |
||
129 | if (isset($bufferBS[0]['Voltage']) && ($bufferBS[0]['Voltage']>0)) { |
||
130 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.($bufferBS[0]['Voltage']*1000)."\n"; |
||
131 | } elseif (isset($bufferWB[0]['DesignVoltage'])) { |
||
132 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.($bufferWB[0]['DesignVoltage']*1000)."\n"; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | if (!isset($bufferWPB[0]['FullChargeCapacity'])) { |
||
137 | $bufferBFCC = CommonFunctions::getWMI($_wmi, 'BatteryFullChargedCapacity', array('FullChargedCapacity')); |
||
138 | if ((sizeof($bufferBFCC)>0) && isset($bufferBFCC[0]['FullChargedCapacity'])) { |
||
139 | $bufferWPB[0]['FullChargeCapacity'] = $bufferBFCC[0]['FullChargedCapacity']; |
||
140 | } |
||
141 | } |
||
142 | if (isset($bufferWPB[0]['FullChargeCapacity'])) { |
||
143 | $buffer_info .= 'POWER_SUPPLY_ENERGY_FULL='.($bufferWPB[0]['FullChargeCapacity']*1000)."\n"; |
||
144 | View Code Duplication | if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.(round($capacity*$bufferWPB[0]['FullChargeCapacity']*10)."\n"); |
|
145 | if (isset($bufferWPB[0]['DesignCapacity']) && ($bufferWPB[0]['DesignCapacity']!=0)) |
||
146 | $buffer_info .= 'POWER_SUPPLY_ENERGY_FULL_DESIGN='.($bufferWPB[0]['DesignCapacity']*1000)."\n"; |
||
147 | } elseif (isset($bufferWPB[0]['DesignCapacity'])) { |
||
148 | $buffer_info .= 'POWER_SUPPLY_ENERGY_FULL_DESIGN='.($bufferWPB[0]['DesignCapacity']*1000)."\n"; |
||
149 | View Code Duplication | if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.(round($capacity*$bufferWPB[0]['DesignCapacity']*10)."\n"); |
|
150 | } else { |
||
151 | if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_CAPACITY='.$capacity."\n"; |
||
152 | } |
||
153 | |||
154 | $bufferBCC = CommonFunctions::getWMI($_wmi, 'BatteryCycleCount', array('CycleCount')); |
||
155 | if ((sizeof($bufferBCC)>0) && isset($bufferBCC[0]['CycleCount']) && ($bufferBCC[0]['CycleCount']>0)) { |
||
156 | $buffer_info .= 'POWER_SUPPLY_CYCLE_COUNT='.$bufferBCC[0]['CycleCount']."\n"; |
||
157 | } |
||
158 | } |
||
159 | } elseif (PSI_OS == 'Darwin') { |
||
160 | $buffer_info = ''; |
||
161 | $buffer_state = ''; |
||
162 | CommonFunctions::executeProgram('ioreg', '-w0 -l -n AppleSmartBattery -r', $buffer_info, false); |
||
163 | } elseif (PSI_OS == 'FreeBSD') { |
||
164 | $buffer_info = ''; |
||
165 | $buffer_state = ''; |
||
166 | CommonFunctions::executeProgram('acpiconf', '-i batt', $buffer_info, false); |
||
167 | } elseif (PSI_OS == 'OpenBSD') { |
||
168 | $buffer_info = ''; |
||
169 | $buffer_state = ''; |
||
170 | CommonFunctions::executeProgram('sysctl', 'hw.sensors.acpibat0', $buffer_info, false); |
||
171 | } else { |
||
172 | $buffer_info = ''; |
||
173 | $buffer_state = ''; |
||
174 | $bat_name = PSI_PLUGIN_BAT_DEVICE; |
||
175 | $rfts_bi = CommonFunctions::rfts('/proc/acpi/battery/'.$bat_name.'/info', $buffer_info, 0, 4096, false); |
||
176 | $rfts_bs = CommonFunctions::rfts('/proc/acpi/battery/'.$bat_name.'/state', $buffer_state, 0, 4096, false); |
||
177 | if (!$rfts_bi && !$rfts_bs) { |
||
178 | $buffer_info = ''; |
||
179 | $buffer_state = ''; |
||
180 | if (!CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/uevent', $buffer_info, 0, 4096, false)) { |
||
181 | if (CommonFunctions::rfts('/sys/class/power_supply/battery/uevent', $buffer_info, 0, 4096, false)) { |
||
182 | $bat_name = 'battery'; |
||
183 | } else { |
||
184 | CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/uevent', $buffer_info, 0, 4096, PSI_DEBUG); // Once again but with debug |
||
185 | } |
||
186 | } |
||
187 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_min_design', $buffer1, 1, 4096, false)) { |
|
188 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN='.$buffer1."\n"; |
||
189 | } |
||
190 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_max_design', $buffer1, 1, 4096, false)) { |
|
191 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_MAX_DESIGN='.$buffer1."\n"; |
||
192 | } |
||
193 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_now', $buffer1, 1, 4096, false)) { |
|
194 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.$buffer1."\n"; |
||
195 | } |
||
196 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/energy_full', $buffer1, 1, 4096, false)) { |
|
197 | $buffer_state .= 'POWER_SUPPLY_ENERGY_FULL='.$buffer1."\n"; |
||
198 | } |
||
199 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/energy_now', $buffer1, 1, 4096, false)) { |
|
200 | $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.$buffer1."\n"; |
||
201 | } |
||
202 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/charge_full', $buffer1, 1, 4096, false)) { |
|
203 | $buffer_state .= 'POWER_SUPPLY_CHARGE_FULL='.$buffer1."\n"; |
||
204 | } |
||
205 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/charge_now', $buffer1, 1, 4096, false)) { |
|
206 | $buffer_state .= 'POWER_SUPPLY_CHARGE_NOW='.$buffer1."\n"; |
||
207 | } |
||
208 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/capacity', $buffer1, 1, 4096, false)) { |
|
209 | $buffer_state .= 'POWER_SUPPLY_CAPACITY='.$buffer1; |
||
210 | } |
||
211 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/technology', $buffer1, 1, 4096, false)) { |
|
212 | $buffer_state .= 'POWER_SUPPLY_TECHNOLOGY='.$buffer1; |
||
213 | } |
||
214 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/status', $buffer1, 1, 4096, false)) { |
|
215 | $buffer_state .= 'POWER_SUPPLY_STATUS='.$buffer1; |
||
216 | } |
||
217 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/batt_temp', $buffer1, 1, 4096, false)) { |
|
218 | $buffer_state .= 'POWER_SUPPLY_TEMP='.$buffer1; |
||
219 | } |
||
220 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/batt_vol', $buffer1, 1, 4096, false)) { |
|
221 | $buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.$buffer1; |
||
222 | } |
||
223 | View Code Duplication | if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/health', $buffer1, 1, 4096, false)) { |
|
224 | $buffer_state .= 'POWER_SUPPLY_HEALTH='.$buffer1; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | break; |
||
229 | case 'data': |
||
230 | CommonFunctions::rfts(APP_ROOT."/data/bat_info.txt", $buffer_info); |
||
231 | CommonFunctions::rfts(APP_ROOT."/data/bat_state.txt", $buffer_state); |
||
232 | break; |
||
233 | default: |
||
234 | $this->global_error->addConfigError("__construct()", "PSI_PLUGIN_BAT_ACCESS"); |
||
235 | break; |
||
236 | } |
||
237 | $this->_filecontent['info'] = preg_split("/\n/", $buffer_info, -1, PREG_SPLIT_NO_EMPTY); |
||
238 | $this->_filecontent['state'] = preg_split("/\n/", $buffer_state, -1, PREG_SPLIT_NO_EMPTY); |
||
239 | } |
||
240 | |||
580 |
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.