Completed
Push — master ( 3479d6...be6b9d )
by Tom
03:47
created

BasicProperties   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 125
Duplicated Lines 11.2 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 12
Bugs 1 Features 1
Metric Value
wmc 36
c 12
b 1
f 1
lcom 2
cbo 2
dl 14
loc 125
rs 8.8

16 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 3 1
A __construct() 0 5 1
A content() 0 10 3
A getContentMode() 0 3 2
A processPseudo() 0 3 4
A pseudoAttr() 0 6 2
A pseudoHeader() 0 6 2
A pseudoBefore() 4 6 2
A pseudoAfter() 4 6 2
A appendToIfNode() 0 10 4
A replaceContent() 3 6 2
A appendContent() 3 5 2
A removeAllChildren() 0 3 2
A createHook() 0 5 2
A repeat() 0 22 3
A display() 0 4 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         0.9                                                             */
7
namespace Transphporm\Hook;
8
class BasicProperties {
9
	private $data;
10
	private $headers;
11
	private $formatter; 
12
13
	public function __construct($data, &$headers, Formatter $formatter) {
14
		$this->data = $data;
15
		$this->headers = &$headers;
16
		$this->formatter = $formatter;
17
	}
18
19
	public function content($value, $element, $rule) {
20
		$value = $this->formatter->format($value, $rule->getRules());
21
		if (!$this->processPseudo($value, $element, $rule)) {
22
			//Remove the current contents
23
			$this->removeAllChildren($element);
24
			//Now make a text node
25
			if ($this->getContentMode($rule->getRules()) === 'replace') $this->replaceContent($element, $value);
26
			else $this->appendContent($element, $value);
27
		}
28
	}
29
30
	private function getContentMode($rules) {
31
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
32
	}
33
34
	private function processPseudo($value, $element, $rule) {
35
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
36
	}
37
38
	private function pseudoAttr($value, $element, $rule) {
39
		if ($attr = $rule->getPseudoMatcher()->attr()) {
40
			$element->setAttribute($attr, implode('', $value));
41
			return true;
42
		}
43
	}
44
45
	private function pseudoHeader($value, $element, $rule) {
46
		if ($header = $rule->getPseudoMatcher()->header($element)) {
47
			$this->headers[] = [$header, implode('', $value)];
48
			return true;
49
		}
50
	}
51
52
	private function pseudoBefore($value, $element, $rule) {
53 View Code Duplication
		if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
			$element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue;
55
			return true;
56
		}
57
	}
58
59
	private function pseudoAfter($value, $element, $rule) {
60 View Code Duplication
		 if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
		 	$element->firstChild->nodeValue .= implode('', $value);
62
		 	return true;
63
		 }		 
64
	}
65
66
	private function appendToIfNode($element, $content, $appendTo) {
67
		if (isset($content[0]) && $content[0] instanceof \DomNode) {
68
			foreach ($content as $node) {
69
				$node = $element->ownerDocument->importNode($node, true);
70
				$appendTo->appendChild($node);
71
			}
72
			return true;
73
		}
74
		return false;
75
	}
76
77
	private function replaceContent($element, $content) {
78 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element->parentNode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79
			$element->parentNode->appendChild($element->ownerDocument->createElement('span', implode('', $content)));
80
		}		
81
		$element->setAttribute('transphporm', 'remove');
82
	}
83
84
	private function appendContent($element, $content) {
85 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
			$element->appendChild($element->ownerDocument->createTextNode(implode('', $content)));
87
		}
88
	}
89
90
	private function removeAllChildren($element) {
91
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
92
	}
93
94
	private function createHook($newRules, $rule) {
95
		$hook = new Rule($newRules, $rule->getPseudoMatcher(), $this->data);
96
		foreach ($rule->getProperties() as $obj) $hook->registerProperties($obj);
97
		return $hook;
98
	}
99
100
	public function repeat($value, $element, $rule) {
101
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
102
103
		foreach ($value as $key => $iteration) {
104
			$clone = $element->cloneNode(true);
105
			//Mark this node as having been added by transphporm
106
			$clone->setAttribute('transphporm', 'added');
107
			$this->data->bind($clone, $iteration, 'iteration');
108
			$this->data->bind($clone, $key, 'key');
109
			$element->parentNode->insertBefore($clone, $element);
110
111
			//Re-run the hook on the new element, but use the iterated data
112
			$newRules = $rule->getRules();
113
			//Don't run repeat on the clones element or it will loop forever
114
			unset($newRules['repeat']);
115
116
			$this->createHook($newRules, $rule)->run($clone);
117
		}
118
		//Flag the original element for removal
119
		$element->setAttribute('transphporm', 'remove');
120
		return false;
121
	}
122
123
	public function display($value, $element) {
124
		if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove');
125
		else $element->setAttribute('transphporm', 'show');
126
	}
127
128
	public function bind($value, $element) {
129
		$this->data->bind($element, $value);
130
	}
131
132
}