ar_htmlElement   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 42
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 7
loc 42
rs 10
c 0
b 0
f 0
ccs 0
cts 37
cp 0
wmc 14
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
C toString() 7 30 12
A getNodeList() 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
	ar_pinp::allow( 'ar_html' );
3
	ar_pinp::allow( 'ar_htmlNode' );
4
	ar_pinp::allow( 'ar_htmlElement' );
5
	ar_pinp::allow( 'ar_htmlNodes' );
6
7
	class ar_html extends ar_xml {
8
9
		public static $xhtml = false;
10
		public static $preserveWhiteSpace = false;
11
		private static $emptyTags = array(
12
			'input' => 1, 'br'       => 1, 'hr'      => 1, 'img'  => 1, 'link'  => 1, 'meta' => 1, 'frame' => 1,
13
			'base'  => 1, 'basefont' => 1, 'isindex' => 1, 'area' => 1, 'param' => 1, 'col'  => 1, 'embed' => 1
14
		);
15
		private static $noIndentInside = array(
16
			'textarea' => 1
17
		);
18
19
		public static function configure( $option, $value ) {
20
			switch ($option) {
21
				case 'xhtml' :
22
					self::$xhtml = (bool)$value;
23
				break;
24
				default:
25
					parent::configure($option, $value);
26
				break;
27
			}
28
		}
29
30
		public function __set( $name, $value ) {
31
			ar_html::configure( $name, $value );
32
		}
33
34
		public function __get( $name ) {
35
			if ( isset( ar_html::${$name} ) ) {
36
				return ar_html::${$name};
37
			}
38
		}
39
40
		public static function doctype( $type = 'strict', $quirksmode = false ) {
41
			$version = '';
42
			if ($type) {
43
				$type = strtolower( $type );
44
				switch ( $type ) {
45
					case 'transitional' :
46
					case 'frameset' :
47
						$version = ucfirst( $type );
48
					case 'strict' :
49
						if (self::$xhtml) {
50
							$version = ucfirst( $type );
51
							$type = '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-' . $type . '.dtd"';
52
						} else {
53
							$type = '"http://www.w3.org/TR/html4/' . $type . '.dtd"';
54
						}
55
					break;
56
				}
57
				if ($version) {
58
					$version = ' ' . $version;
59
				}
60
			}
61
			if (self::$xhtml) {
62
				$doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0' . $version . '//EN"';
63
			} else {
64
				$doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01' . $version . '//EN"';
65
			}
66
			if ( !$quirksmode || self::$xhtml) {
67
				$doctype .= ' ' . $type;
68
			}
69
			$doctype .= ">\n";
70
			return new ar_htmlNode($doctype);
71
		}
72
73
		public static function canHaveContent( $name ) {
74
			return !isset( self::$emptyTags[strtolower($name)] );
75
		}
76
77
		public static function canIndentInside( $name ) {
78
			return !isset( self::$noIndentInside[strtolower($name)] );
79
		}
80
81
		public static function tag() {
82
			$args = func_get_args();
83
			return call_user_func_array( array( 'ar_html', 'el' ), $args );
84
		}
85
86
		public static function element() {
87
			$args = func_get_args();
88
			return call_user_func_array( array( 'ar_html', 'el' ), $args );
89
		}
90
91 View Code Duplication
		public static function el() {
92
			$args = func_get_args();
93
			$name = array_shift($args);
94
			$attributes = array();
95
			$childNodes = array();
96
			foreach ($args as $arg) {
97
				if ( is_array( $arg ) && !is_a( $arg, 'ar_xmlNodes' ) ) {
98
					$attributes = array_merge($attributes, $arg);
99
				} else if ($arg instanceof ar_xmlNodes) {
100
					$childNodes = array_merge($childNodes, (array) $arg);
101
				} else {
102
					$childNodes[] = $arg;
103
				}
104
			}
105
			if ( !count( $childNodes ) ) {
106
				$childNodes = null;
107
			} else {
108
				$childNodes = new ar_htmlNodes( $childNodes );
109
			}
110
			return new ar_htmlElement($name, $attributes, $childNodes);
111
		}
112
113
		public static function nodes() {
114
			$args  = func_get_args();
115
			$nodes = call_user_func_array( array( 'ar_htmlNodes', 'mergeArguments' ), $args );
116
			return new ar_htmlNodes( $nodes );
117
		}
118
119
		public static function form( $fields, $buttons=null, $action='', $method='POST' ) {
120
			return new ar_html_form( $fields, $buttons, $action, $method );
121
		}
122
123
		public static function table( $rows, $attributes = null, $childNodes = null, $parentNode = null ) {
124
			return new ar_html_table( $rows, $attributes, $childNodes, $parentNode );
125
		}
126
127
		public static function menu() {
128
			$args = func_get_args();
129
			return call_user_func_array( array( 'ar_html_menu', 'el' ), $args );
130
		}
131
132
		public static function zen( $string ) {
133
			return new ar_html_zen( $string );
134
		}
135
136
		public static function editable() {
137
			$args = func_get_args();
138
			return call_user_func_array( 'ar_html_edit::el', $args );
139
		}
140
141
		protected static function parseChildren( $DOMElement ) {
142
			$result = array();
143
			foreach ( $DOMElement->childNodes as $child ) {
144 View Code Duplication
				if ( $child instanceof DOMCharacterData ) {
145
					if ( self::$preserveWhiteSpace || trim( $child->data )!=='' ) {
146
						$result[] = new ar_htmlNode( $child->data );
147
					}
148
				} else if ( $child instanceof DOMCdataSection ) {
149
					if ( self::$preserveWhiteSpace || trim( $child->data )!=='' ) {
150
						$result[] = self::cdata( $child->data );
151
					}
152
				} else if ( $child instanceof DOMNode ) {
153
					$result[] = self::el( $child->tagName, self::parseAttributes( $child ), self::parseChildren( $child ) );
0 ignored issues
show
Bug introduced by
The property tagName does not seem to exist in DOMNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
154
				}
155
			}
156
			return self::nodes( $result );
157
		}
158
159
		public static function parse( $html, $encoding = null ) {
160
			// important: parse must never return results with simple string values, but must always
161
			// wrap them in an ar_htmlNode, or tryToParse may get called, which will call parse, which
162
			// will... etc.
163
			$dom = new DOMDocument();
164
			if ( $encoding ) {
165
				$html = '<?xml encoding="' . $encoding . '">' . $html;
166
			}
167
			$prevErrorSetting = libxml_use_internal_errors(true);
168
			if ( $dom->loadHTML( $html ) ) {
169 View Code Duplication
				if ( $encoding ) {
170
					foreach( $dom->childNodes as $item ) {
171
						if ( $item->nodeType == XML_PI_NODE ) {
172
							$dom->removeChild( $item );
173
							break;
174
						}
175
					}
176
					$dom->encoding = $encoding;
177
				}
178
				$domroot = $dom->documentElement;
179
				if ( $domroot ) {
180
					$result = self::parseHead( $dom );
181
					$result[] = self::el( $domroot->tagName, self::parseAttributes( $domroot ), self::parseChildren( $domroot ) );
182
					return $result;
183
				}
184
			}
185
			$errors = libxml_get_errors();
186
			libxml_clear_errors();
187
			libxml_use_internal_errors( $prevErrorSetting );
188
			return ar_error::raiseError( 'Incorrect html passed', ar_exceptions::ILLEGAL_ARGUMENT, $errors );
189
		}
190
191
		public static function tryToParse( $html ) {
192
			$result = $html;
193
			if ( ! ($html instanceof ar_xmlNodeInterface ) ) { // ar_xmlNodeInterface is correct, there is no ar_htmlNodeInterface
194
				if ( $html && strpos( $html, '<' ) !== false ) {
195
					try {
196
						$result = self::parse( $html, 'UTF-8' );
197
						if ( ar_error::isError($result) ) {
198
							$result = new ar_htmlNode( (string) $html );
199
						} else {
200
							$check = trim($html);
201
							/*
202
								DOMDocument::loadHTML always generates a full html document
203
								so the next bit of magic tries to remove the added elements
204
							*/
205
							if (stripos($check, '<p') === 0 ) {
206
								$result = $result->html->body[0]->childNodes;
207
							} else {
208
								$result = $result->html->body[0];
209
								if ($result->firstChild->tagName=='p') {
210
									$result = $result->firstChild;
211
								}
212
								$result = $result->childNodes;
213
							}
214
						}
215
					} catch( Exception $e ) {
216
						$result = new ar_htmlNode( (string) $html );
217
					}
218
				} else {
219
					$result = new ar_htmlNode( (string) $html );
220
				}
221
			}
222
			return $result;
223
		}
224
	}
225
226
	class ar_htmlNodes extends ar_xmlNodes {
227
228
		public function toString( $indentWith = null ) {
229
			$indent = isset($indentWith) ? $indentWith : (
230
				ar_html::$indenting ? ar_html::$indent : ''
231
			);
232
			return parent::toString( $indent );
233
		}
234
235
		public function __toString() {
236
			return $this->toString();
237
		}
238
239
		public function getNodeList() {
240
			$params = func_get_args();
241
			return call_user_func_array( array( 'ar_html', 'nodes'), $params );
242
		}
243
244
		protected function _tryToParse( $node ) {
245
			return ar_html::tryToParse( $node );
246
		}
247
248
	}
249
250
	class ar_htmlNode extends ar_xmlNode {
251
252
	}
253
254
	class ar_htmlElement extends ar_xmlElement {
255
256
		public function __toString() {
257
			return $this->toString();
258
		}
259
260
		public function toString( $indent = '', $current = 0 ) {
261
			$indent = ar_html::$indenting ? $indent : '';
262
			$result = "\n" . $indent . '<' . ar_html::name( $this->tagName );
263 View Code Duplication
			if ( is_array($this->attributes) ) {
264
				foreach ( $this->attributes as $name => $value ) {
265
					$result .= ar_html::attribute($name, $value, $current);
266
				}
267
			} else if ( is_string($this->attributes) ) {
268
				$result .= ltrim(' '.$this->attributes);
269
			}
270
			if ( !ar_html::$xhtml || ar_html::canHaveContent( $this->tagName ) ) {
271
				$result .= '>';
272
				if ( ar_html::canHaveContent( $this->tagName ) ) {
273
					if ( isset($this->childNodes) && count($this->childNodes) ) {
274
						if (ar_html::canIndentInside( $this->tagName ) ) {
275
							$result .= $this->childNodes->toString( ar_html::$indent . $indent );
276
							if ( substr($result, -1) == ">") {
277
								$result .= "\n" . $indent;
278
							}
279
						} else {
280
							$result .= $this->childNodes->toString( '' );
281
						}
282
					}
283
					$result .= '</' . ar_html::name( $this->tagName ) . '>';
284
				}
285
			} else {
286
				$result .= ' />';
287
			}
288
			return $result;
289
		}
290
291
		public function getNodeList() {
292
			$params = func_get_args();
293
			return call_user_func_array( array( 'ar_html', 'nodes'), $params );
294
		}
295
	}
296