We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 20 |
Paths | 769 |
Total Lines | 80 |
Code Lines | 46 |
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 declare(strict_types=1); |
||
193 | protected function convertHtmlToAjaxXml(string $str, bool $returnXml): string { |
||
194 | if (empty($str)) { |
||
195 | return ''; |
||
196 | } |
||
197 | |||
198 | $session = Session::getInstance(); |
||
199 | |||
200 | $getInnerHTML = function(DOMNode $node): string { |
||
201 | $innerHTML = ''; |
||
202 | foreach ($node->childNodes as $child) { |
||
203 | $innerHTML .= $child->ownerDocument->saveHTML($child); |
||
204 | } |
||
205 | return $innerHTML; |
||
206 | }; |
||
207 | |||
208 | // Helper function to canonicalize making an XML element, |
||
209 | // with its inner content properly escaped. |
||
210 | $xmlify = function(string $id, string $str): string { |
||
211 | return '<' . $id . '>' . htmlspecialchars($str, ENT_XML1, 'utf-8') . '</' . $id . '>'; |
||
212 | }; |
||
213 | |||
214 | $xml = ''; |
||
215 | $dom = new DOMDocument(); |
||
216 | $dom->loadHTML($str); |
||
217 | $xpath = new DOMXPath($dom); |
||
218 | |||
219 | // Use relative xpath selectors so that they can be reused when we |
||
220 | // pass the middle panel as the xpath query's context node. |
||
221 | $ajaxSelectors = ['.//span[@id]', './/*[contains(@class,"ajax")]']; |
||
222 | |||
223 | foreach ($ajaxSelectors as $selector) { |
||
224 | $matchNodes = $xpath->query($selector); |
||
225 | foreach ($matchNodes as $node) { |
||
226 | $id = $node->getAttribute('id'); |
||
227 | $inner = $getInnerHTML($node); |
||
228 | if (!$session->addAjaxReturns($id, $inner) && $returnXml) { |
||
229 | $xml .= $xmlify($id, $inner); |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // Determine if we should do ajax updates on the middle panel div |
||
235 | $mid = $dom->getElementById('middle_panel'); |
||
236 | $doAjaxMiddle = true; |
||
237 | if ($mid === null) { |
||
238 | // Skip if there is no middle_panel. |
||
239 | $doAjaxMiddle = false; |
||
240 | } else { |
||
241 | // Skip if middle_panel has ajax-enabled children. |
||
242 | foreach ($ajaxSelectors as $selector) { |
||
243 | if (count($xpath->query($selector, $mid)) > 0) { |
||
244 | $doAjaxMiddle = false; |
||
245 | break; |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if ($doAjaxMiddle) { |
||
251 | $inner = $getInnerHTML($mid); |
||
252 | if (!$this->checkDisableAJAX($inner)) { |
||
253 | $id = $mid->getAttribute('id'); |
||
254 | if (!$session->addAjaxReturns($id, $inner) && $returnXml) { |
||
255 | $xml .= $xmlify($id, $inner); |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | $js = ''; |
||
261 | foreach ($this->ajaxJS as $varName => $JSON) { |
||
262 | if (!$session->addAjaxReturns('JS:' . $varName, $JSON) && $returnXml) { |
||
263 | $js .= $xmlify($varName, $JSON); |
||
264 | } |
||
265 | } |
||
266 | if ($returnXml && count($this->jsAlerts) > 0) { |
||
267 | $js = '<ALERT>' . json_encode($this->jsAlerts) . '</ALERT>'; |
||
268 | } |
||
269 | if (strlen($js) > 0) { |
||
270 | $xml .= '<JS>' . $js . '</JS>'; |
||
271 | } |
||
272 | return $xml; |
||
273 | } |
||
276 |