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