| Conditions | 12 |
| Paths | 22 |
| Total Lines | 47 |
| 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 |
||
| 38 | public static function main(): PBXApiResult |
||
| 39 | { |
||
| 40 | $res = new PBXApiResult(); |
||
| 41 | $res->processor = __METHOD__; |
||
| 42 | |||
| 43 | try { |
||
| 44 | $am = Util::getAstManager('off'); |
||
| 45 | $peers = $am->getPjSipRegistry(); |
||
| 46 | $providers = Sip::find("type = 'friend'"); |
||
| 47 | foreach ($providers as $provider) { |
||
| 48 | if ($provider->disabled === '1') { |
||
| 49 | $peers[] = [ |
||
| 50 | 'state' => 'OFF', |
||
| 51 | 'id' => $provider->uniqid, |
||
| 52 | 'username' => $provider->username, |
||
| 53 | 'host' => $provider->host, |
||
| 54 | ]; |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | if ($provider->registration_type === Sip::REG_TYPE_INBOUND || $provider->registration_type === Sip::REG_TYPE_NONE) { |
||
| 58 | $peers_status = $am->getPjSipPeer($provider->uniqid); |
||
| 59 | $peers[] = [ |
||
| 60 | 'state' => ($peers_status['state'] === 'OK' && $provider->registration_type === Sip::REG_TYPE_INBOUND) ? 'REGISTERED' : $peers_status['state'], |
||
| 61 | 'id' => $provider->uniqid, |
||
| 62 | 'username' => $provider->username, |
||
| 63 | 'host' => $provider->host, |
||
| 64 | ]; |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | foreach ($peers as &$peer) { |
||
| 68 | if (!empty($peer['id'])) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | if ($peer['host'] !== $provider->host || $peer['username'] !== $provider->username) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | $peer['id'] = $provider->uniqid; |
||
| 75 | } |
||
| 76 | unset($peer); |
||
| 77 | } |
||
| 78 | $res->data = $peers; |
||
| 79 | $res->success = true; |
||
| 80 | } catch (\Throwable $e) { |
||
| 81 | $res->success = false; |
||
| 82 | $res->messages[] = $e->getMessage(); |
||
| 83 | } |
||
| 84 | return $res; |
||
| 85 | } |
||
| 86 | } |