| Conditions | 11 |
| Paths | 45 |
| Total Lines | 64 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 38 | public static function main(): PBXApiResult |
||
| 39 | { |
||
| 40 | $res = new PBXApiResult(); |
||
| 41 | $res->processor = __METHOD__; |
||
| 42 | |||
| 43 | |||
| 44 | try { |
||
| 45 | $peers = []; |
||
| 46 | $providers = Iax::find(); |
||
| 47 | foreach ($providers as $provider) { |
||
| 48 | $peers[] = [ |
||
| 49 | 'state' => 'OFF', |
||
| 50 | 'id' => $provider->uniqid, |
||
| 51 | 'username' => trim($provider->username), |
||
| 52 | 'host' => trim($provider->host), |
||
| 53 | 'noregister' => $provider->noregister, |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (Iax::findFirst("disabled = '0'") !== null) { |
||
| 58 | // Find them over AMI |
||
| 59 | $am = Util::getAstManager('off'); |
||
| 60 | $amiRegs = $am->IAXregistry(); // Registrations |
||
| 61 | $amiPeers = $am->IAXpeerlist(); // Peers |
||
| 62 | foreach ($amiPeers as $amiPeer) { |
||
| 63 | $key = array_search($amiPeer['ObjectName'], array_column($peers, 'id'), true); |
||
| 64 | if ($key !== false) { |
||
| 65 | $currentPeer = &$peers[$key]; |
||
| 66 | if ($currentPeer['noregister'] === '1') { |
||
| 67 | // Peer without registration. |
||
| 68 | $arr_status = explode(' ', $amiPeer['Status']); |
||
| 69 | $currentPeer['state'] = strtoupper($arr_status[0]); |
||
| 70 | // Check if the expected index exists before trying to access it. |
||
| 71 | if (isset($arr_status[1])) { |
||
| 72 | $currentPeer['time-response'] = strtoupper(str_replace(['(', ')'], '', $arr_status[1])); |
||
| 73 | } else { |
||
| 74 | // Handle the case where $arr_status[1] is not set. |
||
| 75 | // You might want to assign a default value or handle this scenario appropriately. |
||
| 76 | $currentPeer['time-response'] = 'N/A'; |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | $currentPeer['state'] = 'Error register.'; |
||
| 80 | // Parse active registrations |
||
| 81 | foreach ($amiRegs as $reg) { |
||
| 82 | if ( |
||
| 83 | strcasecmp($reg['Addr'], $currentPeer['host']) === 0 |
||
| 84 | && strcasecmp($reg['Username'], $currentPeer['username']) === 0 |
||
| 85 | ) { |
||
| 86 | $currentPeer['state'] = $reg['State']; |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $res->data = $peers; |
||
| 96 | $res->success = true; |
||
| 97 | } catch (\Throwable $e) { |
||
| 98 | $res->success = false; |
||
| 99 | $res->messages[] = $e->getMessage(); |
||
| 100 | } |
||
| 101 | return $res; |
||
| 102 | } |
||
| 103 | } |