Conditions | 12 |
Paths | 968 |
Total Lines | 77 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
87 | public function prepareAnswer(BeanstalkClient $message): void |
||
88 | { |
||
89 | // Use fork to run the callback in a separate process |
||
90 | $pid = pcntl_fork(); |
||
91 | if ($pid === -1) { |
||
92 | // Fork failed |
||
93 | throw new \RuntimeException("Failed to fork a new process."); |
||
94 | } |
||
95 | if ($pid === 0) { |
||
96 | $res = new PBXApiResult(); |
||
97 | $res->processor = __METHOD__; |
||
98 | $async = false; |
||
99 | try { |
||
100 | $request = json_decode($message->getBody(), true, 512, JSON_THROW_ON_ERROR); |
||
101 | $async = ($request['async'] ?? false) === true; |
||
102 | $processor = $request['processor']; |
||
103 | $res->processor = $processor; |
||
104 | // Old style, we can remove it in 2025 |
||
105 | if ($processor === 'modules') { |
||
106 | $processor = PbxExtensionsProcessor::class; |
||
107 | } |
||
108 | |||
109 | $this->breakpointHere($request); |
||
110 | |||
111 | // This is the child process |
||
112 | if (method_exists($processor, 'callback')) { |
||
113 | cli_set_process_title(__CLASS__ . '-' . $request['action']); |
||
114 | // Execute async job |
||
115 | if ($async) { |
||
116 | $res->success = true; |
||
117 | $res->messages['info'][] = "The async job {$request['action']} starts in background, you will receive answer on {$request['asyncChannelId']} nchan channel"; |
||
118 | $encodedResult = json_encode($res->getResult()); |
||
119 | $message->reply($encodedResult); |
||
120 | $processor::callback($request); |
||
121 | } else { |
||
122 | $res = $processor::callback($request); |
||
123 | } |
||
124 | } else { |
||
125 | $res->success = false; |
||
126 | $res->messages['error'][] = "Unknown processor - $processor in prepareAnswer"; |
||
127 | } |
||
128 | } catch (Throwable $exception) { |
||
129 | $request = []; |
||
130 | $this->needRestart = true; |
||
131 | // Prepare answer with pretty error description |
||
132 | $res->messages['error'][] = CriticalErrorsHandler::handleExceptionWithSyslog($exception); |
||
133 | } finally { |
||
134 | if ($async === false) { |
||
135 | $encodedResult = json_encode($res->getResult()); |
||
136 | if ($encodedResult === false) { |
||
137 | $res->data = []; |
||
138 | $res->messages['error'][] = 'It is impossible to encode to json current processor answer'; |
||
139 | $encodedResult = json_encode($res->getResult()); |
||
140 | } |
||
141 | |||
142 | // Check the response size and write in on file if it bigger than Beanstalk can digest |
||
143 | if (strlen($encodedResult) > BeanstalkConf::JOB_DATA_SIZE_LIMIT) { |
||
144 | $downloadCacheDir = Directories::getDir(Directories::WWW_DOWNLOAD_CACHE_DIR); |
||
145 | $filenameTmp = $downloadCacheDir . '/temp-' . __FUNCTION__ . '_' . microtime() . '.data'; |
||
146 | if (file_put_contents($filenameTmp, serialize($res->getResult()))) { |
||
147 | $encodedResult = json_encode([BeanstalkClient::RESPONSE_IN_FILE => $filenameTmp]); |
||
148 | } else { |
||
149 | $res->data = []; |
||
150 | $res->messages['error'][] = 'It is impossible to write answer into file ' . $filenameTmp; |
||
151 | $encodedResult = json_encode($res->getResult()); |
||
152 | } |
||
153 | } |
||
154 | $message->reply($encodedResult); |
||
155 | } |
||
156 | if ($res->success) { |
||
157 | $this->checkNeedReload($request); |
||
158 | } |
||
159 | } |
||
160 | exit(0); // Exit the child process |
||
|
|||
161 | } |
||
162 | // This is the parent process |
||
163 | pcntl_wait($status); // Wait for the child process to complete |
||
164 | } |
||
247 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.