| Conditions | 33 |
| Paths | > 20000 |
| Total Lines | 225 |
| Code Lines | 136 |
| 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 |
||
| 278 | public function importFromPOST($tmpFile, $splitHeader = false, $publishPages = false, $chosenFolderID = null) |
||
| 279 | { |
||
| 280 | |||
| 281 | $fileDescriptor = [ |
||
| 282 | 'name' => $tmpFile['name'], |
||
| 283 | 'path' => $tmpFile['tmp_name'], |
||
| 284 | 'mimeType' => $tmpFile['type'] |
||
| 285 | ]; |
||
| 286 | |||
| 287 | $sourcePage = $this->form->getRecord(); |
||
| 288 | $importerClass = $this->config()->get('importer_class'); |
||
| 289 | $importer = Injector::inst()->create($importerClass, $fileDescriptor, $chosenFolderID); |
||
| 290 | $content = $importer->import(); |
||
| 291 | |||
| 292 | if (is_array($content) && isset($content['error'])) { |
||
| 293 | return $content; |
||
| 294 | } |
||
| 295 | |||
| 296 | // Clean up with tidy (requires tidy module) |
||
| 297 | $tidy = new Tidy(); |
||
| 298 | $tidy->parseString($content, ['output-xhtml' => true], 'utf8'); |
||
| 299 | $tidy->cleanRepair(); |
||
| 300 | |||
| 301 | $fragment = []; |
||
| 302 | foreach ($tidy->body()->child as $child) { |
||
| 303 | $fragment[] = $child->value; |
||
| 304 | } |
||
| 305 | |||
| 306 | $htmlValue = Injector::inst()->create(HTMLValue::class, implode("\n", $fragment)); |
||
| 307 | |||
| 308 | // Sanitise |
||
| 309 | $santiser = Injector::inst()->create(HTMLEditorSanitiser::class, HTMLEditorConfig::get_active()); |
||
| 310 | $santiser->sanitise($htmlValue); |
||
| 311 | |||
| 312 | // Load in the HTML |
||
| 313 | $doc = $htmlValue->getDocument(); |
||
| 314 | $xpath = new DOMXPath($doc); |
||
| 315 | |||
| 316 | // make sure any images are added as Image records with a relative link to assets |
||
| 317 | $chosenFolder = ($this->chosenFolderID) ? DataObject::get_by_id(Folder::class, $this->chosenFolderID) : null; |
||
| 318 | $folderName = ($chosenFolder) ? '/' . $chosenFolder->Name : ''; |
||
| 319 | $imgs = $xpath->query('//img'); |
||
| 320 | for ($i = 0; $i < $imgs->length; $i++) { |
||
| 321 | $img = $imgs->item($i); |
||
| 322 | $originalPath = 'assets/' . $folderName . '/' . $img->getAttribute('src'); |
||
| 323 | $name = FileNameFilter::create()->filter(basename($originalPath)); |
||
| 324 | |||
| 325 | $image = Image::get()->filter([ |
||
| 326 | 'Name' => $name, |
||
| 327 | 'ParentID' => (int)$chosenFolderID |
||
| 328 | ])->first(); |
||
| 329 | if (!($image && $image->exists())) { |
||
| 330 | $image = Image::create(); |
||
| 331 | $image->ParentID = (int)$chosenFolderID; |
||
| 332 | $image->Name = $name; |
||
| 333 | $image->write(); |
||
| 334 | } |
||
| 335 | |||
| 336 | // make sure it's put in place correctly so Image record knows where it is. |
||
| 337 | // e.g. in the case of underscores being renamed to dashes. |
||
| 338 | @rename(Director::getAbsFile($originalPath), Director::getAbsFile($image->getFilename())); |
||
| 339 | |||
| 340 | $img->setAttribute('src', $image->getFilename()); |
||
| 341 | } |
||
| 342 | |||
| 343 | $remove_rules = [ |
||
| 344 | // Change any headers that contain font tags (other than font face tags) into p elements |
||
| 345 | '//h1[.//font[not(@face)]]' => 'p', |
||
| 346 | // Remove any font tags |
||
| 347 | '//font' |
||
| 348 | ]; |
||
| 349 | |||
| 350 | foreach ($remove_rules as $rule => $parenttag) { |
||
| 351 | if (is_numeric($rule)) { |
||
| 352 | $rule = $parenttag; |
||
| 353 | $parenttag = null; |
||
| 354 | } |
||
| 355 | |||
| 356 | $nodes = []; |
||
| 357 | foreach ($xpath->query($rule) as $node) { |
||
| 358 | $nodes[] = $node; |
||
| 359 | } |
||
| 360 | |||
| 361 | foreach ($nodes as $node) { |
||
| 362 | $parent = $node->parentNode; |
||
| 363 | |||
| 364 | if ($parenttag) { |
||
| 365 | $parent = $doc->createElement($parenttag); |
||
| 366 | $node->nextSibling ? |
||
| 367 | $node->parentNode->insertBefore($parent, $node->nextSibling) : |
||
| 368 | $node->parentNode->appendChild($parent); |
||
| 369 | } |
||
| 370 | |||
| 371 | while ($node->firstChild) { |
||
| 372 | $parent->appendChild($node->firstChild); |
||
| 373 | } |
||
| 374 | $node->parentNode->removeChild($node); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | // Strip style, class, lang attributes. |
||
| 379 | $els = $doc->getElementsByTagName('*'); |
||
| 380 | for ($i = 0; $i < $els->length; $i++) { |
||
| 381 | $el = $els->item($i); |
||
| 382 | $el->removeAttribute('class'); |
||
| 383 | $el->removeAttribute('style'); |
||
| 384 | $el->removeAttribute('lang'); |
||
| 385 | } |
||
| 386 | |||
| 387 | $els = $doc->getElementsByTagName('*'); |
||
| 388 | |||
| 389 | $headingXPath = [ |
||
| 390 | 'self::h1', |
||
| 391 | 'self::h2', |
||
| 392 | 'self::h3', |
||
| 393 | 'self::h4', |
||
| 394 | 'self::h5', |
||
| 395 | 'self::h6', |
||
| 396 | ]; |
||
| 397 | // Remove a bunch of unwanted elements |
||
| 398 | $clean = [ |
||
| 399 | // Empty paragraphs |
||
| 400 | '//p[not(descendant-or-self::text() | descendant-or-self::img)]', |
||
| 401 | // Empty headers |
||
| 402 | '//*[' . implode(' | ', $headingXPath) . '][not(descendant-or-self::text() | descendant-or-self::img)]', |
||
| 403 | // Anchors |
||
| 404 | '//a[not(@href)]', |
||
| 405 | // BR tags |
||
| 406 | '//br' |
||
| 407 | ]; |
||
| 408 | |||
| 409 | foreach ($clean as $query) { |
||
| 410 | // First get all the nodes. Need to build array, as they'll disappear from the |
||
| 411 | // nodelist while we're deleteing them, causing the indexing to screw up. |
||
| 412 | $nodes = []; |
||
| 413 | foreach ($xpath->query($query) as $node) { |
||
| 414 | $nodes[] = $node; |
||
| 415 | } |
||
| 416 | |||
| 417 | // Then remove them all |
||
| 418 | foreach ($nodes as $node) { |
||
| 419 | if ($node->parentNode) { |
||
| 420 | $node->parentNode->removeChild($node); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | // Now split the document into portions by H1 |
||
| 426 | $body = $doc->getElementsByTagName('body')->item(0); |
||
| 427 | |||
| 428 | $this->unusedChildren = []; |
||
| 429 | foreach ($sourcePage->Children() as $child) { |
||
| 430 | $this->unusedChildren[$child->ID] = $child; |
||
| 431 | } |
||
| 432 | |||
| 433 | $documentImporterFieldError = false; |
||
| 434 | |||
| 435 | $documentImporterFieldErrorHandler = function ( |
||
| 436 | $errno, |
||
| 437 | $errstr, |
||
| 438 | $errfile, |
||
| 439 | $errline |
||
| 440 | ) use ($documentImporterFieldError) { |
||
| 441 | $documentImporterFieldError = _t( |
||
| 442 | 'SilverStripe\\DocumentConverter\\ServiceConnector.PROCESSFAILED', |
||
| 443 | 'Could not process document, please double-check you uploaded a .doc or .docx format.', |
||
| 444 | 'Document Converter processes Word documents into HTML.' |
||
| 445 | ); |
||
| 446 | |||
| 447 | // Do not cascade the error through other handlers |
||
| 448 | return true; |
||
| 449 | }; |
||
| 450 | |||
| 451 | set_error_handler($documentImporterFieldErrorHandler); |
||
| 452 | |||
| 453 | $subtitle = null; |
||
| 454 | $subdoc = new DOMDocument(); |
||
| 455 | $subnode = $subdoc->createElement('body'); |
||
| 456 | $node = $body->firstChild; |
||
| 457 | $sort = 1; |
||
| 458 | if ($splitHeader == 1 || $splitHeader == 2) { |
||
| 459 | while ($node && !$documentImporterFieldError) { |
||
| 460 | if ($node instanceof DOMElement && $node->tagName == 'h' . $splitHeader) { |
||
| 461 | if ($subnode->hasChildNodes()) { |
||
| 462 | $this->writeContent($subtitle, $subdoc, $subnode, $sort, $publishPages); |
||
| 463 | $sort++; |
||
| 464 | } |
||
| 465 | |||
| 466 | $subdoc = new DOMDocument(); |
||
| 467 | $subnode = $subdoc->createElement('body'); |
||
| 468 | $subtitle = trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))); |
||
| 469 | } else { |
||
| 470 | $subnode->appendChild($subdoc->importNode($node, true)); |
||
| 471 | } |
||
| 472 | |||
| 473 | $node = $node->nextSibling; |
||
| 474 | } |
||
| 475 | } else { |
||
| 476 | $this->writeContent($subtitle, $subdoc, $body, null, $publishPages); |
||
| 477 | } |
||
| 478 | |||
| 479 | if ($subnode->hasChildNodes() && !$documentImporterFieldError) { |
||
| 480 | $this->writeContent($subtitle, $subdoc, $subnode, null, $publishPages); |
||
| 481 | } |
||
| 482 | |||
| 483 | restore_error_handler(); |
||
| 484 | if ($documentImporterFieldError) { |
||
| 485 | return ['error' => $documentImporterFieldError]; |
||
| 486 | } |
||
| 487 | |||
| 488 | foreach ($this->unusedChildren as $child) { |
||
| 489 | $origStage = Versioned::current_stage(); |
||
| 490 | |||
| 491 | Versioned::reading_stage('Stage'); |
||
| 492 | $draft = clone $child; |
||
| 493 | $draft->delete(); |
||
| 494 | |||
| 495 | Versioned::reading_stage('Live'); |
||
| 496 | $published = clone $child; |
||
| 497 | $published->delete(); |
||
| 498 | |||
| 499 | Versioned::reading_stage($origStage); |
||
| 500 | } |
||
| 501 | |||
| 502 | $sourcePage->write(); |
||
| 503 | } |
||
| 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