Completed
Push — master ( fcd2b1...f8e36b )
by Tom
07:22
created

Content   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 108
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 35
c 5
b 2
f 0
lcom 1
cbo 2
dl 16
loc 108
rs 9

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
A getNode() 0 15 4
A pseudoBefore() 8 8 3
A pseudoAfter() 8 8 3
A removeAdded() 0 7 4
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
				$new = $document->createElement('text');
64
				$new->appendChild($document->createTextNode($n));
65
				$new->setAttribute('transphporm', 'text');
66
			}
67
			yield $new;
68
		}
69
	}
70
71 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...
72
		if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) {
73
			foreach ($this->getNode($value, $element->ownerDocument) as $node) {
74
				$element->insertBefore($node, $element->firstChild);	
75
			}
76
			return true;
77
		}
78
	}
79
80 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...
81
		 if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) {
82
		 	foreach ($this->getNode($value, $element->ownerDocument) as $node) {
83
		 		$element->appendChild($node);
84
			}
85
		 	return true;
86
		 }		 
87
	}
88
89
	private function removeAdded($e) {
90
		$remove = [];
91
		while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $e = ($e->previousSiblin...array(null, 'remove'))), Probably Intended Meaning: ($e = $e->previousSiblin... array(null, 'remove'))
Loading history...
92
			$remove[] = $e;
93
		}
94
		foreach ($remove as $r) $r->parentNode->removeChild($r);
95
	}
96
97
	private function replaceContent($element, $content) {
98
		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
99
		$this->removeAdded($element);
100
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
101
			$element->parentNode->insertBefore($node, $element);
102
		}		
103
		$element->setAttribute('transphporm', 'remove');
104
	}
105
106
	private function appendContent($element, $content) {
107
		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
108
			$element->appendChild($node);
109
		}
110
	}
111
	
112
	private function removeAllChildren($element) {
113
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
114
	}
115
}