| Conditions | 9 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 |
||
| 24 | public static function getRegistry(): PBXApiResult |
||
| 25 | { |
||
| 26 | $res = new PBXApiResult(); |
||
| 27 | $res->processor = __METHOD__; |
||
| 28 | $peers = []; |
||
| 29 | $providers = Iax::find(); |
||
| 30 | foreach ($providers as $provider) { |
||
| 31 | $peers[] = [ |
||
| 32 | 'state' => 'OFF', |
||
| 33 | 'id' => $provider->uniqid, |
||
| 34 | 'username' => trim($provider->username), |
||
| 35 | 'host' => trim($provider->host), |
||
| 36 | 'noregister' => $provider->noregister, |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (Iax::findFirst("disabled = '0'") !== null) { |
||
| 41 | // Find them over AMI |
||
| 42 | $am = Util::getAstManager('off'); |
||
| 43 | $amiRegs = $am->IAXregistry(); // Registrations |
||
| 44 | $amiPeers = $am->IAXpeerlist(); // Peers |
||
| 45 | $am->Logoff(); |
||
| 46 | foreach ($amiPeers as $amiPeer) { |
||
| 47 | $key = array_search($amiPeer['ObjectName'], array_column($peers, 'id'), true); |
||
| 48 | if ($key !== false) { |
||
| 49 | $currentPeer = &$peers[$key]; |
||
| 50 | if ($currentPeer['noregister'] === '1') { |
||
| 51 | // Пир без регистрации. |
||
| 52 | $arr_status = explode(' ', $amiPeer['Status']); |
||
| 53 | $currentPeer['state'] = strtoupper($arr_status[0]); |
||
| 54 | $currentPeer['time-response'] = strtoupper(str_replace(['(', ')'], '', $arr_status[1])); |
||
| 55 | } else { |
||
| 56 | $currentPeer['state'] = 'Error register.'; |
||
| 57 | // Parse active registrations |
||
| 58 | foreach ($amiRegs as $reg) { |
||
| 59 | if ( |
||
| 60 | strcasecmp($reg['Addr'], $currentPeer['host']) === 0 |
||
| 61 | && strcasecmp($reg['Username'], $currentPeer['username']) === 0 |
||
| 62 | ) { |
||
| 63 | $currentPeer['state'] = $reg['State']; |
||
| 64 | break; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | $res->data = $peers; |
||
| 73 | $res->success = true; |
||
| 74 | |||
| 75 | return $res; |
||
| 76 | } |
||
| 77 | } |