| Conditions | 12 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 36 |
| 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 |
||
| 39 | public static function main(): PBXApiResult |
||
| 40 | { |
||
| 41 | $res = new PBXApiResult(); |
||
| 42 | $res->processor = __METHOD__; |
||
| 43 | |||
| 44 | try { |
||
| 45 | $res->success = true; |
||
| 46 | |||
| 47 | $filter = [ |
||
| 48 | 'endtime=""', |
||
| 49 | 'order' => 'id', |
||
| 50 | 'columns' => 'start,answer,src_chan,dst_chan,src_num,dst_num,did,linkedid', |
||
| 51 | 'miko_tmp_db' => true, |
||
| 52 | 'miko_result_in_file' => true, |
||
| 53 | ]; |
||
| 54 | $client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE); |
||
| 55 | list($result, $message) = $client->sendRequest(json_encode($filter), 2); |
||
| 56 | if ($result === false) { |
||
| 57 | $res->data = []; |
||
| 58 | } else { |
||
| 59 | $am = Util::getAstManager('off'); |
||
| 60 | $active_chans = $am->GetChannels(true); |
||
| 61 | $result_data = []; |
||
| 62 | |||
| 63 | $result = json_decode($message); |
||
| 64 | if (file_exists($result)) { |
||
| 65 | $data = json_decode(file_get_contents($result), true); |
||
| 66 | unlink($result); |
||
| 67 | foreach ($data as $row) { |
||
| 68 | if (!isset($active_chans[$row['linkedid']])) { |
||
| 69 | // The call no longer exists. |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | if (empty($row['dst_chan']) && empty($row['src_chan'])) { |
||
| 73 | // This is an erroneous situation. Ignore such a call. |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | $channels = $active_chans[$row['linkedid']]; |
||
| 77 | if ((empty($row['src_chan']) || in_array($row['src_chan'], $channels)) |
||
| 78 | && (empty($row['dst_chan']) || in_array($row['dst_chan'], $channels))) { |
||
| 79 | $result_data[] = $row; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | $res->data = $result_data; |
||
| 84 | } |
||
| 85 | } catch (\Throwable $e) { |
||
| 86 | $res->success = false; |
||
| 87 | $res->messages[] = $e->getMessage(); |
||
| 88 | } |
||
| 89 | return $res; |
||
| 90 | } |
||
| 91 | } |