Code Duplication    Length = 44-45 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

@@ 162-206 (lines=45) @@
159
     *
160
     * @return SimpleHtmlDomInterface
161
     */
162
    protected function replaceChildWithString(string $string): SimpleHtmlDomInterface
163
    {
164
        if (!empty($string)) {
165
            $newDocument = new HtmlDomParser($string);
166
167
            $tmpDomString = $this->normalizeStringForComparision($newDocument);
168
            $tmpStr = $this->normalizeStringForComparision($string);
169
            if ($tmpDomString !== $tmpStr) {
170
                throw new \RuntimeException(
171
                    'Not valid HTML fragment!' . "\n" .
172
                    $tmpDomString . "\n" .
173
                    $tmpStr
174
                );
175
            }
176
        }
177
178
        /** @var \DOMNode[] $remove_nodes */
179
        $remove_nodes = [];
180
        if ($this->node->childNodes->length > 0) {
181
            // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom,
182
            // if we delete the elements on the fly.
183
            foreach ($this->node->childNodes as $node) {
184
                $remove_nodes[] = $node;
185
            }
186
        }
187
        foreach ($remove_nodes as $remove_node) {
188
            $this->node->removeChild($remove_node);
189
        }
190
191
        if (!empty($newDocument)) {
192
            $newDocument = $this->cleanHtmlWrapper($newDocument);
193
            $ownerDocument = $this->node->ownerDocument;
194
            if (
195
                $ownerDocument !== null
196
                &&
197
                $newDocument->getDocument()->documentElement !== null
198
            ) {
199
                $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true);
200
                /** @noinspection UnusedFunctionResultInspection */
201
                $this->node->appendChild($newNode);
202
            }
203
        }
204
205
        return $this;
206
    }
207
208
    /**
209
     * Replace this node.

src/voku/helper/SimpleXmlDom.php 1 location

@@ 150-193 (lines=44) @@
147
     *
148
     * @return SimpleXmlDomInterface
149
     */
150
    protected function replaceChildWithString(string $string): SimpleXmlDomInterface
151
    {
152
        if (!empty($string)) {
153
            $newDocument = new XmlDomParser($string);
154
155
            $tmpDomString = $this->normalizeStringForComparision($newDocument);
156
            $tmpStr = $this->normalizeStringForComparision($string);
157
            if ($tmpDomString !== $tmpStr) {
158
                throw new \RuntimeException(
159
                    'Not valid HTML fragment!' . "\n" .
160
                    $tmpDomString . "\n" .
161
                    $tmpStr
162
                );
163
            }
164
        }
165
166
        /** @var \DOMNode[] $remove_nodes */
167
        $remove_nodes = [];
168
        if ($this->node->childNodes->length > 0) {
169
            // INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom,
170
            // if we delete the elements on the fly.
171
            foreach ($this->node->childNodes as $node) {
172
                $remove_nodes[] = $node;
173
            }
174
        }
175
        foreach ($remove_nodes as $remove_node) {
176
            $this->node->removeChild($remove_node);
177
        }
178
179
        if (!empty($newDocument)) {
180
            $ownerDocument = $this->node->ownerDocument;
181
            if (
182
                $ownerDocument !== null
183
                &&
184
                $newDocument->getDocument()->documentElement !== null
185
            ) {
186
                $newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true);
187
                /** @noinspection UnusedFunctionResultInspection */
188
                $this->node->appendChild($newNode);
189
            }
190
        }
191
192
        return $this;
193
    }
194
195
    /**
196
     * Replace this node.