| Total Complexity | 63 |
| Total Lines | 449 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ImportField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImportField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class ImportField extends UploadField |
||
| 55 | { |
||
| 56 | |||
| 57 | private static $allowed_actions = ['upload']; |
||
| 58 | |||
| 59 | private static $importer_class = ServiceConnector::class; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Process the document immediately upon upload. |
||
| 63 | */ |
||
| 64 | public function upload(HTTPRequest $request) |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Preserves the source file by copying it to a specified folder. |
||
| 124 | * |
||
| 125 | * @param $tmpfile Temporary file data structure. |
||
| 126 | * @param int $chosenFolderID Target folder. |
||
| 127 | * @return File Stored file. |
||
| 128 | */ |
||
| 129 | protected function preserveSourceDocument($tmpfile, $chosenFolderID = null) |
||
| 130 | { |
||
| 131 | $upload = Upload::create(); |
||
| 132 | |||
| 133 | $file = File::create(); |
||
| 134 | $upload->loadIntoFile($tmpfile, $file, $chosenFolderID); |
||
| 135 | |||
| 136 | $page = $this->form->getRecord(); |
||
| 137 | $page->ImportedFromFileID = $file->ID; |
||
| 138 | $page->write(); |
||
| 139 | |||
| 140 | return $file; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Builds and writes the table of contents for the document. |
||
| 145 | * |
||
| 146 | * @param bool $publishPage Should the parent page be published. |
||
| 147 | * @param File $preservedDocument Set if the link to the original document should be added. |
||
| 148 | */ |
||
| 149 | protected function writeTOC($publishPages = false, $preservedDocument = null) |
||
| 150 | { |
||
| 151 | $page = $this->form->getRecord(); |
||
| 152 | $content = '<ul>'; |
||
| 153 | |||
| 154 | if ($page) { |
||
| 155 | if ($page->Children()->Count() > 0) { |
||
| 156 | foreach ($page->Children() as $child) { |
||
| 157 | $content .= '<li><a href="' . $child->Link() . '">' . $child->Title . '</a></li>'; |
||
| 158 | } |
||
| 159 | $page->Content = $content . '</ul>'; |
||
| 160 | } else { |
||
| 161 | $doc = new DOMDocument(); |
||
| 162 | $doc->loadHTML($page->Content); |
||
| 163 | $body = $doc->getElementsByTagName('body')->item(0); |
||
| 164 | $node = $body->firstChild; |
||
| 165 | $h1 = $h2 = 1; |
||
| 166 | while ($node) { |
||
| 167 | if ($node instanceof DOMElement && $node->tagName == 'h1') { |
||
| 168 | $content .= '<li><a href="#h1.' . $h1 . '">' . |
||
| 169 | trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))) . |
||
| 170 | '</a></li>'; |
||
| 171 | $node->setAttributeNode(new DOMAttr("id", "h1.".$h1)); |
||
| 172 | $h1++; |
||
| 173 | } elseif ($node instanceof DOMElement && $node->tagName == 'h2') { |
||
| 174 | $content .= '<li class="menu-h2"><a href="#h2.' . $h2 . '">' . |
||
| 175 | trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))) . |
||
| 176 | '</a></li>'; |
||
| 177 | $node->setAttributeNode(new DOMAttr("id", "h2.".$h2)); |
||
| 178 | $h2++; |
||
| 179 | } |
||
| 180 | $node = $node->nextSibling; |
||
| 181 | } |
||
| 182 | $page->Content = $content . '</ul>' . $doc->saveHTML(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Add in the link to the original document, if provided. |
||
| 186 | if ($preservedDocument) { |
||
| 187 | $page->Content = '<a href="' . |
||
| 188 | $preservedDocument->Link() . |
||
| 189 | '" title="download original document">download original document (' . |
||
| 190 | $preservedDocument->getSize() . |
||
| 191 | ')</a>' . |
||
| 192 | $page->Content; |
||
| 193 | } |
||
| 194 | |||
| 195 | // Store the result |
||
| 196 | $page->write(); |
||
| 197 | if ($publishPages) { |
||
| 198 | $page->doPublish(); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function getBodyText($doc, $node) |
||
| 204 | { |
||
| 205 | // Build a new doc |
||
| 206 | $htmldoc = new DOMDocument(); |
||
| 207 | // Create the html element |
||
| 208 | $html = $htmldoc->createElement('html'); |
||
| 209 | $htmldoc->appendChild($html); |
||
| 210 | // Append the body node |
||
| 211 | $html->appendChild($htmldoc->importNode($node, true)); |
||
| 212 | |||
| 213 | // Get the text as html, remove the entry and exit root tags and return |
||
| 214 | $text = $htmldoc->saveHTML(); |
||
| 215 | $text = preg_replace('/^.*<body>/', '', $text); |
||
| 216 | $text = preg_replace('/<\/body>.*$/', '', $text); |
||
| 217 | |||
| 218 | return $text; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Used only when writing the document that has been split by headers. |
||
| 223 | * Can write both to the chapter pages as well as the master page. |
||
| 224 | * |
||
| 225 | * @param string $subtitle Title of the chapter - if missing, it will write to the master page. |
||
| 226 | * @param $subdoc |
||
| 227 | * @param $subnode |
||
| 228 | * @param int $sort Order of the chapter page. |
||
| 229 | * @param $publishPages Whether to publish the resulting child/master pages. |
||
| 230 | */ |
||
| 231 | protected function writeContent($subtitle, $subdoc, $subnode, $sort = null, $publishPages = false) |
||
| 232 | { |
||
| 233 | $record = $this->form->getRecord(); |
||
| 234 | |||
| 235 | if ($subtitle) { |
||
| 236 | // Write the chapter page to a subpage. |
||
| 237 | $page = DataObject::get_one( |
||
| 238 | 'Page', |
||
| 239 | sprintf('"Title" = \'%s\' AND "ParentID" = %d', $subtitle, $record->ID) |
||
| 240 | ); |
||
| 241 | if (!$page) { |
||
| 242 | $page = Page::create(); |
||
| 243 | $page->ParentID = $record->ID; |
||
| 244 | $page->Title = $subtitle; |
||
| 245 | } |
||
| 246 | |||
| 247 | unset($this->unusedChildren[$page->ID]); |
||
| 248 | file_put_contents(ASSETS_PATH . '/index-' . $sort . '.html', $this->getBodyText($subdoc, $subnode)); |
||
| 249 | |||
| 250 | if ($sort) { |
||
| 251 | $page->Sort = $sort; |
||
| 252 | } |
||
| 253 | $page->Content = $this->getBodyText($subdoc, $subnode); |
||
| 254 | $page->write(); |
||
| 255 | if ($publishPages) { |
||
| 256 | $page->doPublish(); |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | // Write to the master page. |
||
| 260 | $record->Content = $this->getBodyText($subdoc, $subnode); |
||
| 261 | $record->write(); |
||
| 262 | |||
| 263 | if ($publishPages) { |
||
| 264 | $record->doPublish(); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Imports a document at a certain path onto the current page and writes it. |
||
| 271 | * CAUTION: Overwrites any existing content on the page! |
||
| 272 | * |
||
| 273 | * @param array $tmpFile Array as received from PHP's POST upload. |
||
| 274 | * @param bool $splitHeader Heading level to split by. |
||
| 275 | * @param bool $publishPages Whether the underlying pages should be published after import. |
||
| 276 | * @param int $chosenFolderID ID of the working folder - here the converted file and images will be stored. |
||
| 277 | */ |
||
| 278 | public function importFromPOST($tmpFile, $splitHeader = false, $publishPages = false, $chosenFolderID = null) |
||
| 503 | } |
||
| 504 | } |
||
| 505 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths