| Conditions | 12 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 69 | public static function getActiveChannels(): PBXApiResult |
||
| 70 | { |
||
| 71 | $res = new PBXApiResult(); |
||
| 72 | $res->processor = __METHOD__; |
||
| 73 | |||
| 74 | try { |
||
| 75 | $res->success = true; |
||
| 76 | |||
| 77 | $filter = [ |
||
| 78 | 'endtime=""', |
||
| 79 | 'order' => 'id', |
||
| 80 | 'columns' => 'start,answer,src_chan,dst_chan,src_num,dst_num,did,linkedid', |
||
| 81 | 'miko_tmp_db' => true, |
||
| 82 | 'miko_result_in_file' => true, |
||
| 83 | ]; |
||
| 84 | $client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE); |
||
| 85 | $message = $client->request(json_encode($filter), 2); |
||
| 86 | if ($message === false) { |
||
| 87 | $res->data = []; |
||
| 88 | } else { |
||
| 89 | $am = Util::getAstManager('off'); |
||
| 90 | $active_chans = $am->GetChannels(true); |
||
| 91 | $result_data = []; |
||
| 92 | |||
| 93 | $result = json_decode($message); |
||
| 94 | if (file_exists($result)) { |
||
| 95 | $data = json_decode(file_get_contents($result), true); |
||
| 96 | unlink($result); |
||
| 97 | foreach ($data as $row) { |
||
| 98 | if (!isset($active_chans[$row['linkedid']])) { |
||
| 99 | // The call no longer exists. |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | if (empty($row['dst_chan']) && empty($row['src_chan'])) { |
||
| 103 | // This is an erroneous situation. Ignore such a call. |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | $channels = $active_chans[$row['linkedid']]; |
||
| 107 | if ((empty($row['src_chan']) || in_array($row['src_chan'], $channels)) |
||
| 108 | && (empty($row['dst_chan']) || in_array($row['dst_chan'], $channels))) { |
||
| 109 | $result_data[] = $row; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | $res->data = $result_data; |
||
| 114 | } |
||
| 115 | } catch (\Throwable $e) { |
||
| 116 | $res->success = false; |
||
| 117 | $res->messages[] = $e->getMessage(); |
||
| 118 | } |
||
| 119 | return $res; |
||
| 120 | } |
||
| 147 | } |