| Conditions | 33 |
| Paths | > 20000 |
| Total Lines | 190 |
| Code Lines | 114 |
| 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 |
||
| 243 | public function importFromPOST($tmpFile, $splitHeader = false, $publishPages = false, $chosenFolderID = null) { |
||
| 244 | |||
| 245 | $fileDescriptor = array( |
||
| 246 | 'name' => $tmpFile['name'], |
||
| 247 | 'path' => $tmpFile['tmp_name'], |
||
| 248 | 'mimeType' => $tmpFile['type'] |
||
| 249 | ); |
||
| 250 | |||
| 251 | $sourcePage = $this->form->getRecord(); |
||
| 252 | $importerClass = Config::inst()->get(__CLASS__, 'importer_class'); |
||
| 253 | $importer = Injector::inst()->create($importerClass, $fileDescriptor, $chosenFolderID); |
||
| 254 | $content = $importer->import(); |
||
| 255 | |||
| 256 | if (is_array($content) && isset($content['error'])) { |
||
| 257 | return $content; |
||
| 258 | } |
||
| 259 | |||
| 260 | // Clean up with tidy (requires tidy module) |
||
| 261 | $tidy = new Tidy(); |
||
| 262 | $tidy->parseString($content, array('output-xhtml' => true), 'utf8'); |
||
| 263 | $tidy->cleanRepair(); |
||
| 264 | |||
| 265 | $fragment = []; |
||
| 266 | foreach($tidy->body()->child as $child) { |
||
| 267 | $fragment[] = $child->value; |
||
| 268 | } |
||
| 269 | |||
| 270 | $htmlValue = Injector::inst()->create('HTMLValue', implode("\n", $fragment)); |
||
| 271 | |||
| 272 | // Sanitise |
||
| 273 | $santiser = Injector::inst()->create('HtmlEditorSanitiser', HtmlEditorConfig::get_active()); |
||
| 274 | $santiser->sanitise($htmlValue); |
||
| 275 | |||
| 276 | // Load in the HTML |
||
| 277 | $doc = $htmlValue->getDocument(); |
||
| 278 | $xpath = new DOMXPath($doc); |
||
| 279 | |||
| 280 | // make sure any images are added as Image records with a relative link to assets |
||
| 281 | $chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
||
| 282 | $folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
||
| 283 | $imgs = $xpath->query('//img'); |
||
| 284 | for($i = 0; $i < $imgs->length; $i++) { |
||
| 285 | $img = $imgs->item($i); |
||
| 286 | $originalPath = 'assets/' . $folderName . '/' . $img->getAttribute('src'); |
||
| 287 | $name = FileNameFilter::create()->filter(basename($originalPath)); |
||
| 288 | |||
| 289 | $image = Image::get()->filter(array('Name' => $name, 'ParentID' => (int) $chosenFolderID))->first(); |
||
| 290 | if(!($image && $image->exists())) { |
||
| 291 | $image = Image::create(); |
||
| 292 | $image->ParentID = (int) $chosenFolderID; |
||
| 293 | $image->Name = $name; |
||
| 294 | $image->write(); |
||
| 295 | } |
||
| 296 | |||
| 297 | // make sure it's put in place correctly so Image record knows where it is. |
||
| 298 | // e.g. in the case of underscores being renamed to dashes. |
||
| 299 | @rename(Director::getAbsFile($originalPath), Director::getAbsFile($image->getFilename())); |
||
| 300 | |||
| 301 | $img->setAttribute('src', $image->getFilename()); |
||
| 302 | } |
||
| 303 | |||
| 304 | $remove_rules = array( |
||
| 305 | '//h1[.//font[not(@face)]]' => 'p', // Change any headers that contain font tags (other than font face tags) into p elements |
||
| 306 | '//font' // Remove any font tags |
||
| 307 | ); |
||
| 308 | |||
| 309 | foreach($remove_rules as $rule => $parenttag) { |
||
| 310 | if(is_numeric($rule)) { |
||
| 311 | $rule = $parenttag; |
||
| 312 | $parenttag = null; |
||
| 313 | } |
||
| 314 | |||
| 315 | $nodes = array(); |
||
| 316 | foreach($xpath->query($rule) as $node) $nodes[] = $node; |
||
| 317 | |||
| 318 | foreach($nodes as $node) { |
||
| 319 | $parent = $node->parentNode; |
||
| 320 | |||
| 321 | if($parenttag) { |
||
| 322 | $parent = $doc->createElement($parenttag); |
||
| 323 | $node->nextSibling ? $node->parentNode->insertBefore($parent, $node->nextSibling) : $node->parentNode->appendChild($parent); |
||
| 324 | } |
||
| 325 | |||
| 326 | while($node->firstChild) $parent->appendChild($node->firstChild); |
||
| 327 | $node->parentNode->removeChild($node); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | // Strip style, class, lang attributes. |
||
| 332 | $els = $doc->getElementsByTagName('*'); |
||
| 333 | for ($i = 0; $i < $els->length; $i++) { |
||
| 334 | $el = $els->item($i); |
||
| 335 | $el->removeAttribute('class'); |
||
| 336 | $el->removeAttribute('style'); |
||
| 337 | $el->removeAttribute('lang'); |
||
| 338 | } |
||
| 339 | |||
| 340 | $els = $doc->getElementsByTagName('*'); |
||
| 341 | |||
| 342 | // Remove a bunch of unwanted elements |
||
| 343 | $clean = array( |
||
| 344 | '//p[not(descendant-or-self::text() | descendant-or-self::img)]', // Empty paragraphs |
||
| 345 | '//*[self::h1 | self::h2 | self::h3 | self::h4 | self::h5 | self::h6][not(descendant-or-self::text() | descendant-or-self::img)]', // Empty headers |
||
| 346 | '//a[not(@href)]', // Anchors |
||
| 347 | '//br' // BR tags |
||
| 348 | ); |
||
| 349 | |||
| 350 | foreach($clean as $query) { |
||
| 351 | // First get all the nodes. Need to build array, as they'll disappear from the nodelist while we're deleteing them, causing the indexing |
||
| 352 | // to screw up. |
||
| 353 | $nodes = array(); |
||
| 354 | foreach($xpath->query($query) as $node) $nodes[] = $node; |
||
| 355 | |||
| 356 | // Then remove them all |
||
| 357 | foreach ($nodes as $node) { if ($node->parentNode) $node->parentNode->removeChild($node); } |
||
| 358 | } |
||
| 359 | |||
| 360 | // Now split the document into portions by H1 |
||
| 361 | $body = $doc->getElementsByTagName('body')->item(0); |
||
| 362 | |||
| 363 | $this->unusedChildren = array(); |
||
| 364 | foreach($sourcePage->Children() as $child) { |
||
| 365 | $this->unusedChildren[$child->ID] = $child; |
||
| 366 | } |
||
| 367 | |||
| 368 | $documentImporterFieldError; |
||
| 369 | |||
| 370 | $documentImporterFieldErrorHandler = function ($errno, $errstr, $errfile, $errline) use ( $documentImporterFieldError ) { |
||
| 371 | $documentImporterFieldError = _t( |
||
| 372 | 'SilverStripe\\DocumentConverter\\DocumentConverter.PROCESSFAILED', |
||
| 373 | 'Could not process document, please double-check you uploaded a .doc or .docx format.', |
||
| 374 | 'Document Converter processes Word documents into HTML.' |
||
| 375 | ); |
||
| 376 | |||
| 377 | // Do not cascade the error through other handlers |
||
| 378 | return true; |
||
| 379 | }; |
||
| 380 | |||
| 381 | set_error_handler($documentImporterFieldErrorHandler); |
||
| 382 | |||
| 383 | $subtitle = null; |
||
| 384 | $subdoc = new DOMDocument(); |
||
| 385 | $subnode = $subdoc->createElement('body'); |
||
| 386 | $node = $body->firstChild; |
||
| 387 | $sort = 1; |
||
| 388 | if($splitHeader == 1 || $splitHeader == 2) { |
||
| 389 | while($node && !$documentImporterFieldError) { |
||
| 390 | if($node instanceof DOMElement && $node->tagName == 'h' . $splitHeader) { |
||
| 391 | if($subnode->hasChildNodes()) { |
||
| 392 | $this->writeContent($subtitle, $subdoc, $subnode, $sort, $publishPages); |
||
| 393 | $sort++; |
||
| 394 | } |
||
| 395 | |||
| 396 | $subdoc = new DOMDocument(); |
||
| 397 | $subnode = $subdoc->createElement('body'); |
||
| 398 | $subtitle = trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))); |
||
| 399 | } else { |
||
| 400 | $subnode->appendChild($subdoc->importNode($node, true)); |
||
| 401 | } |
||
| 402 | |||
| 403 | $node = $node->nextSibling; |
||
| 404 | } |
||
| 405 | } else { |
||
| 406 | $this->writeContent($subtitle, $subdoc, $body, null, $publishPages); |
||
| 407 | } |
||
| 408 | |||
| 409 | if($subnode->hasChildNodes() && !$documentImporterFieldError) { |
||
| 410 | $this->writeContent($subtitle, $subdoc, $subnode, null, $publishPages); |
||
| 411 | } |
||
| 412 | |||
| 413 | restore_error_handler(); |
||
| 414 | if ($documentImporterFieldError) { |
||
| 415 | return array('error' => $documentImporterFieldError); |
||
| 416 | } |
||
| 417 | |||
| 418 | foreach($this->unusedChildren as $child) { |
||
| 419 | $origStage = Versioned::current_stage(); |
||
| 420 | |||
| 421 | Versioned::reading_stage('Stage'); |
||
| 422 | $clone = clone $child; |
||
| 423 | $clone->delete(); |
||
| 424 | |||
| 425 | Versioned::reading_stage('Live'); |
||
| 426 | $clone = clone $child; |
||
| 427 | $clone->delete(); |
||
| 428 | |||
| 429 | Versioned::reading_stage($origStage); |
||
| 430 | } |
||
| 431 | |||
| 432 | $sourcePage->write(); |
||
| 433 | } |
||
| 435 |
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