Completed
Push — master ( be4263...a7e3b2 )
by Tom
05:14
created

BasicProperties   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 127
Duplicated Lines 18.9 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 36
c 10
b 1
f 1
lcom 2
cbo 2
dl 24
loc 127
rs 8.8

15 Methods

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