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

Nl2br   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 0
cbo 0
dl 10
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A nl2br() 0 17 3
A getContent() 10 10 3
A getClonedElement() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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