Conditions | 12 |
Paths | 410 |
Total Lines | 62 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
84 | public function prepareAnswer(BeanstalkClient $message): void |
||
85 | { |
||
86 | // Use fork to run the callback in a separate process |
||
87 | $pid = pcntl_fork(); |
||
88 | if ($pid == -1) { |
||
89 | // Fork failed |
||
90 | throw new \RuntimeException("Failed to fork a new process."); |
||
91 | } elseif ($pid == 0) { |
||
92 | $res = new PBXApiResult(); |
||
93 | $res->processor = __METHOD__; |
||
94 | try { |
||
95 | $request = json_decode($message->getBody(), true, 512, JSON_THROW_ON_ERROR); |
||
96 | $processor = $request['processor']; |
||
97 | $res->processor = $processor; |
||
98 | // Start xdebug session, don't forget to install xdebug.remote_mode = jit on xdebug.ini |
||
99 | // and set XDEBUG_SESSION Cookie header on REST request to debug it |
||
100 | if (isset($request['debug']) && $request['debug'] === true && extension_loaded('xdebug')) { |
||
101 | xdebug_break(); |
||
102 | } |
||
103 | |||
104 | // This is the child process |
||
105 | if (method_exists($processor, 'callback')) { |
||
106 | $res = $processor::callback($request); |
||
107 | } else { |
||
108 | $res->success = false; |
||
109 | $res->messages['error'][] = "Unknown processor - {$processor} in prepareAnswer"; |
||
110 | } |
||
111 | } catch (Throwable $exception) { |
||
112 | $request = []; |
||
113 | $this->needRestart = true; |
||
114 | // Prepare answer with pretty error description |
||
115 | $res->messages['error'][] = CriticalErrorsHandler::handleExceptionWithSyslog($exception); |
||
116 | } finally { |
||
117 | $encodedResult = json_encode($res->getResult()); |
||
118 | if ($encodedResult === false) { |
||
119 | $res->data = []; |
||
120 | $res->messages['error'][] = 'It is impossible to encode to json current processor answer'; |
||
121 | $encodedResult = json_encode($res->getResult()); |
||
122 | } |
||
123 | |||
124 | // Check response size and write in on file if it bigger than Beanstalk can digest |
||
125 | if (strlen($encodedResult)>BeanstalkConf::JOB_DATA_SIZE_LIMIT){ |
||
126 | $dirsConfig = $this->di->getShared('config'); |
||
127 | $filenameTmp = $dirsConfig->path('www.downloadCacheDir') . '/temp-' . __FUNCTION__ . '_' . microtime() . '.data'; |
||
128 | if (file_put_contents($filenameTmp, serialize($res->getResult()))){ |
||
129 | $encodedResult = json_encode([BeanstalkClient::RESPONSE_IN_FILE=>$filenameTmp]); |
||
130 | } else { |
||
131 | $res->data = []; |
||
132 | $res->messages['error'][] = 'It is impossible to write answer into file '.$filenameTmp; |
||
133 | $encodedResult = json_encode($res->getResult()); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $message->reply($encodedResult); |
||
138 | if ($res->success) { |
||
139 | $this->checkNeedReload($request); |
||
140 | } |
||
141 | } |
||
142 | exit(0); // Exit the child process |
||
|
|||
143 | } else { |
||
144 | // This is the parent process |
||
145 | pcntl_wait($status); // Wait for the child process to complete |
||
146 | } |
||
192 | WorkerApiCommands::startWorker($argv ?? []); |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.