| Conditions | 53 |
| Paths | > 20000 |
| Total Lines | 260 |
| Code Lines | 149 |
| Lines | 66 |
| Ratio | 25.38 % |
| 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 |
||
| 190 | public function magic() { |
||
| 191 | |||
| 192 | // simple things first: do we know anything about the realm, either |
||
| 193 | // because it's a CAT participant or because it's in the eduroam DB? |
||
| 194 | // if so, we can exclude the INFRA_NONEXISTENTREALM cause |
||
| 195 | |||
| 196 | $this->additionalFindings[Telepath::INFRA_NONEXISTENTREALM][] = ["ID1" => $this->catIdP, "ID2" => $this->dbIdP]; |
||
| 197 | |||
| 198 | if ($this->catIdP != \core\Federation::UNKNOWN_IDP || $this->dbIdP != \core\Federation::UNKNOWN_IDP) { |
||
| 199 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NONEXISTENTREALM]); |
||
| 200 | } |
||
| 201 | |||
| 202 | // let's do the least amount of testing needed: |
||
| 203 | // - The CAT reachability test already covers ELTRs, IdP NRO level and the IdP itself. |
||
| 204 | // if the realm maps to a CAT IdP, we can run the more thorough tests; otherwise just |
||
| 205 | // the normal shallow ones |
||
| 206 | |||
| 207 | if ($this->catIdP > 0) { |
||
| 208 | $idpObject = new \core\IdP($this->catIdP); |
||
| 209 | $profileObjects = $idpObject->listProfiles(); |
||
| 210 | |||
| 211 | $bestProfile = FALSE; |
||
| 212 | |||
| 213 | |||
| 214 | foreach ($profileObjects as $profileObject) { |
||
| 215 | $mangledRealm = substr($profileObject->realm, strpos($profileObject->realm, "@") + 1); |
||
| 216 | $readinessLevel = $profileObject->readinessLevel(); |
||
| 217 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SHOWTIME && $mangledRealm == $this->realm) { |
||
| 218 | $bestProfile = $profileObject; |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SUFFICIENTCONFIG && $profileObject->realm == $this->realm) { |
||
| 222 | $bestProfile = $profileObject; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | if ($bestProfile == FALSE) { // huh? no match on the realm. Then let's take the next-best with SUFFICIENTCONFIG |
||
| 226 | foreach ($profileObjects as $profileObject) { |
||
| 227 | $readinessLevel = $profileObject->readinessLevel(); |
||
| 228 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SUFFICIENTCONFIG) { |
||
| 229 | $bestProfile = $profileObject; |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | if ($bestProfile != FALSE) { // still nothing? then there's only a very incomplete profile definition, and we can't work with that. Fall back to shallow |
||
| 235 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["Profile" => $bestProfile->identifier]; |
||
| 236 | $testsuite = new RADIUSTests($this->realm, $bestProfile->getRealmCheckOuterUsername(), $bestProfile->getEapMethodsinOrderOfPreference(1), $bestProfile->getCollapsedAttributes()['eap:server_name'], $bestProfile->getCollapsedAttributes()["eap:ca_file"]); |
||
| 237 | } else { |
||
| 238 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["Profile" => "UNCONCLUSIVE"]; |
||
| 239 | $testsuite = new RADIUSTests($this->realm, "anonymous@" . $this->realm); |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | $testsuite = new RADIUSTests($this->realm, "anonymous@" . $this->realm); |
||
| 243 | } |
||
| 244 | |||
| 245 | // we are expecting to get a REJECT from all runs, because that means the packet got through to the IdP. |
||
| 246 | // (the ETLR sometimes does a "Reject instead of Ignore" but that is filtered out and changed into a timeout |
||
| 247 | // by the test suite automatically, so it does not disturb the measurement) |
||
| 248 | // If that's true, we can exclude two sources of problems (both proxy levels). Hooray! |
||
| 249 | $allAreConversationReject = TRUE; |
||
| 250 | $atLeastOneConversationReject = FALSE; |
||
| 251 | |||
| 252 | foreach (CONFIG_DIAGNOSTICS['RADIUSTESTS']['UDP-hosts'] as $probeindex => $probe) { |
||
| 253 | $reachCheck = $testsuite->udpReachability($probeindex); |
||
| 254 | if ($reachCheck != RADIUSTests::RETVAL_CONVERSATION_REJECT) { |
||
| 255 | $allAreConversationReject = FALSE; |
||
| 256 | } else { |
||
| 257 | $atLeastOneConversationReject = TRUE; |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->additionalFindings[Telepath::INFRA_ETLR][] = ["DETAIL" => $testsuite->consolidateUdpResult($probeindex)]; |
||
| 261 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["DETAIL" => $testsuite->consolidateUdpResult($probeindex)]; |
||
| 262 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["DETAIL" => $testsuite->consolidateUdpResult($probeindex)]; |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($allAreConversationReject) { |
||
| 266 | $this->additionalFindings[Telepath::INFRA_ETLR][] = ["CONNCHECK" => RADIUSTests::RETVAL_CONVERSATION_REJECT]; |
||
| 267 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["CONNCHECK" => RADIUSTests::RETVAL_CONVERSATION_REJECT]; |
||
| 268 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["CONNCHECK" => RADIUSTests::RETVAL_CONVERSATION_REJECT]; |
||
| 269 | $this->additionalFindings[Telepath::INFRA_LINK_ETLR_NRO_IDP][] = ["LINKCHECK" => RADIUSTests::L_OK]; |
||
| 270 | // we have actually reached an IdP, so all links are good, and the |
||
| 271 | // realm is routable in eduroam. So even if it exists in neither DB |
||
| 272 | // we can exclude the NONEXISTENTREALM case |
||
| 273 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_ETLR, Telepath::INFRA_NRO_IDP, Telepath::INFRA_LINK_ETLR_NRO_IDP, Telepath::INFRA_NONEXISTENTREALM]); |
||
| 274 | }; |
||
| 275 | |||
| 276 | if ($atLeastOneConversationReject) { |
||
| 277 | // at least we can be sure it exists |
||
| 278 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NONEXISTENTREALM]); |
||
| 279 | // It could still be an IdP RADIUS problem in that some cert oddities |
||
| 280 | // in combination with the device lead to a broken auth |
||
| 281 | // if there is nothing beyond the "REMARK" level, then it's not an IdP problem |
||
| 282 | // otherwise, add the corresponding warnings and errors to $additionalFindings |
||
| 283 | switch ($this->additionalFindings[Telepath::INFRA_IDP_RADIUS][0]['DETAIL']['level']) { |
||
| 284 | case RADIUSTests::L_OK: |
||
| 285 | case RADIUSTests::L_REMARK: |
||
| 286 | // both are fine - the IdP is working and the user problem |
||
| 287 | // is not on the IdP RADIUS level |
||
| 288 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["ODDITYLEVEL" => $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][0]['DETAIL']['level']]; |
||
| 289 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_IDP_RADIUS]); |
||
| 290 | break; |
||
| 291 | case RADIUSTests::L_WARN: |
||
| 292 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["ODDITYLEVEL" => RADIUSTests::L_WARN]; |
||
| 293 | $this->probabilities[Telepath::INFRA_IDP_RADIUS] = 0.3; // possibly we found the culprit - if RADIUS server is misconfigured AND user is on a device which reacts picky about exactly this oddity. |
||
| 294 | break; |
||
| 295 | case RADIUSTests::L_ERROR: |
||
| 296 | $this->additionalFindings[Telepath::INFRA_IDP_RADIUS][] = ["ODDITYLEVEL" => RADIUSTests::L_ERROR]; |
||
| 297 | $this->probabilities[Telepath::INFRA_IDP_RADIUS] = 0.8; // errors are never good, so we can be reasonably sure we've hit the spot! |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | // - if the test does NOT go through, we need to find out which of the three is guilty |
||
| 302 | // - then, the international "via ETLR" check can be used to find out if the IdP alone |
||
| 303 | // is guilty. If that one fails, the direct monitoring of servers and ETLRs themselves |
||
| 304 | // closes the loop. |
||
| 305 | // let's see if the ETLRs are up |
||
| 306 | |||
| 307 | $etlrStatus = $this->checkEtlrStatus(); |
||
| 308 | $this->additionalFindings[Telepath::INFRA_ETLR][] = $etlrStatus; |
||
| 309 | switch ($etlrStatus["STATUS"]) { |
||
| 310 | case Telepath::STATUS_GOOD: |
||
| 311 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_ETLR]); |
||
| 312 | break; |
||
| 313 | case Telepath::STATUS_PARTIAL: |
||
| 314 | case Telepath::STATUS_MONITORINGFAIL: |
||
| 315 | // one of the ETLRs is down, or there is a failure in the monitoring system? |
||
| 316 | // This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 317 | // keep ETLR as a possible problem with original probability |
||
| 318 | break; |
||
| 319 | case Telepath::STATUS_DOWN: |
||
| 320 | // Oh! Well if it is not international roaming, that still doesn't have an effect /in this case/. |
||
| 321 | if ($this->idPFederation == $this->visitedFlr) { |
||
| 322 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_ETLR]); |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | // But it is about int'l roaming, and we are spot on here. |
||
| 326 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 327 | $this->probabilities[Telepath::INFRA_ETLR] = 0.95; |
||
| 328 | } |
||
| 329 | // next up: if the ETLR was okay, check the the FLR and its ETLR |
||
| 330 | // uplink (if we know which federation we are talking about) |
||
| 331 | |||
| 332 | if (!in_array(Telepath::INFRA_ETLR, $this->possibleFailureReasons) && $this->idPFederation != NULL) { |
||
| 333 | // first the direct connectivity to the server |
||
| 334 | $flrServerStatus = $this->checkFlrServerStatus($this->idPFederation); |
||
| 335 | View Code Duplication | switch ($flrServerStatus) { |
|
| 336 | case Telepath::STATUS_GOOD: |
||
| 337 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_GOOD]; |
||
| 338 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NRO_IDP]); |
||
| 339 | break; |
||
| 340 | case Telepath::STATUS_PARTIAL: |
||
| 341 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 342 | // keep FLR as a possible problem with original probability |
||
| 343 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_PARTIAL]; |
||
| 344 | break; |
||
| 345 | case Telepath::STATUS_DOWN: |
||
| 346 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_DOWN]; |
||
| 347 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 348 | $this->probabilities[Telepath::INFRA_NRO_IDP] = 0.95; |
||
| 349 | } |
||
| 350 | // then its uplink |
||
| 351 | $flrUplinkStatus = $this->checkFedEtlrUplink($this->idPFederation); |
||
| 352 | View Code Duplication | switch ($flrUplinkStatus) { |
|
| 353 | case Telepath::STATUS_GOOD: |
||
| 354 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_GOOD]; |
||
| 355 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NRO_IDP, Telepath::INFRA_LINK_ETLR_NRO_IDP]); |
||
| 356 | break; |
||
| 357 | case Telepath::STATUS_PARTIAL: |
||
| 358 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 359 | // keep FLR as a possible problem with original probability |
||
| 360 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_PARTIAL]; |
||
| 361 | break; |
||
| 362 | case Telepath::STATUS_DOWN: |
||
| 363 | $this->additionalFindings[Telepath::INFRA_NRO_IDP][] = ["STATUS" => Telepath::STATUS_DOWN]; |
||
| 364 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 365 | // if earlier test found the server itself to be the problem, keep it, otherwise put the blame on the link |
||
| 366 | if ($this->probabilities[Telepath::INFRA_NRO_IDP] != 0.95) { |
||
| 367 | $this->probabilities[Telepath::INFRA_LINK_ETLR_NRO_IDP] = 0.95; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | // now, if we know the country the user is currently in, let's see |
||
| 373 | // if the NRO SP-side is up |
||
| 374 | if ($this->visitedFlr !== NULL) { |
||
| 375 | $visitedFlrStatus = $this->checkFlrServerStatus($this->visitedFlr); |
||
| 376 | // direct query to server |
||
| 377 | View Code Duplication | switch ($visitedFlrStatus) { |
|
| 378 | case Telepath::STATUS_GOOD: |
||
| 379 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_GOOD]; |
||
| 380 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NRO_SP]); |
||
| 381 | break; |
||
| 382 | case Telepath::STATUS_PARTIAL: |
||
| 383 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 384 | // keep FLR as a possible problem with original probability |
||
| 385 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_PARTIAL]; |
||
| 386 | break; |
||
| 387 | case Telepath::STATUS_DOWN: |
||
| 388 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_DOWN]; |
||
| 389 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 390 | $this->probabilities[Telepath::INFRA_NRO_SP] = 0.95; |
||
| 391 | } |
||
| 392 | // and again its uplink to the ETLR |
||
| 393 | $visitedFlrUplinkStatus = $this->checkFedEtlrUplink($this->visitedFlr); |
||
| 394 | View Code Duplication | switch ($visitedFlrUplinkStatus) { |
|
| 395 | case Telepath::STATUS_GOOD: |
||
| 396 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_GOOD]; |
||
| 397 | $this->possibleFailureReasons = array_diff($this->possibleFailureReasons, [Telepath::INFRA_NRO_SP, Telepath::INFRA_LINK_ETLR_NRO_SP]); |
||
| 398 | break; |
||
| 399 | case Telepath::STATUS_PARTIAL: |
||
| 400 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 401 | // keep FLR as a possible problem with original probability |
||
| 402 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_PARTIAL]; |
||
| 403 | break; |
||
| 404 | case Telepath::STATUS_DOWN: |
||
| 405 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["STATUS" => Telepath::STATUS_DOWN]; |
||
| 406 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 407 | // if earlier test found the server itself to be the problem, keep it, otherwise put the blame on the link |
||
| 408 | if ($this->probabilities[Telepath::INFRA_NRO_SP] != 0.95) { |
||
| 409 | $this->probabilities[Telepath::INFRA_LINK_ETLR_NRO_SP] = 0.95; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | // the last thing we can do (but it's a bit redundant): check the country-to-country link |
||
| 413 | // it's only needed if all three and their links are up, but we want to exclude funny routing blacklists |
||
| 414 | // which occur only in the *combination* of source and dest |
||
| 415 | // if there is an issue at that point, blame the SP: once a request would have reached the ETLRs, things would be all good. So they apparently didn't. |
||
| 416 | if (!in_array(Telepath::INFRA_ETLR, $this->possibleFailureReasons) && |
||
| 417 | !in_array(Telepath::INFRA_LINK_ETLR_NRO_IDP, $this->possibleFailureReasons) && |
||
| 418 | !in_array(Telepath::INFRA_NRO_IDP, $this->possibleFailureReasons) && |
||
| 419 | !in_array(Telepath::INFRA_LINK_ETLR_NRO_SP, $this->possibleFailureReasons) && |
||
| 420 | !in_array(Telepath::INFRA_NRO_SP, $this->possibleFailureReasons) |
||
| 421 | ) { |
||
| 422 | $countryToCountryStatus = $this->checkNROFlow($this->visitedFlr, $this->idPFederation); |
||
| 423 | switch ($countryToCountryStatus) { |
||
| 424 | case Telepath::STATUS_GOOD: |
||
| 425 | // all routes work |
||
| 426 | break; |
||
| 427 | case Telepath::STATUS_PARTIAL: |
||
| 428 | // at least one, or even all have a routing problem |
||
| 429 | case Telepath::STATUS_DOWN: |
||
| 430 | // that's rather telling. |
||
| 431 | $this->additionalFindings[Telepath::INFRA_NRO_SP][] = ["C2CLINK" => Telepath::STATUS_DOWN]; |
||
| 432 | $this->probabilities[Telepath::INFRA_NRO_SP] = 0.95; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | // done. return both the list of possible problem sources with their probabilities, and the additional findings we collected along the way. |
||
| 438 | $totalScores = 0.; |
||
| 439 | foreach ($this->possibleFailureReasons as $oneReason) { |
||
| 440 | $totalScores += $this->probabilities[$oneReason]; |
||
| 441 | } |
||
| 442 | $probArray = []; |
||
| 443 | foreach ($this->possibleFailureReasons as $oneReason) { |
||
| 444 | $probArray[$oneReason] = $this->probabilities[$oneReason] / $totalScores; |
||
| 445 | } |
||
| 446 | array_multisort($probArray, SORT_DESC, SORT_NUMERIC, $this->possibleFailureReasons); |
||
| 447 | |||
| 448 | return ["SUSPECTS" => $this->possibleFailureReasons, "PROBABILITIES" => $probArray, "EVIDENCE" => $this->additionalFindings]; |
||
| 449 | } |
||
| 450 | |||
| 452 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.