Conditions | 62 |
Paths | > 20000 |
Total Lines | 208 |
Code Lines | 108 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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 |
||
266 | public function Check($logAsDebug = false) { |
||
267 | // semantic checks general "turn off switch" |
||
268 | if (defined("DO_SEMANTIC_CHECKS") && DO_SEMANTIC_CHECKS === false) { |
||
269 | SLog::Write(LOGLEVEL_DEBUG, "SyncObject->Check(): semantic checks disabled. Check your config for 'DO_SEMANTIC_CHECKS'."); |
||
270 | |||
271 | return true; |
||
272 | } |
||
273 | |||
274 | $defaultLogLevel = LOGLEVEL_WARN; |
||
275 | $this->checkedParameters = 0; |
||
276 | |||
277 | // in some cases non-false checks should not provoke a WARN log but only a DEBUG log |
||
278 | if ($logAsDebug) { |
||
279 | $defaultLogLevel = LOGLEVEL_DEBUG; |
||
280 | } |
||
281 | |||
282 | $objClass = get_class($this); |
||
283 | foreach ($this->mapping as $k => $v) { |
||
284 | // check sub-objects recursively |
||
285 | if (isset($v[self::STREAMER_TYPE], $this->{$v[self::STREAMER_VAR]})) { |
||
286 | if ($this->{$v[self::STREAMER_VAR]} instanceof SyncObject) { |
||
287 | if (!$this->{$v[self::STREAMER_VAR]}->Check($logAsDebug)) { |
||
288 | return false; |
||
289 | } |
||
290 | } |
||
291 | elseif (is_array($this->{$v[self::STREAMER_VAR]})) { |
||
292 | foreach ($this->{$v[self::STREAMER_VAR]} as $subobj) { |
||
293 | if ($subobj instanceof SyncObject && !$subobj->Check($logAsDebug)) { |
||
294 | return false; |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | |||
300 | if (isset($v[self::STREAMER_CHECKS])) { |
||
301 | foreach ($v[self::STREAMER_CHECKS] as $rule => $condition) { |
||
302 | // count parameter if it's set |
||
303 | if (isset($this->{$v[self::STREAMER_VAR]})) { |
||
304 | ++$this->checkedParameters; |
||
305 | } |
||
306 | |||
307 | // check REQUIRED settings |
||
308 | if ($rule === self::STREAMER_CHECK_REQUIRED && (!isset($this->{$v[self::STREAMER_VAR]}) || $this->{$v[self::STREAMER_VAR]} === '')) { |
||
309 | // parameter is not set but .. |
||
310 | // requested to set to 0 |
||
311 | if ($condition === self::STREAMER_CHECK_SETZERO) { |
||
312 | $this->{$v[self::STREAMER_VAR]} = 0; |
||
313 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to 0", $objClass, $v[self::STREAMER_VAR])); |
||
314 | } |
||
315 | // requested to be set to 1 |
||
316 | elseif ($condition === self::STREAMER_CHECK_SETONE) { |
||
317 | $this->{$v[self::STREAMER_VAR]} = 1; |
||
318 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to 1", $objClass, $v[self::STREAMER_VAR])); |
||
319 | } |
||
320 | // requested to be set to 2 |
||
321 | elseif ($condition === self::STREAMER_CHECK_SETTWO) { |
||
322 | $this->{$v[self::STREAMER_VAR]} = 2; |
||
323 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to 2", $objClass, $v[self::STREAMER_VAR])); |
||
324 | } |
||
325 | // requested to be set to '' |
||
326 | elseif ($condition === self::STREAMER_CHECK_SETEMPTY) { |
||
327 | if (!isset($this->{$v[self::STREAMER_VAR]})) { |
||
328 | $this->{$v[self::STREAMER_VAR]} = ''; |
||
329 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to ''", $objClass, $v[self::STREAMER_VAR])); |
||
330 | } |
||
331 | } |
||
332 | // there is another value !== false |
||
333 | elseif ($condition !== false) { |
||
334 | $this->{$v[self::STREAMER_VAR]} = $condition; |
||
335 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to '%s'", $objClass, $v[self::STREAMER_VAR], $condition)); |
||
336 | } |
||
337 | // no fix available! |
||
338 | else { |
||
339 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Unmet condition in object from type %s: parameter '%s' is required but not set. Check failed!", $objClass, $v[self::STREAMER_VAR])); |
||
340 | |||
341 | return false; |
||
342 | } |
||
343 | } // end STREAMER_CHECK_REQUIRED |
||
344 | |||
345 | // check STREAMER_CHECK_ZEROORONE |
||
346 | if ($rule === self::STREAMER_CHECK_ZEROORONE && isset($this->{$v[self::STREAMER_VAR]})) { |
||
347 | if ($this->{$v[self::STREAMER_VAR]} != 0 && $this->{$v[self::STREAMER_VAR]} != 1) { |
||
348 | $newval = $condition === self::STREAMER_CHECK_SETZERO ? 0 : 1; |
||
349 | $this->{$v[self::STREAMER_VAR]} = $newval; |
||
350 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): Fixed object from type %s: parameter '%s' is set to '%s' as it was not 0 or 1", $objClass, $v[self::STREAMER_VAR], $newval)); |
||
351 | } |
||
352 | }// end STREAMER_CHECK_ZEROORONE |
||
353 | |||
354 | // check STREAMER_CHECK_ONEVALUEOF |
||
355 | if ($rule === self::STREAMER_CHECK_ONEVALUEOF && isset($this->{$v[self::STREAMER_VAR]})) { |
||
356 | if (!in_array($this->{$v[self::STREAMER_VAR]}, $condition)) { |
||
357 | SLog::Write($defaultLogLevel, sprintf("SyncObject->Check(): object from type %s: parameter '%s'->'%s' is not in the range of allowed values.", $objClass, $v[self::STREAMER_VAR], $this->{$v[self::STREAMER_VAR]})); |
||
358 | |||
359 | return false; |
||
360 | } |
||
361 | }// end STREAMER_CHECK_ONEVALUEOF |
||
362 | |||
363 | // Check value compared to other value or literal |
||
364 | if ($rule === self::STREAMER_CHECK_CMPHIGHER || $rule === self::STREAMER_CHECK_CMPLOWER) { |
||
365 | if (isset($this->{$v[self::STREAMER_VAR]})) { |
||
366 | $cmp = false; |
||
367 | // directly compare against literals |
||
368 | if (is_int($condition)) { |
||
369 | $cmp = $condition; |
||
370 | } |
||
371 | // check for invalid compare-to |
||
372 | elseif (!isset($this->mapping[$condition])) { |
||
373 | SLog::Write(LOGLEVEL_ERROR, sprintf("SyncObject->Check(): Can not compare parameter '%s' against the other value '%s' as it is not defined object from type %s. Please report this! Check skipped!", $objClass, $v[self::STREAMER_VAR], $condition)); |
||
374 | |||
375 | continue; |
||
376 | } |
||
377 | else { |
||
378 | $cmpPar = $this->mapping[$condition][self::STREAMER_VAR]; |
||
379 | if (isset($this->{$cmpPar})) { |
||
380 | $cmp = $this->{$cmpPar}; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | if ($cmp === false) { |
||
385 | SLog::Write(LOGLEVEL_WARN, sprintf("SyncObject->Check(): Unmet condition in object from type %s: parameter '%s' can not be compared, as the comparable is not set. Check failed!", $objClass, $v[self::STREAMER_VAR])); |
||
386 | |||
387 | return false; |
||
388 | } |
||
389 | if ( |
||
390 | ($rule == self::STREAMER_CHECK_CMPHIGHER && intval($this->{$v[self::STREAMER_VAR]}) < $cmp) || |
||
391 | ($rule == self::STREAMER_CHECK_CMPLOWER && intval($this->{$v[self::STREAMER_VAR]}) > $cmp) |
||
392 | ) { |
||
393 | SLog::Write(LOGLEVEL_WARN, sprintf( |
||
394 | "SyncObject->Check(): Unmet condition in object from type %s: parameter '%s' is %s than '%s'. Check failed!", |
||
395 | $objClass, |
||
396 | $v[self::STREAMER_VAR], |
||
397 | ($rule === self::STREAMER_CHECK_CMPHIGHER) ? 'LOWER' : 'HIGHER', |
||
398 | isset($cmpPar) ? $cmpPar : $condition |
||
399 | )); |
||
400 | |||
401 | return false; |
||
402 | } |
||
403 | } |
||
404 | } // STREAMER_CHECK_CMP* |
||
405 | |||
406 | // check STREAMER_CHECK_LENGTHMAX |
||
407 | if ($rule === self::STREAMER_CHECK_LENGTHMAX && isset($this->{$v[self::STREAMER_VAR]})) { |
||
408 | if (is_array($this->{$v[self::STREAMER_VAR]})) { |
||
409 | // implosion takes 2bytes, so we just assume ", " here |
||
410 | $chkstr = implode(", ", $this->{$v[self::STREAMER_VAR]}); |
||
411 | } |
||
412 | else { |
||
413 | $chkstr = $this->{$v[self::STREAMER_VAR]}; |
||
414 | } |
||
415 | |||
416 | if (strlen($chkstr) > $condition) { |
||
417 | SLog::Write(LOGLEVEL_WARN, sprintf("SyncObject->Check(): object from type %s: parameter '%s' is longer than %d. Check failed", $objClass, $v[self::STREAMER_VAR], $condition)); |
||
418 | |||
419 | return false; |
||
420 | } |
||
421 | }// end STREAMER_CHECK_LENGTHMAX |
||
422 | |||
423 | // check STREAMER_CHECK_EMAIL |
||
424 | // if $condition is false then the check really fails. Otherwise invalid emails are removed. |
||
425 | // if nothing is left (all emails were false), the parameter is set to condition |
||
426 | if ($rule === self::STREAMER_CHECK_EMAIL && isset($this->{$v[self::STREAMER_VAR]})) { |
||
427 | if ($condition === false && ((is_array($this->{$v[self::STREAMER_VAR]}) && empty($this->{$v[self::STREAMER_VAR]})) || strlen($this->{$v[self::STREAMER_VAR]}) == 0)) { |
||
428 | continue; |
||
429 | } |
||
430 | |||
431 | $as_array = false; |
||
432 | |||
433 | if (is_array($this->{$v[self::STREAMER_VAR]})) { |
||
434 | $mails = $this->{$v[self::STREAMER_VAR]}; |
||
435 | $as_array = true; |
||
436 | } |
||
437 | else { |
||
438 | $mails = [$this->{$v[self::STREAMER_VAR]}]; |
||
439 | } |
||
440 | |||
441 | $output = []; |
||
442 | foreach ($mails as $mail) { |
||
443 | if (!Utils::CheckEmail($mail)) { |
||
444 | SLog::Write(LOGLEVEL_WARN, sprintf("SyncObject->Check(): object from type %s: parameter '%s' contains an invalid email address '%s'. Address is removed.", $objClass, $v[self::STREAMER_VAR], $mail)); |
||
445 | } |
||
446 | else { |
||
447 | $output[] = $mail; |
||
448 | } |
||
449 | } |
||
450 | if (count($mails) != count($output)) { |
||
451 | if ($condition === false) { |
||
452 | return false; |
||
453 | } |
||
454 | |||
455 | // nothing left, use $condition as new value |
||
456 | if (count($output) == 0) { |
||
457 | $output[] = $condition; |
||
458 | } |
||
459 | |||
460 | // if we are allowed to rewrite the attribute, we do that |
||
461 | if ($as_array) { |
||
462 | $this->{$v[self::STREAMER_VAR]} = $output; |
||
463 | } |
||
464 | else { |
||
465 | $this->{$v[self::STREAMER_VAR]} = $output[0]; |
||
466 | } |
||
467 | } |
||
468 | }// end STREAMER_CHECK_EMAIL |
||
469 | } // foreach CHECKS |
||
470 | } // isset CHECKS |
||
471 | } // foreach mapping |
||
472 | |||
473 | return true; |
||
474 | } |
||
501 |