Conditions | 60 |
Paths | > 20000 |
Total Lines | 124 |
Code Lines | 86 |
Lines | 48 |
Ratio | 38.71 % |
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 |
||
136 | public function execute() |
||
137 | { |
||
138 | if (empty($this->_filecontent) || empty($this->_ids)) { |
||
139 | return; |
||
140 | } |
||
141 | foreach ($this->_filecontent as $disk=>$result) { |
||
142 | // set the start and end offset in the result string at the beginning and end respectively |
||
143 | // just in case we don't find the two strings, so that it still works as expected. |
||
144 | $startIndex = 0; |
||
145 | $endIndex = 0; |
||
146 | $vendorInfos = ""; |
||
147 | |||
148 | // locate the beginning string offset for the attributes |
||
149 | if (preg_match('/(Vendor Specific SMART Attributes with Thresholds)/', $result, $matches, PREG_OFFSET_CAPTURE)) |
||
150 | $startIndex = $matches[0][1]; |
||
151 | |||
152 | // locate the end string offset for the attributes, this is usually right before string "SMART Error Log Version" or "SMART Error Log not supported" or "Error SMART Error Log Read failed" (hopefully every output has it!) |
||
153 | if (preg_match('/(SMART Error Log Version)|(SMART Error Log not supported)|(Error SMART Error Log Read failed)/', $result, $matches, PREG_OFFSET_CAPTURE)) |
||
154 | $endIndex = $matches[0][1]; |
||
155 | |||
156 | if ($startIndex && $endIndex && ($endIndex>$startIndex)) |
||
157 | $vendorInfos = preg_split("/\n/", substr($result, $startIndex, $endIndex - $startIndex)); |
||
158 | |||
159 | if (!empty($vendorInfos)) { |
||
160 | $labels = preg_split('/\s+/', $vendorInfos[1]); |
||
161 | foreach ($labels as $k=>$v) { |
||
162 | $labels[$k] = str_replace('#', '', strtolower($v)); |
||
163 | } |
||
164 | $i = 0; // Line number |
||
165 | foreach ($vendorInfos as $line) { |
||
166 | $line = preg_replace('/^\s+/', '', $line); |
||
167 | $values = preg_split('/\s+/', $line); |
||
168 | if (count($values) > count($labels)) { |
||
169 | $values = array_slice($values, 0, count($labels), true); |
||
170 | } |
||
171 | $j = 0; |
||
172 | $found = false; |
||
173 | foreach ($values as $value) { |
||
174 | if ((in_array($value, array_keys($this->_ids)) && $labels[$j] == 'id')) { |
||
175 | $arrFullVa = preg_split('/-/', $this->_ids[$value]); |
||
176 | if (($arrFullVa[0]=="#replace") && !empty($arrFullVa[1])) |
||
177 | $value=$arrFullVa[1]; |
||
178 | } |
||
179 | if (((in_array($value, array_keys($this->_ids)) && $labels[$j] == 'id') || ($found && (in_array($labels[$j], array_values($this->_ids)))) || ($found && $labels[$j] == 'attribute_name'))) { |
||
180 | $this->_result[$disk][$i][$labels[$j]] = $value; |
||
181 | $found = true; |
||
182 | } |
||
183 | $j++; |
||
184 | } |
||
185 | $i++; |
||
186 | } |
||
187 | } else { |
||
188 | //SCSI devices |
||
189 | View Code Duplication | if (!empty($this->_ids[1]) && ($this->_ids[1]=="raw_value")) { |
|
190 | preg_match('/read\: (.*)\n/', $result, $lines); |
||
191 | if (!empty($lines) && !empty($lines[0])) { |
||
192 | $values=preg_split('/\s+/', $lines[0]); |
||
193 | if (!empty($values) && ($values[7]!=null)) { |
||
194 | $vals=preg_split('/[,\.]/', $values[7]); |
||
195 | $this->_result[$disk][0]['id'] = 1; |
||
196 | $this->_result[$disk][0]['attribute_name'] = "Raw_Read_Error_Rate"; |
||
197 | $this->_result[$disk][0]['raw_value'] = $vals[0]; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | View Code Duplication | if (!empty($this->_ids[5]) && ($this->_ids[5]=="raw_value")) { |
|
202 | preg_match('/Elements in grown defect list\: (.*)\n/', $result, $lines); |
||
203 | if (!empty($lines) && !empty($lines[0])) { |
||
204 | $values=preg_split('/\s+/', $lines[0]); |
||
205 | if (!empty($values) && ($values[5]!=null)) { |
||
206 | $vals=preg_split('/[,\.]/', $values[5]); |
||
207 | $this->_result[$disk][1]['id'] = 5; |
||
208 | $this->_result[$disk][1]['attribute_name'] = "Reallocated_Sector_Ct"; |
||
209 | $this->_result[$disk][1]['raw_value'] = $vals[0]; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | View Code Duplication | if (!empty($this->_ids[9]) && ($this->_ids[9]=="raw_value")) { |
|
214 | preg_match('/ number of hours powered up = (.*)\n/', $result, $lines); |
||
215 | if (!empty($lines) && !empty($lines[0])) { |
||
216 | $values=preg_split('/\s+/', $lines[0]); |
||
217 | if (!empty($values) && ($values[7]!=null)) { |
||
218 | $vals=preg_split('/[,\.]/', $values[7]); |
||
219 | $this->_result[$disk][2]['id'] = 9; |
||
220 | $this->_result[$disk][2]['attribute_name'] = "Power_On_Hours"; |
||
221 | $this->_result[$disk][2]['raw_value'] = $vals[0]; |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | View Code Duplication | if (!empty($this->_ids[194]) && ($this->_ids[194]=="raw_value")) { |
|
226 | preg_match('/Current Drive Temperature\: (.*)\n/', $result, $lines); |
||
227 | if (!empty($lines) && !empty($lines[0])) { |
||
228 | $values=preg_split('/\s+/', $lines[0]); |
||
229 | if (!empty($values) && ($values[3]!=null)) { |
||
230 | $vals=preg_split('/[,\.]/', $values[3]); |
||
231 | $this->_result[$disk][3]['id'] = 194; |
||
232 | $this->_result[$disk][3]['attribute_name'] = "Temperature_Celsius"; |
||
233 | $this->_result[$disk][3]['raw_value'] = $vals[0]; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | //Usage test |
||
240 | $newIds = array(); |
||
241 | foreach ($this->_ids as $id=>$column_name) { |
||
242 | $found = 0; |
||
243 | foreach ($this->_result as $diskName=>$diskInfos) { |
||
244 | if ($found!=2) foreach ($diskInfos as $lineInfos) { |
||
245 | if ($found!=2) { |
||
246 | $found = 0; |
||
247 | foreach ($lineInfos as $label=>$value) { |
||
248 | if (($found==0) && ($label=="id") && ($value==$id)) |
||
249 | $found = 1; |
||
250 | if (($found==1) && ($label==$column_name)) |
||
251 | $found = 2; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | if ($found==2) $newIds[$id] = $this->_ids[$id]; |
||
257 | } |
||
258 | $this->_ids = $newIds; |
||
259 | } |
||
260 | |||
303 |
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.