| Conditions | 7 |
| Paths | 10 |
| Total Lines | 55 |
| Code Lines | 37 |
| 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 |
||
| 81 | private function quoteHtml(string $newHtml, string $originalHtml, string $headerText): string |
||
| 82 | { |
||
| 83 | $originalHtml = \trim($originalHtml); |
||
| 84 | if ($originalHtml === '') { |
||
| 85 | return ''; |
||
| 86 | } |
||
| 87 | |||
| 88 | $document = new \DOMDocument(); |
||
| 89 | $document->substituteEntities = false; |
||
| 90 | $document->resolveExternals = false; |
||
| 91 | |||
| 92 | $result = @$document->loadHTML($originalHtml); |
||
| 93 | if ($result === false) { |
||
| 94 | throw new \DOMException('Incorrect HTML'); |
||
| 95 | } |
||
| 96 | |||
| 97 | $query = new \DOMXPath($document); |
||
| 98 | $removeItems = $query->query('//head|//script|//body/@style|//html/@style', $document->documentElement); |
||
| 99 | /** @var \DOMElement $removeItem */ |
||
| 100 | foreach ($removeItems as $removeItem) { |
||
| 101 | $parent = $removeItem->parentNode; |
||
| 102 | $parent->removeChild($removeItem); |
||
| 103 | } |
||
| 104 | |||
| 105 | $body = $document->getElementsByTagName('body'); |
||
| 106 | $quote = $document->createElement('blockquote'); |
||
| 107 | $quote->setAttribute('type', 'cite'); |
||
| 108 | |||
| 109 | if ($body->length === 0) { |
||
| 110 | $quote->appendChild($document->removeChild($document->documentElement)); |
||
| 111 | } else { |
||
| 112 | $root = $body->item(0); |
||
| 113 | while ($root->childNodes->length !== 0) { |
||
| 114 | $quote->appendChild($root->childNodes->item(0)); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | $newDocument = new \DOMDocument(); |
||
| 119 | $newDocument->substituteEntities = false; |
||
| 120 | $newDocument->resolveExternals = false; |
||
| 121 | $result = @$newDocument->loadHTML($newHtml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
||
| 122 | if ($result === false) { |
||
| 123 | throw new \DOMException('Incorrect HTML'); |
||
| 124 | } |
||
| 125 | |||
| 126 | $quotedNode = $newDocument->importNode($quote, true); |
||
| 127 | $newBody = $this->prepareBody($newDocument); |
||
| 128 | $newBody->appendChild($quotedNode); |
||
| 129 | |||
| 130 | $header = $newDocument->createElement('p'); |
||
| 131 | $header->textContent = $headerText; |
||
| 132 | |||
| 133 | $quotedNode->parentNode->insertBefore($header, $quotedNode); |
||
| 134 | return \trim($newDocument->saveHTML()); |
||
| 135 | } |
||
| 136 | |||
| 194 |