xml_parser   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 192
Duplicated Lines 10.42 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 20
loc 192
rs 9.52
c 0
b 0
f 0
ccs 0
cts 155
cp 0
wmc 36
lcom 2
cbo 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A _set_element_handler() 0 4 1
A _set_character_data_handler() 0 3 1
A _parse() 5 11 2
A _get_array() 5 18 3
B _parse_url() 5 24 6
A _parse_curl() 5 24 3
A call_tag_open() 0 7 2
A call_tag_close() 0 7 2
A call_tag_data() 0 7 2
A startElement() 0 20 4
A endElement() 0 19 5
A characterData() 0 19 4

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
	class pinp_xml {
3
		public static function _parser() {
4
			$context = pobject::getContext();
5
			$me      = $context['arCurrentObject'];
6
			$parser = new xml_parser($me);
7
			return $parser;
8
		}
9
10
		public static function _escape($text) {
11
			$search  = array('&','"',"'",'<','>');
12
			$replace = array('&amp;','&quot;','&apos;','&lt;','&gt;');
13
			return str_replace($search, $replace, $text);
14
		}
15
16
		public static function _unescape($text) {
17
			$search  = array('&quot;','&apos;','&lt;','&gt;','&amp;');
18
			$replace = array('"',"'",'<','>','&');
19
			return str_replace($search, $replace, $text);
20
		}
21
22
	};
23
24
	class xml_parser {
25
		protected $object;
26
		protected $tag_open_template;
27
		protected $tag_class_template;
28
		protected $tag_data_template;
29
		protected $ns = array();
30
		public $elements;
31
		public $MULTI_TAGS;
32
		public $error;
33
		public $rss_items;
34
35
36
		public function __construct($object) {
37
			$this->object = $object;
38
		}
39
40
		public function _set_element_handler($tag_open, $tag_close) {
41
			$this->tag_open_template = $tag_open;
42
			$this->tag_close_template = $tag_close;
0 ignored issues
show
Bug introduced by
The property tag_close_template does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
		}
44
45
		public function _set_character_data_handler($tag_data) {
46
			$this->tag_data_template = $tag_data;
47
		}
48
49
		public function _parse($string) {
50
			$parser = xml_parser_create();
51
			xml_set_object($parser, $this);
52
			xml_set_element_handler($parser, "call_tag_open", "call_tag_close");
53
			xml_set_character_data_handler($parser, "call_tag_data");
54 View Code Duplication
			if (!xml_parse($parser, $string)) {
55
				$this->error = sprintf("XML error: %s at line %d",
56
					xml_error_string(xml_get_error_code($parser)),
57
						xml_get_current_line_number($parser));
58
			}
59
		}
60
61
		public function _get_array($string, $MULTI_TAGS = array()) {
62
			$parser = xml_parser_create();
63
			$this->elements = array();
64
			$this->MULTI_TAGS = array();
65
			foreach ($MULTI_TAGS as $tag) {
66
				$this->MULTI_TAGS[] = strtoupper($tag);
67
			}
68
			xml_set_object($parser, $this);
69
			xml_set_element_handler($parser, "startElement", "endElement");
70
			xml_set_character_data_handler($parser, "characterData");
71 View Code Duplication
			if (!xml_parse($parser, $string)) {
72
				$this->error = sprintf("XML error: %s at line %d",
73
					xml_error_string(xml_get_error_code($parser)),
74
						xml_get_current_line_number($parser));
75
			}
76
77
			return $this->elements;
78
		}
79
80
		public function _parse_url($url) {
81
			if (!preg_match('|^https?://|i', $url)) {
82
				$this->error = "Not a valid URL ($url)";
83
			} else {
84
				$parser = xml_parser_create();
85
				xml_set_object($parser, $this);
86
				xml_set_element_handler($parser, "call_tag_open", "call_tag_close");
87
				xml_set_character_data_handler($parser, "call_tag_data");
88
				$fp = fopen($url, "r");
89
				if (!$fp) {
90
					$this->error = "Could not open ($url)";
91
				} else {
92
					while (!$this->error && !feof($fp)) {
93
						$string = fread($fp, 4096);
94 View Code Duplication
						if (!xml_parse($parser, $string)) {
95
							$this->error = sprintf("XML error: %s at line %d",
96
								xml_error_string(xml_get_error_code($parser)),
97
									xml_get_current_line_number($parser));
98
						}
99
					}
100
					fclose($fp);
101
				}
102
			}
103
		}
104
105
		public function _parse_curl($url) {
106
			if (!preg_match('|^https?://|i', $url)) {
107
				$this->error = "Not a valid URL ($url)";
108
			} else {
109
				$parser = xml_parser_create();
110
				xml_set_object($parser, $this);
111
				xml_set_element_handler($parser, "call_tag_open", "call_tag_close");
112
				xml_set_character_data_handler($parser, "call_tag_data");
113
114
				$ch = curl_init($url);
115
116
				curl_setopt($ch, CURLOPT_HEADER, 0);
117
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
118
119
				$string = curl_exec($ch);
120
				curl_close($ch);
121 View Code Duplication
				if (!xml_parse($parser, $string)) {
122
					$this->error = sprintf("XML error: %s at line %d",
123
					xml_error_string(xml_get_error_code($parser)),
124
					xml_get_current_line_number($parser));
125
				}
126
127
			}
128
		}
129
130
		public function call_tag_open($parser, $tag, $attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
			global $ARBeenHere;
132
			$ARBeenHere = array();
133
			if ($this->tag_open_template) {
134
				$this->object->call($this->tag_open_template, array("tag" => $tag, "attributes" => $attributes));
135
			}
136
		}
137
138
		public function call_tag_close($parser, $tag) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
			global $ARBeenHere;
140
			$ARBeenHere = array();
141
			if ($this->tag_close_template) {
142
				$this->object->call($this->tag_close_template, array("tag" => $tag));
143
			}
144
		}
145
146
		public function call_tag_data($parser, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
			global $ARBeenHere;
148
			$ARBeenHere = array();
149
			if ($this->tag_data_template) {
150
				$this->object->call($this->tag_data_template, array("tag_data" => $data));
151
			}
152
		}
153
154
155
		public function startElement($parser, $name, $attribs) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
		//global $MULTI_TAGS;
157
			$newElement = array();
158
			$element = &$this->elements;
159
160
			if (is_array($this->ns)) {
161
				foreach ($this->ns as $n) {
162
					$element = &$element[$n];
163
				}
164
			}
165
166
			$this->ns[] = $name;
167
			$newElement[':attribs'] = $attribs;
168
			if (!in_array($name, $this->MULTI_TAGS)) {
169
				$element[$name] = $newElement;
170
			} else {
171
				$element[$name][] = $newElement;
172
				$this->ns[] = count($element[$name])-1;
173
			}
174
		}
175
176
		public function endElement($parser, $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
		//global $MULTI_TAGS;
178
			$element = &$this->elements;
179
			foreach ($this->ns as $n) {
180
				$parentElement = $element;
181
				$element = &$element[$n];
182
			}
183
			switch ($name) {
184
				case 'item':
185
					$this->rss_items[] = $element;
186
					unset($parentElement[$name]);
187
				break;
188
			}
189
			$parent = $this->ns[count($this->ns)-2];
190
			if (in_array($parent, $this->MULTI_TAGS) && $parent === $name) {
191
				array_pop($this->ns);
192
			}
193
			array_pop($this->ns);
194
		}
195
196
		public function characterData($parser, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
			$element = &$this->elements;
198
			$name = "";
199
			foreach ($this->ns as $n) {
200
				$name .= ":$n";
201
				$element = &$element[$n];
202
			}
203
			switch ($n) {
0 ignored issues
show
Bug introduced by
The variable $n seems to be defined by a foreach iteration on line 199. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
204
				// do not put anything else above this line
205
				// or else '0' values will trigger it.
206
				case 0:
207
				default:
208
					if (!$element) {
209
						$element = array();
210
					}
211
					$element[':data'] .= $data;
212
				break;
213
			}
214
		}
215
	}
216