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