Completed
Push — master ( db6d82...fcd2b1 )
by Tom
09:17
created

Content   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 110
Duplicated Lines 14.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 37
c 4
b 2
f 0
lcom 1
cbo 2
dl 16
loc 110
rs 8.6

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 12 4
A getContentMode() 0 3 2
A processPseudo() 0 3 4
A pseudoAttr() 0 6 2
A pseudoHeader() 0 6 2
B getNode() 0 17 5
A pseudoBefore() 8 8 3
A pseudoAfter() 8 8 3
B removeAdded() 0 7 5
A replaceContent() 0 8 2
A appendContent() 0 5 2
A removeAllChildren() 0 3 2

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
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Property;
8
class Content implements \Transphporm\Property {
9
	private $data;
10
	private $headers;
11
	private $formatter;
12
13
14
	public function __construct($data, &$headers, \Transphporm\Hook\Formatter $formatter) {
15
		$this->data = $data;
16
		$this->headers = &$headers;
17
		$this->formatter = $formatter;
18
	}
19
20
	public function run($value, \DomElement $element, \Transphporm\Hook\PropertyHook $rule) {
21
		if ($element->getAttribute('transphporm') === 'remove') return;
22
				
23
		$value = $this->formatter->format($value, $rule->getRules());
24
		if (!$this->processPseudo($value, $element, $rule)) {
25
			//Remove the current contents
26
			$this->removeAllChildren($element);
27
			//Now make a text node
28
			if ($this->getContentMode($rule->getRules()) === 'replace') $this->replaceContent($element, $value);
29
			else $this->appendContent($element, $value);
30
		}
31
	}
32
33
	private function getContentMode($rules) {
34
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
35
	}
36
37
	private function processPseudo($value, $element, $rule) {
38
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
39
	}
40
41
	private function pseudoAttr($value, $element, $rule) {
42
		if ($attr = $rule->getPseudoMatcher()->attr()) {
43
			$element->setAttribute($attr, implode('', $value));
44
			return true;
45
		}
46
	}
47
48
	private function pseudoHeader($value, $element, $rule) {
49
		if ($header = $rule->getPseudoMatcher()->header($element)) {
50
			$this->headers[] = [$header, implode('', $value)];
51
			return true;
52
		}
53
	}
54
55
	private function getNode($node, $document) {
56
		foreach ($node as $n) {
57
			if ($n instanceof \DomElement) {
58
				$new = $document->importNode($n, true);
59
				$new->setAttribute('transphporm', 'added');
60
			}
61
			else {
62
				if ($n instanceof \DomText) $n = $n->nodeValue;
63
				if ($n == '') continue;
64
65
				$new = $document->createElement('text');
66
				$new->appendChild($document->createTextNode($n));
67
				$new->setAttribute('transphporm', 'text');
68
			}
69
			yield $new;
70
		}
71
	}
72
73 View Code Duplication
	private function pseudoBefore($value, $element, $rule) {
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...
74
		if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) {
75
			foreach ($this->getNode($value, $element->ownerDocument) as $node) {
76
				$element->insertBefore($node, $element->firstChild);	
77
			}
78
			return true;
79
		}
80
	}
81
82 View Code Duplication
	private function pseudoAfter($value, $element, $rule) {
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...
83
		 if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) {
84
		 	foreach ($this->getNode($value, $element->ownerDocument) as $node) {
85
		 		$element->appendChild($node);
86
			}
87
		 	return true;
88
		 }		 
89
	}
90
91
	private function removeAdded($e) {
92
		$remove = [];
93
		while ($e = $e->previousSibling && $e->getAttribute('transphporm') != null && $e->getAttribute('transphporm') != 'remove') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $e = ($e->previousSiblin...nsphporm') != 'remove'), Probably Intended Meaning: ($e = $e->previousSiblin...ansphporm') != 'remove'
Loading history...
94
			$remove[] = $e;
95
		}
96
		foreach ($remove as $r) $r->parentNode->removeChild($r);
97
	}
98
99
	private function replaceContent($element, $content) {
100
		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
101
		$this->removeAdded($element);
102
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
103
			$element->parentNode->insertBefore($node, $element);
104
		}		
105
		$element->setAttribute('transphporm', 'remove');
106
	}
107
108
	private function appendContent($element, $content) {
109
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
110
			$element->appendChild($node);
111
		}
112
	}
113
114
	private function removeAllChildren($element) {
115
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
116
	}
117
}