Conditions | 15 |
Paths | 88 |
Total Lines | 107 |
Code Lines | 54 |
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 |
||
213 | function check_service($command) |
||
214 | { |
||
215 | // This array is used to test for valid UOM's to be used for graphing. |
||
216 | // Valid values from: https://nagios-plugins.org/doc/guidelines.html#AEN200 |
||
217 | // Note: This array must be decend from 2 char to 1 char so that the search works correctly. |
||
218 | $valid_uom = ['us', 'ms', 'KB', 'MB', 'GB', 'TB', 'c', 's', '%', 'B']; |
||
219 | |||
220 | // Make our command safe. |
||
221 | $parts = preg_split('~(?:\'[^\']*\'|"[^"]*")(*SKIP)(*F)|\h+~', trim($command)); |
||
222 | $safe_command = implode(' ', array_map(function ($part) { |
||
223 | $trimmed = preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $part); |
||
224 | |||
225 | return escapeshellarg($trimmed); |
||
226 | }, $parts)); |
||
227 | |||
228 | d_echo("Request: $safe_command\n"); |
||
229 | |||
230 | // Run the command and return its response. |
||
231 | exec('LC_NUMERIC="C" ' . $safe_command, $response_array, $status); |
||
232 | |||
233 | // exec returns an array, lets implode it back to a string. |
||
234 | $response_string = implode("\n", $response_array); |
||
235 | |||
236 | // Split out the response and the performance data. |
||
237 | [$response, $perf] = explode('|', $response_string); |
||
238 | |||
239 | // Split each performance metric |
||
240 | $perf_arr = explode(' ', $perf); |
||
241 | |||
242 | // Create an array for our metrics. |
||
243 | $metrics = []; |
||
244 | |||
245 | // Loop through the perf string extracting our metric data |
||
246 | foreach ($perf_arr as $string) { |
||
247 | // Separate the DS and value: DS=value |
||
248 | [$ds,$values] = explode('=', trim($string)); |
||
249 | |||
250 | // Keep the first value, discard the others. |
||
251 | [$value,,,] = explode(';', trim($values)); |
||
252 | $value = trim($value); |
||
253 | |||
254 | // Set an empty uom |
||
255 | $uom = ''; |
||
256 | |||
257 | // is the UOM valid - https://nagios-plugins.org/doc/guidelines.html#AEN200 |
||
258 | foreach ($valid_uom as $v) { |
||
259 | if ((strlen($value) - strlen($v)) === strpos($value, $v)) { |
||
260 | // Yes, store and strip it off the value |
||
261 | $uom = $v; |
||
262 | $value = substr($value, 0, -strlen($v)); |
||
263 | break; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | if ($ds != '') { |
||
268 | // Normalize ds for rrd : ds-name must be 1 to 19 characters long in the characters [a-zA-Z0-9_] |
||
269 | // http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html |
||
270 | $normalized_ds = preg_replace('/[^a-zA-Z0-9_]/', '', $ds); |
||
271 | // if ds_name is longer than 19 characters, only use the first 19 |
||
272 | if (strlen($normalized_ds) > 19) { |
||
273 | $normalized_ds = substr($normalized_ds, 0, 19); |
||
274 | d_echo($ds . ' exceeded 19 characters, renaming to ' . $normalized_ds . "\n"); |
||
275 | } |
||
276 | if ($ds != $normalized_ds) { |
||
277 | // ds has changed. check if normalized_ds is already in the array |
||
278 | if (isset($metrics[$normalized_ds])) { |
||
279 | d_echo($normalized_ds . " collides with an existing index\n"); |
||
280 | $perf_unique = 0; |
||
281 | // Try to generate a unique name |
||
282 | for ($i = 0; $i < 10; $i++) { |
||
283 | $tmp_ds_name = substr($normalized_ds, 0, 18) . $i; |
||
284 | if (! isset($metrics[$tmp_ds_name])) { |
||
285 | d_echo($normalized_ds . " collides with an existing index\n"); |
||
286 | $normalized_ds = $tmp_ds_name; |
||
287 | $perf_unique = 1; |
||
288 | break; |
||
289 | } |
||
290 | } |
||
291 | if ($perf_unique == 0) { |
||
292 | // Try harder to generate a unique name |
||
293 | for ($i = 0; $i < 10; $i++) { |
||
294 | for ($j = 0; $j < 10; $j++) { |
||
295 | $tmp_ds_name = substr($normalized_ds, 0, 17) . $j . $i; |
||
296 | if (! isset($perf[$tmp_ds_name])) { |
||
297 | $normalized_ds = $tmp_ds_name; |
||
298 | $perf_unique = 1; |
||
299 | break 2; |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | if ($perf_unique == 0) { |
||
305 | d_echo('could not generate a unique ds-name for ' . $ds . "\n"); |
||
306 | } |
||
307 | } |
||
308 | $ds = $normalized_ds; |
||
309 | } |
||
310 | // We have a DS. Add an entry to the array. |
||
311 | d_echo('Perf Data - DS: ' . $ds . ', Value: ' . $value . ', UOM: ' . $uom . "\n"); |
||
312 | $metrics[$ds] = ['value'=>$value, 'uom'=>$uom]; |
||
313 | } else { |
||
314 | // No DS. Don't add an entry to the array. |
||
315 | d_echo("Perf Data - None.\n"); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return [$status, $response, $metrics]; |
||
320 | } |
||
331 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: