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