Completed
Pull Request — master (#97)
by
unknown
04:00 queued 49s
created

Nl2br::nl2br()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
namespace Transphporm\Formatter;
3
class Nl2br {
4
    public function nl2br($var) {
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
5
        $parts = explode("\n", $var);
6
        $doc = new \DomDocument();
7
        $root = $doc->createElement('root');
8
        $doc->appendChild($root);
9
10
        foreach ($parts as $key => $part) {
11
            $new = $doc->createTextNode($part);
12
            $doc->documentElement->appendChild($new);
13
            if ($key !== count($parts)-1) {
14
                $br = $doc->createElement('br');
15
                $doc->documentElement->appendChild($br);
16
            }
17
        }
18
19
        return $this->getContent($doc);
20
    }
21
22 View Code Duplication
    private function getContent($document) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
		$newNode = $document->documentElement;
24
		$result = [];
25
		if ($newNode->tagName === 'root') {
26
			foreach ($newNode->childNodes as $node) {
27
				$result[] = $this->getClonedElement($node);
28
			}
29
		}
30
		return $result;
31
	}
32
33
	private function getClonedElement($node) {
34
		$clone = $node->cloneNode(true);
35
		return $clone;
36
	}
37
}
38